Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
com.fasterxml.jackson.databind.ObjectWriter Example
How to use com.fasterxml.jackson.databind.ObjectWriter class?
✍: FYIcenter.com
com.fasterxml.jackson.databind.ObjectWriter class allows you to map a Java class object to a JSON message in a pretty format.
Here is an example Java program, ObjectMapperWriter.java:
// ObjectMapperWriter.java
// Copyright (c) FYIcenter.com
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
public class ObjectMapperWriter {
public static void main(String[] args) throws Exception {
Person person = new Person();
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
String json = writer.writeValueAsString(person);
System.out.println(json);
}
public static class Person {
private String name = "John Smith";
private boolean married = false;
private int age = 25;
private String phone = null;
public String getName() {
return this.name;
}
public boolean getMarried() {
return this.married;
}
public int getAge() {
return this.age;
}
public String getPhone() {
return this.phone;
}
}
}
Run this Java program with 3 Jackson JAR files:
fyicenter$ java -cp jackson-core-2.14.0.jar: \
jackson-databind-2.14.0.jar: \
jackson-annotations-2.14.0.jar \
ObjectMapperWriter.java
{
"name" : "John Smith",
"married" : false,
"age" : 25,
"phone" : null
}
The Java program mapped a Java class object to a JSON message correctly.
⇒ com.fasterxml.jackson.dataformat.xml.XmlMapper Example
⇐ com.fasterxml.jackson.databind.ObjectMapper Example
2021-08-11, ∼1366🔥, 0💬
Popular Posts:
JLayer is a library that decodes/plays/converts MPEG 1/2/2.5 Layer 1/2/3 (i.e. MP3) in real time for...
jTDS JDBC Driver Source Code Files are provided in the source package file, jtds-1.3.1-fyi.zip. You ...
jlGui is a music player for the Java platform. It is based on Java Sound 1.0 (i.e. JDK 1.3+). It sup...
What Is jaxb-api-2.1.6.jar? Java Architecture for XML Binding (JAXB) is a Java API that allows Java ...
What Is poi-3.5.jar - Part 2? poi-3.5.jar is one of the JAR files for Apache POI 3.5, which provides...