By default:
- All properties are serialized because they have no
nullvalues dateOfBirthfield was translated with the default Gson date pattern- Output is not formatted and JSON property names correspond to the Java entities
Using a custom serializer allows us to modify the standard behavior. We can introduce an output formatter with HTML, handle null values, exclude properties from output, or add a new output.
ActorGsonSerializer
In order to exclude the director property, the @Expose annotation is used for properties we want to consider:
public class MovieWithNullValue {
@Expose
private String imdbId;
private String director;
@Expose
private List<ActorGson> actors;
}
Notice that:
- the output is formatted
- some property names are changed and contain HTML
nullvalues are included, and thedirectorfield is omittedDateis now in thedd-MM-yyyyformat- a new property is present –
N° Film - filmography is a formatted property, not the default JSON list
As was the case with the simple serializer:
- the JSON input names must correspond with the Java entity names, or they are set to
null. dateOfBirthfield was translated with the default Gson date pattern, ignoring the time zone.
Using a custom deserializer allows us to modify the standard deserializer behavior.
ActorGsonDeserializer
Some notes of interest:
ObjectMapperis our Jackson serializer/deserializer- The output JSON is not formatted
- By default, Java Date is translated to
longvalue
We can create a Jackson serializer for ActorJackson element generation by extending StdSerializer for our entity. Again note that the entity getters/setters must be public
As was the case with the simple serializer:
- the JSON input names must correspond with the Java entity names, or they are set to
null, dateOfBirthfield was translated with the default Jackson date pattern, ignoring the time zone.
Using a custom deserializer allows us to modify the standard deserializer behavior.
Alternatively, we could have created a custom deserializer for the ActorJackson class, registered this module with our ObjectMapper, and deserialized the date using the @JsonDeserialize annotation on the ActorJackson entity.
The disadvantage of that approach is the need to modify the entity, which may not be ideal for cases when we don't have access to the input entity classes.
Both Gson and Jackson are good options for serializing/deserializing JSON data, simple to use and well documented.
Advantages of Gson:
- Simplicity of
toJson/fromJsonin the simple cases - For deserialization, do not need access to the Java entities
Advantages of Jackson:
- Built into all JAX-RS (Jersey, Apache CXF, RESTEasy, Restlet), and Spring framework
- Extensive annotation support
GsonJacksonTestJacksonGsonTest