Categories:
Audio (13)
Biotech (29)
Bytecode (35)
Database (77)
Framework (7)
Game (7)
General (512)
Graphics (53)
I/O (32)
IDE (2)
JAR Tools (86)
JavaBeans (16)
JDBC (89)
JDK (337)
JSP (20)
Logging (103)
Mail (54)
Messaging (8)
Network (71)
PDF (94)
Report (7)
Scripting (83)
Security (32)
Server (119)
Servlet (17)
SOAP (24)
Testing (50)
Web (19)
XML (301)
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.12.4.jar: \ jackson-databind-2.12.4.jar: \ jackson-annotations-2.12.4.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
⇑ Using Jackson Java JSON library
⇑⇑ Jackson - Java JSON library
2021-08-11, 412👍, 0💬
Popular Posts:
How to perform XML Schema validation with sax\Writer.java provided in the Apache Xerces package? You...
JCIFS is an Open Source client library that implements the CIFS/SMB networking protocol in 100% Java...
commons-net-1.4.1.jar is the JAR file for Apache Commons Net 1.4.1, which implements the client side...
Saxon-HE (home edition) is an open source product available under the Mozilla Public License. It pro...
JDK 8 tools.jar is the JAR file for JDK 8 tools. It contains Java classes to support different JDK t...