THIS USER NEEDS HELP THIS USER NEEDS HELP - 1 month ago 19
Java Question

What is the difference between ObjectNode and JsonNode in Jackson?

I am in the process of migrating JSON Objects to Jackson JSON objects, and I was wondering which would fit my project better.

According to the documetation of

:


Most mutators, however, need to be accessed through specific sub-classes (such as
ObjectNode
and ArrayNode).


However I am still confused since some stackoverflow answers seem to use them quite interchangeably. What different purpose do they serve?

Answer

JsonNode is a base class that ObjectNode and ArrayNode extend. JsonNode represents any valid Json structure where as ObjectNode and ArrayNode are particular implementations for objects (aka maps) and arrays, respectively.

ArrayNode has specific methods for dealing with arrays such as get(index i) E.g. you cannot get an item at a specific index in a JsonNode or ObjectNode but you can in an ArrayNode.