This tutorial is going to cover how to convert a Java object to JSON string and vice versa using both Jackson 2  and Gson libraries.

1. Preparation

1.1. POJO

Firstly, let’s see a POJO class which will be converted to JSON for all our examples.

The Book has 3 fields: id, name, and author. Besides, it also has a default constructor, getters, and setters for all fields. Note that there are 2 fields annotated with @JsonInclude(NON_NULL) which is used to tell Jackson to ignore or not include annotated fields when their values are NULL or empty during Jackson’s serialization.

1.2. Source code

The sample source code presented in this tutorial is available on Github. It’s a Maven based project, it should be easy to run or to be imported into IDE such as Eclipse, Intelli, etc.

1. Convert A Java Object To JSON With Jackson

This section is going to cover how to convert a Java object to JSON with Jackson 2, a very popular JSON library for Java.

1.1. Jackson Library Dependency

The only dependency of Jackson 2 library we need for all examples is:

1.2. Convert a Java object to JSON with Jackson

Let’s assume that we have an object “shBook” and we want to convert that book to JSON:

To convert a Java object to JSON with Jackson, we need to create an ObjectMapper object and use its writeValueAsString method to serialize any Java value as a String. In the above example, we have used the writerWithDefaultPrettyPrinter method to serialize objects using the Jackson default pretty printer for indentation. If we don’t need the pretty print feature, to convert a Java object to JSON, we can simply call:

Let’s see the JSON output of the object:

1.3. Convert a Java collection to Json with Jackson

We can also use the mapper.writeValueAsString method to convert a Java collection to JSON, for example:

Let’s see the JSON string of the collection:

To make the JSON string looks better, we can use the ObjectWriter to serialize the objects using the default pretty printer for indentation:

The Json output now looks better:

1.4. Convert a Java Map to Json with Jackson

Let’s assume we have a java.util.Map “bookMap” which its keys are the ids of books and values are the books themselves. And then let’s see how we convert the map to JSON in the below example:

And now let’s see the console’s output:

1.5. Customize Jackson

1.5.1. Change Date Time Format With Jackson

When we convert a Java object to JSON with Jackson, there are some situations that we want to change the date time format of the output JSON. Before doing that, let’s see how a Date or LocalDate object in Java is serialized to JSON by default in the following example:

The JSON with pretty print is:

We can see that the field “pubDate” was printed very detail while to the field “copyrightDate” was converted to milliseconds before printing out.

Let’s customize Jackson so that we can have better formats of those date fields.

1.5.2. Change Date Format With Jackson

Let’s change the date format with Jackson by providing the ObjectMapper class a SimpleDateFormat instance as follow:

We have created an instance of SimpleDateFormat class with a pattern: dd-MM-yyyy hh:mm:ss which should be used by Jackson when it serializes the copyrightDate into String.

Let’s see the output JSON:

We can see that the copyrightDate field now has value “27-08-2017 01:21:25” instead of milliseconds.

1.5.3. Change LocalDate Format With Jackson

Setting SimplDateFormat works for java.util.Date class only. And we will need another way to change the LocalDate format with Jackson. Firstly, let’s add the following dependency into pom.xml file:

Secondly, we will need to register capability of serializing java.time objects with the Jackson core:

Lastly, let’s specify our desired format of the LocalDate field in the POJO class by annotating it with the @JsonFormat annotation :

Let’s see the output on the console:

We can see that the pubDate now has format dd/MM/yyyy.

2. Convert A Java Object To Json With Gson

2.1. Gson Library Dependency

The only dependency of Gson library we need for all blow examples is:

2.2. Convert A Java Object To Json With Gson

Firstly, let’s convert a Java object to Json using Gson:

We have created a Gson object by using GsonBuilder class so that we can configure Gson to output Json that fits in a page for pretty printing. If we don’t need such pretty printing, we can create a Gson object simply as follows:

The JSON string output is:

2.3. Convert A Java List To Json With Gson

Next, let’s convert a java.util.List to Json with Gson:

Let’s see the JSON output of the list is:

2.4. Convert A Java Set To Json with Gson

Let’s get to an example of converting a java.util.Set to Json with Gson:

And see the output of the Set in Json:

2.5. Convert a Java Map to Json with Gson

And now, let’s convert a java.util.Map to Json with Gson:

The JSON output of the Map is:

3. Conclusion

The tutorial has illustrated how to convert a Java object to JSON with Jackson and Gson. Below are other related tutorials for your references:

JsonObject Example – Getting Started with Java API for JSON Processing

Ignore Unknown Properties or New Fields in Jackson

Ignore or Exclude Field in Gson

Gson Example with Maven and Gradle

 

 

0 0 vote
Article Rating