javajsonjacksonjackson2

What is the difference between ObjectNode and JsonNode in Jackson?


According to the documetation of JsonNode:

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?


Solution

  • JsonNode is a base class that ObjectNode and ArrayNode extend. JsonNode represents any valid Json structure whereas 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.