Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson JSON difference between JsonNode and ObjectNode

Tags:

jackson

I am using Jackson for JSON parsing. What is the difference between JsonNode and ObjectNode?

And which to use for mapping JSON in string format.

like image 975
unnik Avatar asked Aug 04 '16 06:08

unnik


1 Answers

Quick answer

  • JsonNode: Abstract class, used when reading a JSON document.
  • ObjectNode: Concrete implementation, used when building or modifying a JSON document.

Keep reading for a more detailed answer.

JsonNode

JsonNode is an abstract class used as the base class for all JSON nodes, which form the basis of JSON Tree Model that Jackson implements.

Quoting the JsonNode documentation:

As a general design rule, most accessors (getters) methods are included in this base class, to allow for traversing structure without type casts.

Mutators methods (setters), 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).

The JsonNode concrete implementations can be found in the com.fasterxml.jackson.databind.node package.

ObjectNode

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. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

JSON object

like image 85
cassiomolin Avatar answered Nov 17 '22 02:11

cassiomolin