Richard Searle

home

Enriching POJO JSON

29 Jun 2014

Jackson provides a simple binding mechanism to create json from a Java POJO. It also provides annotations to control the generated json, but these are not always sufficient or appropriate.

Consider a use case where the POJO does not contain all the data required by the consumer of the json; and the POJO cannot be changed.

A wrapper POJO could be created around the original POJO, but that is clumsy and adds additional structure to the json. A simpler approach would generate a json tree from the POJO and then manipulate the tree.

For example, add the classname and some location metadata:

ObjectNode tree = mapper.valueToTree(data);
tree.put("type", data.getClass().getCanonicalName());
tree.put("location", locationLookup.getLocation(data));
String result = tree.toString();