Overview of the Java JSON Jackson library
The java JSON Jackson is a JSON library for Java It has very powerful data binding capabilities and provides a framework to serialize custom java objects to JSON and deserialize JSON back to Java objects. JSON written with Jackson can contain embedded class information that helps in creating the complete object tree during deserialization.
Java JSON Jackson – Creating JSON from JAVA
There are three ways to create JSON from JAVA:
- From a Java Object (The Same object can also be then used to read the JSON)
- From a JsonNode Tree
- Building a JSON Stream
In the first tutorial, we look at all the three ways of creating JSON. Note that if you are new to Jackson this is probably the best place to begin even if you are looking at a way to parse JSON.
Java JSON Jackson – Parsing JSON
The first tutorial showed how to convert a java object to JSON. The same java object can then be used to bind the JSON to java. However, as the author of Jackson points out, there are three ways to parse JSON using Jackson.
- Streaming – use the JsonParser to parse the JSON stream. It provides the JSON elements as tokens. Use the JsonGenerator to generate the JSON from String, integer, boolean etc.
- Tree Traversing – the compete JSON can be read into a JsonNode. The node can then be traversed to get the required property. A tree can also be created and then written as JSON string
- Data Binding – Binding the JSON to custom java objects (POJO). Jackson offers a lot of flexibility around these. Annotation can be used to mark properties and constructors on the POJO.
Java JSON Jackson – Annotation and Serialization
Jackson provides certain features that make the API extremely useful and flexible. These features are
- List Serialization – Serialization list can be tricky if type info is to be preserved. Look at this example to see how to do that.
- Annotation and Dynamic beans – Serialize unknown properties of JSON into a Map in the Java Object.
- Annotation Filters – While data binding, properties of the POJO can be ignored or filtered out. These filters can be set using annotation on the POJO or by using custom filters in the calling class.
- Mix-in – Annotations on POJO can be used to map the POJO properties to JSON properties and to also mark constructors for use during data binding. However, what if the POJO is from a third party, and cannot be annotated. Mix-in comes to the rescue. It provides a way to annotate third party classes.
- Polymorphic Behaviour – If a JSON is created using objects that are specific instances of an abstract class, then Jackson provides a way to create the exact subclass
The subsequent tutorials provide examples for each of the above cases.
2 thoughts on “Java JSON Jackson Introduction”
Hello, am really hoping to discover WHAT has been keeping my JSF web app from not being able to bind remote MySQL (AWS RDS) as a converted valid JSON string to display data in a simple html table.
Have tried everything. Am here to learn and discover WHAT prevents my successful completion of this servlet with web.xml coding.
Nice article . keep this work up