Jackson's versioning module adds versioning that satisfies the GSON @Since and @Until super-set.
Let's say you have an annotated GSON model:
public class Car { public String model; public int year; @Until(1) public String new; @Since(2) public boolean used; }
Using a module, you can convert it to the following annotation at the Jackson class level ...
@JsonVersionedModel(currentVersion = '3', toCurrentConverterClass = ToCurrentCarConverter) public class Car { public String model; public int year; public boolean used; }
... and write the current version converter:
public class ToCurrentCarConverter implements VersionedModelConverter { @Override public ObjectNode convert(ObjectNode modelData, String modelVersion, String targetModelVersion, JsonNodeFactory nodeFactory) {
Now configure JackMark ObjectMapper using the module and test it.
ObjectMapper mapper = new ObjectMapper().registerModule(new VersioningModule()); // version 1 JSON -> POJO Car hondaCivic = mapper.readValue( "{\"model\": \"honda:civic\", \"year\": 2016, \"new\": \"true\", \"modelVersion\": \"1\"}", Car.class ) // POJO -> version 2 JSON System.out.println(mapper.writeValueAsString(hondaCivic)) // prints '{"model": "honda:civic", "year": 2016, "used": false, "modelVersion": "2"}'
Disclaimer: I am the author of this module. See the GitHub project page for more information on additional features. I also wrote Spring MVC ResponseBodyAdvise to use this module.
Jon peterson
source share