I've got a JsonNode that is provided by an external library. I need to convert this JsonNode into it's POJO representation.
I've seen methods like this:
mapper.readValue(jsonNode.traverse(), MyPojo.class);
But I'm not very happy with this sollution. traverse() will actually convert my JsonNode into a String representation before it is deserialized into a POJO. The performance is an issue for me in this case.
Any other way of doing it?
Thanks
ObjectNode is a concrete implementation of JsonNode that maps a JSON object, and a JSON object is defined as following: An object is an unordered set of name/value pairs.
JsonNode represents any valid Json structure whereas ObjectNode and ArrayNode are particular implementations for objects (aka maps) and arrays, respectively.
Write JsonNode to JSONObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = readJsonIntoJsonNode(); String json = objectMapper. writeValueAsString(jsonNode); The readJsonIntoJsonNode() method is just a method I have created which parses a JSON string into a JsonNode - just so we have a JsonNode to write.
Read Object From JSON via URL ObjectMapper objectMapper = new ObjectMapper(); URL url = new URL("file:data/car. json"); Car car = objectMapper. readValue(url, Car. class);
Perhaps you're looking for:
mapper.convertValue(jsonNode, MyPojo.class)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With