Jackson Json Annotation Example

This tutorial introduces about JSON Annotations via Jackson JSON Annotation Example. Jackson JSON Annotations are implemented exactly as approach read and write of JSON data-binding. But the different point is properties of POJO are annotated at running time.

Other interesting posts you may like

Table of contents:
1. Project structure
1. JACKSON dependency
2. Create input JSON
3. Create POJO with JSON annotations
4. Create Custom Serializer
5. Create Main

Project structure
We will create project structure like this:

Jackson Json Annotation Example

JACKSON dependency
The jackson-databind provides ObjectMapper API and the jackson-annotations provides JSON annotations.

Create input JSON

We create a file named fruit.json that contains content like this:

Create POJO with JSON annotations
Using JSON annotations is the most important point in the Jackson Json Annotation Example. We create POJO with JSON annotations such as below:

Let’s dig deeper:

1) @JsonProperty : This annotation is used on a property or method. it provide mechanism for Serialization and Deserialization of JSON.

2) @JsonIgnoreProperties : This Class level annotation is used to ignore certain properties to be serialized & deserialized. It means that they will not be mapped to JSON content.

3) @JsonAnySetter, @JsonAnyGetter : Two annotations are applied on Getter/Setter methods working with a Map. @JsonAnySetter will catch JSON value which is not mapped to a property in POJO class and deserialized(stored) into Map.
These annotations works as a Catch-All and are applied on Getters/Setter working with a Map. If there is any JSON value which is not mapped to a property in POJO, then that value can be caught by @JsonAnySetter, and stored (deserialized) into Map. Similarly @JsonAnyGetter will serialized all values which are stored into Map back to JSON.

4) @JsonSerialize : This annotation is used to customize the default serialization (Java to JSON) process. In case the default serialization is not matching your needs and you should customize it for your requirement.

In this example, we only show the popular used annotations. You can refer to all annotations by referring to the JavaDocs

Create Custom Serializer
In this Jackson Json Annotation Example, Instead of showing default format of a property we can declare a Custom Serializer to serialize with desired format. Here, we create Customer Serializer and annotate it for importedDate property.

Create Main
The main class is quite simple. We read from a file and deserialize the input JSON into a POJO object using ObjectMapper readValue. Then we serialize that POJO object using ObjectMapper writeValue and write the JSON data to a file.

Running the above main class, the output file contains content like below(newFruit.json):

That’s it on the tutorial Jackson Json Annotation Example. If you find any error on this tutorial, please leave comment.

Download source code, click link below

Jackson-Json-Annotation-Example.zip

Leave a Comment

*

Please share it if you found this useful
Hide Buttons