Home United States USA — software Jackson Annotations for JSON (Part 2): Serialization

Jackson Annotations for JSON (Part 2): Serialization

314
0
SHARE

See which Jackson annotations are useful for your POJOs to serialize Java objects to JSON as well as accompanying code snippets to test your annotations.
Jackson is a suite of data-processing tools for Java comprising of three components:
Streaming (jackson-core) defines low-level streaming APIs and includes JSON-specific implementations.
Annotations (jackson-annotations) contains standard Jackson annotations.
Databind (jackson-databind) implements data-binding (and object serialization) support on the streaming package. This package depends on both the streaming and annotations packages.
In this series of articles, I will explain data binding Java objects to JSON using Jackson annotations. I will take up each of the Jackson annotations and explain, with code snippets, how to use them. Each annotation usage is accompanied with proper test cases.
If you want to catch up on what’s happened so far, read:
Part 1: Serialization and Deserialization
Jackson provides several annotations that you can use in POJOs to serialize Java objects to JSON. These annotations are:
The @JsonValue annotation is used at the method level. This annotation tells Jackson to use this method to generate the JSON string from the Java object.
Typically, if you want to print a serialized object, you override the toString() method. But, by using the@JsonValue annotation, you can define the way in which the Java object is to be serialized.
Note: Jackson omits any quotation marks inside the String returned by the custom serializer. Let us consider an example Java class that uses the @JsonValue annotation.
In order to explain the difference between the serialized object with and without the @JsonValue annotation, the code includes the toString() method. You can also run the code without overriding the toString() method.
The test code to test the @JsonValue annotation is:
The output of running the test in IntelliJ is:
As shown in the preceding figure, the Java object is serialized by Jackson by calling the defined methodtoJson(). The quotation marks are added by Jackson.
The @JsonInclude annotation is used to exclude properties or fields of a class under certain conditions. This is defined using the JsonInclude. Include enum. This enum contains constants that determine whether or not to exclude the property. The constants are:
Let us consider an example Java class that uses the @JsonInclude annotation:
The test code to test the @JsonInclude annotation is:
The output of running the test in IntelliJ is:
As shown in the preceding figure, the JSON string does not contain the property name, as it is initialized to null.
The @JsonGetter annotation is used to customize the generated JSON keys. This is accomplished with the value argument of @JsonGetter. The value passed is the name that should be used as the JSON key.
Let us consider an example Java class that uses the @JsonGetter annotation.
The test code to test the @JsonGetter annotation is:
The output of running the test in IntelliJ is:
As you can see in the example, the Java object is serialized with the property names that you defined using the@JsonGetter annotation. Without the annotations, the serialized JSON would contain the property names: personId and personName.
The @JsonAnyGetter annotation can be used when you don’t want to declare a property or a method for every possible key in JSON. This annotation is used on the getter methods which enables you to use a Map to hold all your properties that you want to serialize.
Let us consider an example Java class that uses the @JsonAnyGetter annotation.
The test code to test the @JsonAnyGetter annotation is:
The output of running the test in IntelliJ is:
As you can see, all the properties are serialized as the properties of the AnyGetterDemoBean object.
The @JsonPropertyOrder annotation tells Jackson to serialize the Java object to JSON in the order specified as the arguments of the annotation. This annotation also allows partial ordering. The properties are first serialized in the order in which they are found, followed by any other properties not included in the annotation.
Let us consider an example of Java class that uses the @JsonPropertyOrder annotation.
The test code to test the @JsonPropertyOrder annotation is:
The output of running the test in IntelliJ is:
As you can see, the name property is first serialized before the personId. Without the@JsonPropertyOrder annotation, the object would have been serialized in the order found in the class.
The @JsonRawValue annotation is used on methods and fields. It tells Jackson to serialize the field or property as declared. For example, if you have a String field in your Java class, the JSON value that Jackson generates is enclosed within quotes (” “). But when you annotate the field with @JsonRawValue, Jackson omits the quotes.
Let us consider an example Java class that explains the use of @JsonRawValue.
Here, the address field is a JSON string. This JSON string will be serialized as a part of the final JSON string of the RawValueDemoBean object.
The test code to test the @JsonRawValue annotation is:
The output of running the test in IntelliJ is:
As you can see, the final JSON string of the Java object is generated as defined in the POJO class omitting the quotes.
The @JsonSerialize annotation is used tell Jackson to use the declared custom serializer during the serialization of the field, which is marked with this annotation. Let us consider a POJO that uses the @JsonSerializeannotation.
Next, let us define a custom serializer that will serialize the activeDate field with a specific format.
The code to test the @JsonSerialize annotation is:
The output of running the test in IntelliJ is:
The @JsonRootName annotation can be used to tell Jackson to wrap the object to be serialized with a top-level element. You can pass the name as a parameter to the @JsonRootName annotation. Let us consider that you want to wrap your serialized Java object with the key user.

Continue reading...