# Jackson: Java Parsing Json Library

Jackson is a simple Java-based library to serialize Java objects to JSON and vice versa. It has some features like easy to use, no need mapping, good performance, clean JSON, no dependency, and opne source.

There are mainly 3 usages for Jackson library.

# Tree Model

ObjectMapper object will provide a pointer to the root node of your JSON tree after reading the JSON string, and then we can use the root node to traverse the whole tree.
Here is the code to create ObjectMapper and get the root JSON node:

And then you can use the the root node to traverse the whole tree like:

And the printing result is:

If you want to modify value of some fields, you cannot directly change it in the JsonNode because it is immutable, which means read-only. However, we can cast JsonNode to ObjectNode first, and then we can modify the values:

put method will check if there is an old value; if yes, then overwrite it; else just put in the new value.

And if you want to put new value in an array, then cast it to ArrayNode and use add methdo to add new value:

Using this tree model, you can easily cast a JSON String to a readable JSON Tree, and also you can cast a JSON Tree to a JSON String as well. However, there is a small different between getTextValue() and toString(); if using getTextValue(), there won’t be any double quotes in the result String; if using toString(), it will keep all the double quotes.

# Data Binding

It converts JSON to and from Plain Old Java Object (POJO) using property accessor or using annotations. ObjectMapper reads or writes JSON for both types of data bindings. Data binding is analogous to JAXB parser for XML. Data binding is of two types:

• Simple Data Binding
It converts JSON to and from Java Maps, Lists, Strings, Numbers, Booleans, and null objects.
• Full Data Binding
It converts JSON to and from any Java type.

The code above is an example of creating a JSON string with Student details and then deserializing it to Student object and then serializing it back to a JSON string.

For the inner class, you must use static class for Jackson because non-static inner class will not have a default zero-argument constructor which is required for using Jackson data binding. For more details, check here.

If you want to ignore null value, using this:

If you want to ignore non-matching field in the json, using this:

# Streaming API

Streaming API reads and writes JSON content as discrete events. JsonParser reads the data, and JsonGenerator writes the data.

Here is the usage of JsonParser:

Here is the usage of JsonGenerator:

For more details, check here.