Convert Java Object to JsonNode in Jackson

JavaJsonJackson

Java Problem Overview


Is it possible to directly convert a Java Object to an JsonNode-Object?

The only way I found to solve this is to convert the Java Object to String and then to JsonNode:

ObjectMapper mapper = new ObjectMapper(); 
String json = mapper.writeValueAsString(object);
JsonNode jsonNode = mapper.readTree(json);

Java Solutions


Solution 1 - Java

As of Jackson 1.6, you can use:

JsonNode node = mapper.valueToTree(map);

or

JsonNode node = mapper.convertValue(object, JsonNode.class);

Source: [is there a way to serialize pojo's directly to treemodel?][1] [1]: https://stackoverflow.com/questions/6967583/jackson-is-there-a-way-to-serialize-pojos-directly-to-treemodel

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMax SchmidtView Question on Stackoverflow
Solution 1 - JavaMax SchmidtView Answer on Stackoverflow