Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add extra fields to an Object during Jackson's json serialization?

Tags:

I need to add new property to an object, when serializing to JSON. The value for the property is calculated on runtime and does not exist in the object. Also the same object can be used for creation of different JSON with different set ot fields (kind of having a base class with subclasses, but I don't want to create ones just for JSON generation).

What is the best way of doing that, which doesn't involve creation of custom serializer class, which will take care of serializing of whole set of object's fields? Or may be it is possible to inherit some "basic" serializer, and simply take it's output and add new field to it somehow?

I learned about mixins, and looks like it is possible to rename/hide some fields, however it seems not be possible to add an extra one.

like image 817
jdevelop Avatar asked Jan 16 '13 15:01

jdevelop


People also ask

Does Jackson serialize null fields?

With its default settings, Jackson serializes null-valued public fields. In other words, resulting JSON will include null fields. Here, the name field which is null is in the resulting JSON string.

Does Jackson use fields or getters?

Jackson uses the setter and getter methods to auto-detect the private field and updates the field during deserialization. Assume you have defined all the fields or properties in your Java class so that both input JSON and output Java class have identical fields.

What is Jackson object serialization?

Jackson is a solid and mature JSON serialization/deserialization library for Java. The ObjectMapper API provides a straightforward way to parse and generate JSON response objects with a lot of flexibility. This article discussed the main features that make the library so popular.

How do you serialize an object to JSON Jackson in Java?

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.


2 Answers

Can you not just add a method in value class? Note that it does not have to be either public, or use getter naming convention; you could do something like:

public class MyStuff {
   // ... the usual fields, getters and/or setters

   @JsonProperty("sum") // or whatever name you need in JSON
   private int calculateSumForJSON() {
        return 42; // calculate somehow
   }
}

Otherwise you could convert POJO into JSON Tree value:

JsonNode tree = mapper.valueToTree(value);

and then modify it by adding properties etc.

like image 189
StaxMan Avatar answered Oct 24 '22 07:10

StaxMan


One option is to add a field for this property and set it on the object before writing to JSON. A second option, if the property can be computed from other object properties you could just add a getter for it, for example:

public String getFullName() {
  return getFirstName() + " " + getLastName();
}

And even though there's no matching field Jackson will automatically call this getter while writing the JSON and it will appear as fullName in the JSON output. If that won't work a third option is to convert the object to a map and then manipulate it however you need:

ObjectMapper mapper //.....
MyObject o //.....
long specialValue //.....
Map<String, Object> map = mapper.convertValue(o, new TypeReference<Map<String, Object>>() { });
map.put("specialValue", specialValue);

You're question didn't mention unmarshalling but if you need to do that as well then the first option would work fine but the second two would need some tweaking.

And as for writing different fields of the same object it sounds like a job for @JsonView

like image 37
HiJon89 Avatar answered Oct 24 '22 07:10

HiJon89