Part 2 - Simple Serialization into JSON with Jackson

Part 2 - Simple Serialization into 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 Serialize a simple POJO into a JSON using Jackson!

Let's start by serializing a simple POJO object in Java named Tutorial. With serializing I mean transform a Java object into a JSON using Jackson:

The Tutorial class:

Our main goal is to transform/parse/serialize a Tutorial object into a JSON like this:

Great challenge, isn't it?

To do that we should use a class from Jackson called ObjectMapper. As you can guess from the name, this object will map an object in Java to something, in our case to a JSON object.

This class has a method called writeValueAsString() that can receives an object as an argument and generate a JSON object as a String.

Let's see the complete code in a Unit Test with JUnit : )

Pretty simple, right?

Notice that I just printed out the JSON object to have the JSON being validated by you easier than asserting it with the assertEquals() method. In the following examples we're going to print out every result!

When you run the code, the output will be:

Great!

But notice that this JSON could be really huge and see it in just 1 line could be challenging. It's time to see a better way in the next post!

That's it for today folks!

Let's learn about how to Print a pretty JSON in the next part: Part 3 - Printing a Pretty JSON 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: