Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson: How to add custom property to the JSON without modifying the POJO

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.

like image 297
Alex Vayda Avatar asked Feb 05 '13 18:02

Alex Vayda


People also ask

How does Jackson convert object to JSON?

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.

What converts JSON to and from POJO using property accessor?

Data Binding API is used to convert JSON to and from POJO (Plain Old Java Object) using property accessor or using annotations.

What is difference between JsonProperty and JsonAlias?

@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.

How do I ignore properties in ObjectMapper?

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.


1 Answers

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.

like image 100
Henrik Aasted Sørensen Avatar answered Sep 21 '22 08:09

Henrik Aasted Sørensen