pmartin8
pmartin8

Reputation: 1584

Convert JsonNode into Object

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

Upvotes: 27

Views: 76017

Answers (1)

dnault
dnault

Reputation: 8909

Perhaps you're looking for:

mapper.convertValue(jsonNode, MyPojo.class)

Javadoc: ObjectMapper.convertValue(Object, Class)

Convenience method for doing two-step conversion from given value, into instance of given value type, by writing value into temporary buffer and reading from the buffer into specified target type. This method is functionally similar to first serializing given value into JSON, and then binding JSON data into value of given type, but should be more efficient since full serialization does not (need to) occur. However, same converters (serializers, deserializers) will be used as for data binding, meaning same object mapper configuration works.

There's more to the Javadoc, but that's the gist of it.

Upvotes: 59

Related Questions