Jackson JSON difference between JsonNode and ObjectNode

前端 未结 1 587
小鲜肉
小鲜肉 2020-12-31 12:48

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

And which to use for mapping JSON in string fo

相关标签:
1条回答
  • 2020-12-31 13:12

    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).

    0 讨论(0)
提交回复
热议问题