Serialize and de-serialize with Jackson

At the first time to serialize Java objects and vice versa, we are overwhelmed when there are a lot of JSON libraries. We come to the three libraries in our final list: XStream, Gson, and Jackson. After that, we excluded the XStream out of the list because XStream did not do a good job when it binds the Java object to the JSON and vice versa. It is very hard to custom the output of JSON for the complicated Java objects via XStream’s Converter and in many cases it is impossible.

Gson is the good JSON library, and both it and Jackson are simple to use. However, we choose Jackson because it is integrated flawlessly with Spring framework. The starter is simple to write.

First, to add the Jackson libraries to the Maven project

And we write the simple program to serialize an object to JSON. Below code is written in Scala, but the Java developers can convert it to Java easily

In MyCollab, we custom the serializer and de-serializer process to marshall a hierarchy object model. Let it simple for this blog post; I add a Address class is an attribute of the class Person

We define the two classes Person and Address as following

and create a Person object to serialize it to JSON and deserialize to object again

The JSON output is {"firstName":"Nguyen","lastName":"Hai","address":[{"name":"class 1","time":{"chronology":{"zone":{"fixed":true,"id":"UTC"}},..... It means Jackson does not understand how to serialize the joda class LocalDateTime. We will write the JSON serializer and deserializer

Json Serializer

Create a new class extends the JsonSerializer

and register it to mapper by adding the @JsonSerialize on the class level Classroom

then the output result looks as expected {"firstName":"Nguyen","lastName":"Hai","address":[{"name":"class 1","time":"01-Jan-2016"},{"name":"class 2","time":"01-Jan-2016"}]}

JSON Deserializer

Create a new class extends JsonDeserializer

and register it to mapper by adding the @JsonDeserialize on the class level Classroom

then the Jackson will deserialize JSON value to the object Person as expected

Treating the NULL values

By default, the Jackson will produce the NULL string if your Java object has NULL property. To ignore the NULL value, you must add the annotation @JsonInclude(JsonInclude.Include.NON_NULL) on the top level serialized class.

Jackson and XML

JSON is ours prefer format. However, we have worked with XML in a few cases, and Jackson also does the right job with XML format as well. You should add the library jackson-dataformat-xml to your project, below is the Maven dependency declaration

Instead of using the ObjectMapper, you use the XmlMapper instance

There are several annotations for the XML element you want to use to align with your bean properties and XML format, such as JacksonXmlRootElement or JacksonXmlProperty. All the rest should be transparent with the XML or JSON formats you are working.

You can check the full Scala source code at here. Still having a question? Please put it in the below comment

Leave a Reply

Your email address will not be published. Required fields are marked *