Convert JsonNode to POJO

This may seem a little unusual, but I'm looking for an efficient way to convert / display a JsonNode to POJO .

I store some data of my model in json files, and I have to support a couple of versions of my model.

I load the json file into memory in JsonNode, apply a couple of version control strategies so that they match the latest version of my model.

  ObjectMapper mapper = new ObjectMapper(); BufferedReader fileReader = new BufferedReader(new FileReader(projPath)); JsonNode rootNode = mapper.readTree(fileReader); //Upgrade our file in memory applyVersioningStrategy(rootNode); ProjectModel project = mapJsonNodeToProject(rootNode); 

If there is no faster way to do this, I probably ended up just manually applying JsonNodes to my model

+71
java json jackson
Oct 31 '13 at 16:18
source share
4 answers

In Jackson 2.4, you can convert the following:

 MyClass newJsonNode = jsonObjectMapper.treeToValue(someJsonNode, MyClass.class); 

where jsonObjectMapper is Jackson's ObjectMapper .




In older versions of Jackson, this would be

 MyClass newJsonNode = jsonObjectMapper.readValue(someJsonNode, MyClass.class); 
+141
Feb 25 '15 at 8:39
source share
β€” -

This should do the trick:

 mapper.readValue(fileReader, MyClass.class); 

I say because I use this with String and not with BufferedReader , but it should work anyway.

Here is my code:

 String inputString = // I grab my string here MySessionClass sessionObject; try { ObjectMapper objectMapper = new ObjectMapper(); sessionObject = objectMapper.readValue(inputString, MySessionClass.class); 

Here is the official documentation for this call: http://jackson.codehaus.org/1.7.9/javadoc/org/codehaus/jackson/map/ObjectMapper.html # readValue (java.lang.String, java.lang.Class)

You can also define a custom deserializer when instantiating an ObjectMapper : http://wiki.fasterxml.com/JacksonHowToCustomDeserializers

Edit: I just remembered something else. If your object has more properties than the POJO value and you just want to ignore the extra functions, you want to set this:

  objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); 

Or you will receive an error message that it cannot find the property that needs to be set.

+8
Oct 31 '13 at 16:24
source share

If you use org.codehaus.jackson, this has been possible since 1.6. You can convert JsonNode to POJO using ObjectMapper#readValue : http://jackson.codehaus.org/1.9.4/javadoc/org/codehaus/jackson/map/ObjectMapper.html#readValue(org.codehaus.jackson.JsonNode , java.lang.Class)

 ObjectMapper mapper = new ObjectMapper(); JsonParser jsonParser = mapper.getJsonFactory().createJsonParser("{\"foo\":\"bar\"}"); JsonNode tree = jsonParser.readValueAsTree(); // Do stuff to the tree mapper.readValue(tree, Foo.class); 
+4
Jan 06 '15 at 2:18
source share
 String jsonInput = "{ \"hi\": \"Assume this is the JSON\"} "; com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); MyClass myObject = objectMapper.readValue(jsonInput, MyClass.class); 

If your JSON input has more properties than your POJO, and you just want to ignore the extra features in Jackson 2.4, you can configure your ObjectMapper as follows. This syntax is different from older versions of Jackson. (If you use the wrong syntax, it will silently do nothing.)

 mapper.disable(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKβ€Œβ€‹NOWN_PROPERTIES); 
+3
Oct. 14 '14 at 10:17
source share



All Articles