Part 3 - Printing a Pretty JSON with Jackson

Part 3 - Printing a Pretty JSON with Jackson

Hello!

This is a post from the Jackson Series: Jackson - The Complete Guide in 20 Parts

In the today's post we're going to see how to print out a pretty JSON with Jackson!

As we've seen in the previous post, if we use a basic Jackson serialization, our JSON object will be printed out in just 1 line!

Pretty JSON with ObjectWriter from Jackson

To print a pretty JSON with Jackson we just need to use an object called ObjectWriter from Jackson.

To have this class we should call the method writerWithDefaultPrettyPrinter() from the ObjectMapper class.

The complete code could be:

If you run this code, the output will be:

Cool! Now we have a pretty JSON being printed out \o/

Choosing to Print a Pretty JSON at Runtime

Imagine that you have the situation:

Depending on a condition, you should print a pretty JSON or not, at Runtime!

To reach this goal, we should enable the pretty printer in the ObjectMapper object by using the following code:

The method enable() allows us to enable a few features when generating/parsing a JSON.

The complete code could be:

Just test the boolean variable shouldPrintPretty with true and false to see the result : )

That's it for today folks!

Let's learn how to Serialize Composed Objects in the next part: Part 4 - Serializing Composed Java Objects with Jackson

Follow us to keep up to date! \o/

Alexandre Gama

Alexandre Gama

Hacking Code Founder

Hacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
Jackson JSON Serialization - Java Tutorial - Hacking Code

Part 2 - Simple Serialization into JSON with Jackson

It's time to see the first JSON Serialization with Jackson!

Jackson Pretty JSON Serialization - Java Tutorial - Hacking Code

Part 3 - Printing a Pretty JSON with Jackson

Did your JSON is being printed in a ugly way? Let's see a Pretty JSON Serialization with Jackson

Jackson Composed Objects JSON Serialization - Java Tutorial - Hacking Code

Part 4 - Serializing Composed Java Objects with Jackson

What about serialize a Composed Objects in Java with Jackson?

Jackson Getter JSON Deserialization - Java Tutorial - Hacking Code

Part 5 - Serializing Getter Methods to JSON with Jackson

Let's see hows does getter Serialization with Jackson work!

Jackson Field JSON Serialization - Java Tutorial - Hacking Code

Part 6 - Serializing Java Object into JSON using Fields with Jackson

It's time to serialize Fields to JSON using Jackson

Jackson JSON Ordering Serialization - Java Tutorial - Hacking Code

Part 7 - Ordering Properties in JSON with Jackson Serialization

Did you know that you can Order attributes in JSON with Jackson?

Jackson JSON Raw Content Serialization - Java Tutorial - Hacking Code

Part 8 - Serializing the Raw Content in the JSON with Jackson

Jackson allows us to Serialize that Raw Content of the Java Object!

Jackson Custom Value Serializer - Java Tutorial - Hacking Code

Part 9 - JSON Object with a Custom Value with Jackson

You can serialize the entire object with a custom method with Jackson!

Jackson Root JSON Serialization - Java Tutorial - Hacking Code

Part 10 - JSON Object with a Root with Jackson

Root Serialization is really common in the JSON World. Let's see how Jackson does that!

Jackson Custom JSON Serializer - Java Tutorial - Hacking Code

Part 11 - JSON Custom Serializer with Jackson

Custom Serializer with Jackson is really great! Let's see how to use it!

Jackson JSON Map Serialization - Java Tutorial - Hacking Code

Part 12 - Map into JSON Objects with Jackson

Serializing Map in JSON with Jackson is really easy!

Jackson Ignoring JSON Properties - Java Tutorial - Hacking Code

Part 13 - Ignoring Properties in JSON with Jackson

Let's learn how to configure properties to be ignored in the JSON Serialization with Jackson

Jackson Enum JSON Serialization - Java Tutorial - Hacking Code

Part 14 - Enum Objects into a JSON with Jackson

Did you try to Serialize an Enum to JSON? It's pretty easy with Jackson!

Jackson Deserialization - Java Tutorial - Hacking Code

Part 15 - Jackson Deserialization with ObjectMapper

Is it easy to Deserialize a JSON to a Java Object? Let's see how with Jackson!

Jackson Custom JSON Deserialization - Java Tutorial - Hacking Code

Part 16 - Jackson Deserialization with @JsonSetter

Let's see how Custom JSON Deserialization works with Jackson with @JsonSetter

Jackson Injected JSON Deserialization - Java Tutorial - Hacking Code

Part 17 - Jackson Deserialization with @JacksonInject

Did you know that you can have a value being injected in the generated JSON with Jackson?

Jackson Custom JSON Construction - Java Tutorial - Hacking Code

Part 18 - Jackson Deserialization with @JsonCreator

Jackson Deserialization with @JsonCreator allows us to create a custom constructor

Jackson Map JSON Deserialization - Java Tutorial - Hacking Code

Part 19 - Deserializing Map from a JSON with Jackson

Jackson Map JSON Deserialization to Java Objects can be really easy with Jackson

Jackson Custom JSON Deserialization - Java Tutorial - Hacking Code

Part 20 - Jackson Deserialization with @JsonDeserialize

Jackson Custom JSON Deserialization bring to use power to serialize JSON that doesn't mathc with our expectation

0 Comments

Leave a Reply

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: