Jackson Annotations with Examples

Jackson is most widely used JSON parsing library in Java. It is used in most of the frameworks. It has various annotations to serialize and deserialize JSON data. This will help to customize the behaviour. In this article we will explore few important annotations like JsonProperty, JsonFormat, JsonIgnore, JsonIgnoreProperties, JsonPropertyOrder, JsonRawValue, JsonCreator, JsonRootName, JsonValue, JsonAnyGetter, JsonSetter, JsonGetter.
To learn basics of Jackson and how to serialize and deserialize POJO, refer to our previous article Understanding Basics of Jackson JSON Parser
JsonPropery
If we want to change the property name while serializing data then we used use this annotation.
When we serialize instead of name
, full_name
property will be used. To test, use the function test_serialize_person
defined below.
JsonIgnore
Sometimes we want to ignore few properties while serializing data. JsonIgnore annotation helps to ignore the property from being serialized.
To test, use the function test_serialize_person
defined below.
JsonFormat
If we want to change the way the object 's value to be serialized then JsonFormat annotation has to be used. Best example is with Date, if we don't use this annotation, it might serialize to time in milliseconds. Below sample code prints the date in ISO 8601 format.
To test, use the function test_serialize_person
defined below.
Test function
The above function will print the output as below
JsonIgnoreProperties
This is similar to JsonIgnore annotation but it can ignore multiple properties.
When we serialize user, id
and email
field will be ignored and the rest will be serialized.
JsonPropertyOrder
Usually when we serialize, JSON string will be generated using the same order as we listed in the class definition. If we want to change the order then we need to use JsonPropertyOrder annotation.
In the below sample, lastName
will be printed first and followed by name
and age
JsonRawValue
If we already have JSON value and we just want to print the same in the JSON output then we have to use JsonRawValue annotation.
Let's test the function
The above function will print the output as below
JsonAnyGetter
Consider we have a map of properties which we want to include in the root JSON as fields instead of nested object. This map field will not have getter and setter functions defined. Use JsonAnyGetter annotation to output the property values of the map in the root JSON.
Let's test this function.
The above function will print the output as below
JsonRootName
If we want to change the root JSON name to be different then we need to use this annotation. To use this annotation, we need to enable the feature in ObjectMapper instance objectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
JsonValue
If we want to serialize the object using its value perferably toString(), in this case we need to use JsonValue annotation.
JsonCreator
In order to deserialize the JSON to POJO, If we want to use specific creator function instead of standard constructor then JsonCreator annotation has to be used.
Let's test this annotation.
The above function will print the output as below
JsonGetter and JsonSetter
If we want to use different function for property name then we need to JsonGetter and JsonSetter annotation.
Let's test this function and see how it works.
The above function will print the output as below
Source code available in Github
Conclusion
In this article, we explored most frequently used Jackson annotation.
Happy Coding !