78

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?

THIS USER NEEDS HELP
  • 3,136
  • 4
  • 30
  • 55
  • JsonNode is a base class that ObjectNode and ArrayNode extend. E.g. you cannot get an item at a specific index in a JsonNode or ObjectNode but you can in an ArrayNode. – bhspencer Aug 01 '16 at 18:57

3 Answers3

103

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.

MyStackRunnethOver
  • 4,872
  • 2
  • 28
  • 42
bhspencer
  • 13,086
  • 5
  • 35
  • 44
  • 3
    @MyStackRunnethOver I am not sure your edit is a good fit for this answer. It turns a straightforward concise answer into a longer discursive narrative about a possible source of confusion. I do not feel sure that confusion is widespread. I personally have not felt it. Would you be willing put your thoughts on the possible source of confusion into their own answer for this question? – bhspencer Oct 15 '20 at 15:59
  • Yeah absolutely :) – MyStackRunnethOver Oct 15 '20 at 16:59
1

To address OP's point about Stack Overflow answers using ObjectNode and JsonNode interchangeably: a possible source of the confusion you see is that JsonNode implements most of the functionality of both ObjectNode and ArrayNode, returning mostly false or null by default. The sub-types then override their respective methods to work correctly (e.g. indexing for ArrayNode, named access for ObjectNode). The Javadoc says:

As a general design rule, most accessors ("getters") are included in this [JsonNode] base class, to allow for traversing structure without type casts. Most mutators, however, need to be accessed through specific sub-classes (such as [ObjectNode] and [ArrayNode]). This seems sensible because proper type information is generally available when building or modifying trees, but less often when reading a tree (newly built from parsed JSON content)

"[T]o allow for traversing structure without type casts" is key, and is the reason that you can often get away with using JsonNode all the time, because as long as the methods you're calling match the sub-type of the object data actually got parsed into, they work as expected.

MyStackRunnethOver
  • 4,872
  • 2
  • 28
  • 42
0

JsonNode and ObjectNode are both classes in the Jackson library used for working with JSON data in Java.

JsonNode is an abstract class that represents a node in a JSON tree. It can be used to read and navigate through JSON data, but it doesn't allow modifications to the underlying data. Because it's abstract, you can't create a JsonNode object directly. Instead, you can use one of its concrete subclasses, such as ObjectNode, ArrayNode, or ValueNode.

ObjectNode is a subclass of JsonNode that specifically represents a JSON object. It allows you to modify the underlying JSON data by adding, removing, or changing properties. An ObjectNode is created with the JsonNodeFactory class, which provides methods for creating different types of JsonNode objects.

In general, you should use JsonNode when you only need to read JSON data, and don't need to modify it. You might use ObjectNode when you need to modify the data, such as when you're building up a JSON object programmatically.

Aman Jain
  • 2,975
  • 1
  • 20
  • 35