This quick tutorial is going to how to get JSON pretty print output with Gson and Jackson.

1. Get JSON Pretty Print Output with Gson

To get JSON pretty print output with Gson, we just need to set the prettyPrinting property of the Gson instance with true value, for example:

On the above example, the gson instance was created from a GsonBuilder on which we enable JSON pretty print output by calling the setPrettyPrinting method.

Running the test will output JSON that fits in a page for pretty printing as follows:

2. Get JSON Pretty Print Output with Jackson

To get JSON pretty print output with Jackson, we will need to call the writerWithDefaultPrettyPrinter of the ObjectMapper class which will construct the ObjectWriter that will serialize objects using the default pretty printer for indentation, for example:

The output will be:

3. References

ObjectMapper

ObjectWriter

GsonBuilder

Below are other related tutorials:

Convert A Java Object To JSON

Ignore or Exclude Field in Gson

Ignore Unknown Properties or New Fields in Jackson

 

0 0 vote
Article Rating