I am developing a REST interface for my app using Jackson to serialize my POJO domain objects to JSON representation. I want to customize the serialization for some types to add additional properties to the JSON representation that do not exist in POJOs (e.g. add some metadata, reference data, etc). I know how to write my own JsonSerializer
, but in that case I would need to explicitly call JsonGenerator.writeXXX(..)
methods for each property of my object while all I need is just to add an additional property. In other words I would like to be able to write something like:
@Override public void serialize(TaxonomyNode value, JsonGenerator jgen, SerializerProvider provider) { jgen.writeStartObject(); jgen.writeAllFields(value); // <-- The method I'd like to have jgen.writeObjectField("my_extra_field", "some data"); jgen.writeEndObject(); }
or (even better) to somehow intercept the serialization before the jgen.writeEndObject()
call, e.g.:
@Override void beforeEndObject(....) { jgen.writeObjectField("my_extra_field", "some data"); }
I thought I could extend BeanSerializer
and override its serialize(..)
method but it's declared final
and also I couldn't find an easy way to create a new instance of BeanSerializer
without providing it with all the type metadata details practically duplicating a good portion of Jackson. So I've given up on doing that.
My question is - how to customize Jackson's serialization to add additional stuff to the JSON output for particular POJOs without introducing too much of the boilerplate code and reusing as much as possible of the default Jackson behaviour.
Converting Java object to JSON In it, create an object of the POJO class, set required values to it using the setter methods. Instantiate the ObjectMapper class. Invoke the writeValueAsString() method by passing the above created POJO object. Retrieve and print the obtained JSON.
Data Binding API is used to convert JSON to and from POJO (Plain Old Java Object) using property accessor or using annotations.
@JsonProperty can change the visibility of logical property using its access element during serialization and deserialization of JSON. @JsonAlias defines one or more alternative names for a property to be accepted during deserialization.
ObjectMapper; ObjectMapper objectMapper = new ObjectMapper(); objectMapper. configure(DeserializationFeature. FAIL_ON_UNKNOWN_PROPERTIES, false); This will now ignore unknown properties for any JSON it's going to parse, You should only use this option if you can't annotate a class with @JsonIgnoreProperties annotation.
Jackson 2.5 introduced the @JsonAppend
annotation, which can be used to add "virtual" properties during serialization. It can be used with the mixin functionality to avoid modifying the original POJO.
The following example adds an ApprovalState
property during serialization:
@JsonAppend( attrs = { @JsonAppend.Attr(value = "ApprovalState") } ) public static class ApprovalMixin {}
Register the mixin with the ObjectMapper
:
mapper.addMixIn(POJO.class, ApprovalMixin.class);
Use an ObjectWriter
to set the attribute during serialization:
ObjectWriter writer = mapper.writerFor(POJO.class) .withAttribute("ApprovalState", "Pending");
Using the writer for serialization will add the ApprovalState
field to the ouput.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With