Java json – jackson List serialization

Also see json simple org.json

In this tutorial we will see how to convert a java List to JSON. Serializing list is a little tricky since by default the type info is not stored while serializing and deserializing lists. In this tutorial we look at two examples. In the first example we serialize an Object that has a java List as one of its properties. In the second example we try and serialize the List directly. In both examples we use special configuration to preserve type info.

Example 1 : Serializing Object containing a list

The example converts a Zoo class to json. the zoo class contains the name of zoo, its city and a list of animals. The list is of type ‘Animal’, i.e. the list contains elements that are subclass of the Abstract class Animal. Lets see what happens when we try to serialize zoo. First we create the Zoo class. Notice how the constructor looks. When we try to get the Zoo Object back from the JSON, Jackson has to know that it should create the Zoo Object using the constructor that takes in the name and city properties.

The Animal is an abstract class. It is further extended to create the Elephant and the Lion class.

Lets now create a serializer for Zoo. Imagine that the London zoo needs to shift to a larger area. We will have to serialize the zoo from its current location and deserialize it at the target location. (a teleportation machine)

We use the ObjectMapper class to do the serialization and print the json to console. You could also print it to a file

Ok so lets see how our zoo looks like. Remember, we should be able to re create the zoo (along with all its animals) when we deserialize it

Clearly our teleporter is not ready. Manny and Simba are in there, but there is nothing in the json that says that Manny is an elephant and Simba is a lion. Lets see what happens when we try to deserialize the zoo

We get an error! the error says “Can not construct instance of com.studytrails.json.jackson.Animal, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information” . Ok, we were expecting this. Lets see how to resolve it. We need to be able to put the information about the type in the json. There are two things you need to do this 1. Tell Jackson that we need to include the type info for Animal class.

2. Tell Jackson that Animal can have subclasses Elephant and Lion

Lets see how our json looks now

This looks much better. Now, if you read the json back using the Zoo class then it works well and the Zoo class would have a list of animals.

Example 2 : Serializing a list

In the previous example we saw how to serialize a class that has a List. In this example lets see how to serialize a list directly. We will use the same Animal, Lion and Elephant classes. Lets see what happens when we try to serialize the list directly (note that we have added the typeinfo annotations to the Animal class)

This gives the following json

No type info! To add type info while serializing Lists directly you need to configure the mapper in this way

This now produces the correct json

So to recap, in this tutorial we saw how to i. serialize a Java object containing a List and ii. serialize a list (root object)

Java json – jackson List serialization

One thought on “Java json – jackson List serialization

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top