Jackson 2 support for version control

Does anyone know if Jackson2 supports version control; something similar to GSON @Since and @Until ?

+7
jackson
source share
2 answers

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) { // model version is an int int version = Integer.parse(modelVersion); // version 1 had a 'new' text field instead of a boolean 'used' field if(version <= 1) modelData.put("used", !Boolean.parseBoolean(modelData.remove("new").asText())); } } 

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.

+5
source share

Not directly. You can use the @JsonView or JSON Filter functions to implement a similar inclusion / exclusion.

+2
source share

All Articles