Serialize Java object to JSON and back

There are many libraries in Java that support serialization of Java objects to JSON and back. Below are few of them.

1. json-lib
2. json-io
3. Flexjson
4. Jackson

Jackson is a very popular JSON processor and is widely used. This tutorial will demonstrate how to serialize Java object to JSON and de-serialize it back using Jackson 2.x library.

In this example, we have an Employee class that contains some mixed type of properties namely integer, String and custom Compensation class type. We will serialize the Employee object to JSON and back. We will show how the serialized object looks like. And then, we will demonstrate how to control the serialization process with custom serializers and de-serializers.

First, take a look at the Employee class that will be serialized.

Below is the Compensation class that the Employee class internally refers to.

 

Object to JSON

Below is the main class that serializes the Employee object.

First, we create the Jackson ObjectMapper instance. ObjectMapper is the data binder that provides functionality for converting between Java objects and matching JSON constructs. Then, we call the writeValue() method on the binder passing in the OutputStream where the object will be serialized and the object itself, that is to be serialized. The writeValue() is overloaded to take File or Writer types to which the input object can be serialized.  

Output

 

JSON to Object

To de-serialize, we call the readValue() method of the ObjectMapper passing in the JSON source and the class type to whose instance the JSON would be deserialized to. The readValue() method is overloaded to accept other JSON input sources such as byte[], String, URL, File, InputStream etc.

Output

 

Custom Serializers and De-serializers

The default serialization is desirable in most cases but sometimes you may need more control on the way the objects are serialized. For eg. In the serialization example above, you may want the Compensation object to be serialized as a string and not as an object in the JSON.

Also, when this string is being de-serialized it should be correctly interpreted and mapped to the correct class instance.  To handle such customizations, Jackson allows you to write custom serializers and de-serializers.

Writing custom serializers and de-serializers is quite simple. To write a custom serializer, all you need to do is extend the generic class com.fasterxml.jackson.databind.JsonSerializer by specifying the type to be serialized as the type parameter and implement the serialize() method. The serialize() method accepts the type parameter that you specified and a JsonGenerator type.

The custom implementation of the serialize() method is supposed to extract information out of the input type parameter instance and use the JsonGenerator instance to serialize the data the way it desires. Lets write a custom serializer for the Compensation class from the earlier example and serialize it as a string instead of an object.

Now, for the ObjectMapper to use our custom serializer, we need to create a module and add the custom serializer for the Compensation class and then register the module with ObjectMapper. Below is the code.

 

Custom Serializer Example

Output

 

Custom De-Serializer Example

Since, the compensation is serialized as a primitive type string and not as an object, you need to write a custom de-serializer for the Employee object and not the primitive type string.

While de-serializing the Employee object you need to split the compensation value into the currency character and the salary and then populate them into the Compensation object. Below is the code.

De-serializer for the Employee class

Main class

Output

 


 

3
Leave a Reply

avatar
300
2 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
AlexDronaJohn DeRegnaucourt Recent comment authors
  Subscribe  
newest oldest
Notify of
Alex
Guest
Alex

I was forced to precede writing object with

jgen.writeStartObject();
and finish it with
jgen.writeEndObject();

otherwise received exception
“Can not write a field name, expecting a value”

John DeRegnaucourt
Guest
John DeRegnaucourt

json-io, a Java to JSON and JSON to Java serializer, mentioned in the article, is now hosted here: https://github.com/jdereg/json-io