Class ObjectNode

All Implemented Interfaces:
Serializable, Iterable<JsonNode>, ObjectTreeNode, TreeNode, JacksonSerializable, JsonNodeCreator

public class ObjectNode extends ContainerNode<ObjectNode> implements ObjectTreeNode, Serializable
Node that maps to JSON Object structures in JSON content.
See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • _at

      protected JsonNode _at(JsonPointer ptr)
      Description copied from class: JsonNode
      Helper method used by other methods for traversing the next step of given path expression, and returning matching value node if any: if no match, null is returned.
      Specified by:
      _at in class JsonNode
      Parameters:
      ptr - Path expression to use
      Returns:
      Either matching JsonNode for the first step of path or null if no match (including case that this node is not a container)
    • _valueDesc

      protected String _valueDesc()
      Description copied from class: BaseJsonNode
      Method for implementation classes to return a short description of contained value, to be used in error messages.
      Specified by:
      _valueDesc in class BaseJsonNode
    • deepCopy

      public ObjectNode deepCopy()
      Description copied from class: JsonNode
      Method that can be called to get a node that is guaranteed not to allow changing of this node through mutators on this node or any of its children. This means it can either make a copy of this node (and all mutable children and grand children nodes), or node itself if it is immutable.

      Note: return type is guaranteed to have same type as the node method is called on; which is why method is declared with local generic type.

      Specified by:
      deepCopy in class JsonNode
      Returns:
      Node that is either a copy of this node (and all non-leaf children); or, for immutable leaf nodes, node itself.
    • withObject

      public ObjectNode withObject(String exprOrProperty)
      Description copied from class: JsonNode
      Method that works in one of possible ways, depending on whether exprOrProperty is a valid JsonPointer expression or not (valid expression is either empty String "" or starts with leading slash / character). If it is, works as a short-cut to:
        withObject(JsonPointer.compile(exprOrProperty));
      
      If it is NOT a valid JsonPointer expression, value is taken as a literal Object property name and calls is alias for
        withObjectProperty(exprOrProperty);
      
      Overrides:
      withObject in class JsonNode
      Parameters:
      exprOrProperty - JsonPointer expression to use (if valid as one), or, if not (no leading "/"), property name to match.
      Returns:
      ObjectNode found or created
    • withObjectProperty

      public ObjectNode withObjectProperty(String propName)
      Description copied from class: JsonNode
      Method similar to JsonNode.withObject(JsonPointer, OverwriteMode, boolean) -- basically short-cut to:
         withObject(JsonPointer.compile("/"+propName), OverwriteMode.NULLS, false);
      
      that is, only matches immediate property on ObjectNode and will either use an existing ObjectNode that is value of the property, or create one if no value or value is NullNode.
      Will fail with an exception if:
      • Node method called on is NOT ObjectNode
      • Property has an existing value that is NOT NullNode (explicit null)
      Overrides:
      withObjectProperty in class JsonNode
      Parameters:
      propName - Name of property that has or will have ObjectNode as value
      Returns:
      ObjectNode value of given property (existing or created)
    • withArray

      public ArrayNode withArray(String exprOrProperty)
      Description copied from class: JsonNode
      Short-cut equivalent to:
        withArray(JsonPointer.compile(expr), overwriteMode, preferIndex);
      
      Overrides:
      withArray in class JsonNode
    • withArrayProperty

      public ArrayNode withArrayProperty(String propName)
      Description copied from class: JsonNode
      Method similar to JsonNode.withArray(JsonPointer, OverwriteMode, boolean) -- basically short-cut to:
         withArray(JsonPointer.compile("/"+propName), OverwriteMode.NULLS, false);
      
      that is, only matches immediate property on ObjectNode and will either use an existing ArrayNode that is value of the property, or create one if no value or value is NullNode.
      Will fail with an exception if:
      • Node method called on is NOT ObjectNode
      • Property has an existing value that is NOT NullNode (explicit null)
      Overrides:
      withArrayProperty in class JsonNode
      Parameters:
      propName - Name of property that has or will have ArrayNode as value
      Returns:
      ArrayNode value of given property (existing or created)
    • _withObject

      protected ObjectNode _withObject(JsonPointer origPtr, JsonPointer currentPtr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex)
      Specified by:
      _withObject in class ContainerNode<ObjectNode>
    • _withArray

      protected ArrayNode _withArray(JsonPointer origPtr, JsonPointer currentPtr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex)
      Overrides:
      _withArray in class BaseJsonNode
    • _withObjectAddTailProperty

      protected ObjectNode _withObjectAddTailProperty(JsonPointer tail, boolean preferIndex)
    • _withArrayAddTailProperty

      protected ArrayNode _withArrayAddTailProperty(JsonPointer tail, boolean preferIndex)
    • isEmpty

      public boolean isEmpty(SerializationContext serializers)
      Description copied from class: JacksonSerializable.Base
      Method that may be called on instance to determine if it is considered "empty" for purposes of serialization filtering or not.
      Overrides:
      isEmpty in class JacksonSerializable.Base
    • getNodeType

      public JsonNodeType getNodeType()
      Description copied from class: JsonNode
      Return the type of this node
      Specified by:
      getNodeType in class JsonNode
      Returns:
      the node type as a JsonNodeType enum value
    • isObject

      public final boolean isObject()
      Specified by:
      isObject in interface TreeNode
      Overrides:
      isObject in class JsonNode
    • asToken

      public JsonToken asToken()
      Description copied from class: BaseJsonNode
      Method that can be used for efficient type detection when using stream abstraction for traversing nodes. Will return the first JsonToken that equivalent stream event would produce (for most nodes there is just one token but for structured/container types multiple)
      Specified by:
      asToken in interface TreeNode
      Specified by:
      asToken in class ContainerNode<ObjectNode>
    • size

      public int size()
      Specified by:
      size in interface TreeNode
      Specified by:
      size in class ContainerNode<ObjectNode>
    • isEmpty

      public boolean isEmpty()
      Description copied from class: JsonNode
      Convenience method that is functionally same as:
          size() == 0
      
      for all node types.
      Overrides:
      isEmpty in class JsonNode
    • get

      public JsonNode get(int index)
      Description copied from class: JsonNode
      Method for accessing value of the specified element of an array node. For other nodes, null is always returned.

      For array nodes, index specifies exact location within array and allows for efficient iteration over child elements (underlying storage is guaranteed to be efficiently indexable, i.e. has random-access to elements). If index is less than 0, or equal-or-greater than node.size(), null is returned; no exception is thrown for any index.

      NOTE: if the element value has been explicitly set as null (which is different from removal!), a NullNode will be returned, not null.

      Specified by:
      get in interface TreeNode
      Specified by:
      get in class ContainerNode<ObjectNode>
      Returns:
      Node that represent value of the specified element, if this node is an array and has specified element. Null otherwise.
    • get

      public JsonNode get(String propertyName)
      Description copied from class: JsonNode
      Method for accessing value of the specified field of an object node. If this node is not an object (or it does not have a value for specified field name), or if there is no field with such name, null is returned.

      NOTE: if the property value has been explicitly set as null (which is different from removal!), a NullNode will be returned, not null.

      Specified by:
      get in interface TreeNode
      Specified by:
      get in class ContainerNode<ObjectNode>
      Returns:
      Node that represent value of the specified field, if this node is an object and has value for the specified field. Null otherwise.
    • optional

      public Optional<JsonNode> optional(String propertyName)
      Description copied from class: JsonNode
      Method for accessing value of the specified field of an object node. If this node is not an object (or it does not have a value for specified field name), or if there is no field with such name, empty Optional is returned.

      NOTE: if the property value has been explicitly set as null (which is different from removal!), an Optional containing NullNode will be returned, not null.

      Overrides:
      optional in class JsonNode
      Returns:
      Optional that may contain value of the specified field, if this node is an object and has value for the specified field. Empty Optional otherwise never null.
    • path

      public JsonNode path(int index)
      Description copied from class: JsonNode
      This method is similar to JsonNode.get(int), except that instead of returning null if no such element exists (due to index being out of range, or this node not being an array), a "missing node" (node that returns true for JsonNode.isMissingNode()) will be returned. This allows for convenient and safe chained access via path calls.
      Specified by:
      path in interface TreeNode
      Specified by:
      path in class JsonNode
    • path

      public JsonNode path(String propertyName)
      Description copied from class: JsonNode
      This method is similar to JsonNode.get(String), except that instead of returning null if no such value exists (due to this node not being an object, or object not having value for the specified field), a "missing node" (node that returns true for JsonNode.isMissingNode()) will be returned. This allows for convenient and safe chained access via path calls.
      Specified by:
      path in interface TreeNode
      Specified by:
      path in class JsonNode
    • required

      public JsonNode required(String propertyName)
      Description copied from class: JsonNode
      Method is functionally equivalent to path(propertyName).required() and can be used to check that this node is an ObjectNode (that is, represents JSON Object value) and has value for specified property with key propertyName (but note that value may be explicit JSON null value). If this node is Object Node and has value for specified property, matching value is returned; otherwise IllegalArgumentException is thrown.
      Overrides:
      required in class BaseJsonNode
      Parameters:
      propertyName - Name of property to access
      Returns:
      Value of the specified property of this Object node
    • values

      public Collection<JsonNode> values()
      Description copied from class: JsonNode
      Method for accessing all value nodes of this Node, iff this node is a JSON Array or Object node. In case of Object node, field names (keys) are not included, only values. For other types of nodes, returns empty iterator.
      Overrides:
      values in class JsonNode
    • valueStream

      public Stream<JsonNode> valueStream()
      Description copied from class: JsonNode
      Returns a stream of all value nodes of this Node, iff this node is an ArrayNode or ObjectNode. In case of Object node, property names (keys) are not included, only values. For other types of nodes, returns empty stream.
      Specified by:
      valueStream in class ContainerNode<ObjectNode>
    • propertyNames

      public Collection<String> propertyNames()
      Specified by:
      propertyNames in interface TreeNode
      Overrides:
      propertyNames in class JsonNode
    • properties

      public Set<Map.Entry<String,JsonNode>> properties()
      Method to use for accessing all properties (with both names and values) of this JSON Object.
      Overrides:
      properties in class JsonNode
      Returns:
      Set of properties, if this node is an ObjectNode (JsonNode.isObject() returns true); empty Set otherwise.
    • propertyStream

      public Stream<Map.Entry<String,JsonNode>> propertyStream()
      Description copied from class: JsonNode
      Returns a stream of all properties (key, value pairs) of this Node, iff this node is an an ObjectNode. For other types of nodes, returns empty stream.
      Overrides:
      propertyStream in class JsonNode
    • forEachEntry

      public void forEachEntry(BiConsumer<? super String,? super JsonNode> action)
      Description copied from class: JsonNode
      If this node is an ObjectNode, performs the given action for each property (key, value pair) until all entries have been processed or the action throws an exception. Exceptions thrown by the action are relayed to the caller. For other node types, no action is performed.

      Actions are performed in the order of properties, same as order returned by method JsonNode.properties(). This is generally the document order of properties in JSON object.

      Overrides:
      forEachEntry in class JsonNode
      Parameters:
      action - Action to perform for each entry
    • equals

      public boolean equals(Comparator<JsonNode> comparator, JsonNode o)
      Description copied from class: JsonNode
      Entry method for invoking customizable comparison, using passed-in Comparator object. Nodes will handle traversal of structured types (arrays, objects), but defer to comparator for scalar value comparisons. If a "natural" Comparator is passed -- one that simply calls equals() on one of arguments, passing the other -- implementation is the same as directly calling equals() on node.

      Default implementation simply delegates to passed in comparator, with this as the first argument, and other as the second argument.

      Overrides:
      equals in class JsonNode
      Parameters:
      comparator - Object called to compare two scalar JsonNode instances, and return either 0 (are equals) or non-zero (not equal)
    • findValue

      public JsonNode findValue(String propertyName)
      Description copied from class: JsonNode
      Method for finding the first JSON Object field with specified name in this node or its child nodes, and returning value it has. If no matching field is found in this node or its descendants, returns null.

      Note that traversal is done in document order (that is, order in which nodes are iterated if using JsonNode.values())

      Specified by:
      findValue in class JsonNode
      Parameters:
      propertyName - Name of field to look for
      Returns:
      Value of first matching node found, if any; null if none
    • findValues

      public List<JsonNode> findValues(String propertyName, List<JsonNode> foundSoFar)
      Specified by:
      findValues in class JsonNode
    • findValuesAsString

      public List<String> findValuesAsString(String propertyName, List<String> foundSoFar)
      Specified by:
      findValuesAsString in class JsonNode
    • findParent

      public ObjectNode findParent(String propertyName)
      Description copied from class: JsonNode
      Method for finding a JSON Object that contains specified field, within this node or its descendants. If no matching field is found in this node or its descendants, returns null.
      Specified by:
      findParent in class JsonNode
      Parameters:
      propertyName - Name of field to look for
      Returns:
      Value of first matching node found, if any; null if none
    • findParents

      public List<JsonNode> findParents(String propertyName, List<JsonNode> foundSoFar)
      Specified by:
      findParents in class JsonNode
    • serialize

      public void serialize(JsonGenerator g, SerializationContext ctxt) throws JacksonException
      Method that can be called to serialize this node and all of its descendants using specified JSON generator.
      Specified by:
      serialize in interface JacksonSerializable
      Specified by:
      serialize in class BaseJsonNode
      Throws:
      JacksonException
    • serializeWithType

      public void serializeWithType(JsonGenerator g, SerializationContext ctxt, TypeSerializer typeSer) throws JacksonException
      Description copied from class: BaseJsonNode
      Type information is needed, even if JsonNode instances are "plain" JSON, since they may be mixed with other types.
      Specified by:
      serializeWithType in interface JacksonSerializable
      Specified by:
      serializeWithType in class BaseJsonNode
      Throws:
      JacksonException
    • serializeFilteredContents

      protected void serializeFilteredContents(JsonGenerator g, SerializationContext ctxt, boolean trimEmptyArray, boolean skipNulls) throws JacksonException
      Throws:
      JacksonException
    • _contentsToSerialize

      protected Map<String,JsonNode> _contentsToSerialize(SerializationContext ctxt)
      Helper method for encapsulating details of accessing child node entries to serialize.
      Since:
      2.16
    • set

      public ObjectNode set(String propertyName, JsonNode value)
      Method that will set specified property, replacing old value, if any. Note that this is identical to replace(String, JsonNode), except for return value.
      Parameters:
      propertyName - Name of property to set
      value - Value to set property to; if null, will be converted to a NullNode first (to remove a property, call remove(java.lang.String) instead)
      Returns:
      This node after adding/replacing property value (to allow chaining)
    • setAll

      public ObjectNode setAll(Map<String,? extends JsonNode> properties)
      Method for adding given properties to this object node, overriding any existing values for those properties.

      NOTE: null keys are not allowed; (null values get converted to a NullNode).

      Parameters:
      properties - Properties to add
      Returns:
      This node after adding/replacing property values (to allow chaining)
    • setAll

      public ObjectNode setAll(ObjectNode other)
      Method for adding all properties of the given Object, overriding any existing values for those properties.
      Parameters:
      other - Object of which properties to add to this object
      Returns:
      This node after addition (to allow chaining)
    • replace

      public JsonNode replace(String propertyName, JsonNode value)
      Method for replacing value of specific property with passed value, and returning previous value (or null if none).
      Parameters:
      propertyName - Property of which value to replace: must not be null
      value - Value to set property to, replacing old value if any
      Returns:
      Old value of the property; null if there was no such property with value
    • without

      public ObjectNode without(String propertyName)
      Method for removing property from this ObjectNode, and returning instance after removal.
      Returns:
      This node after removing property (if any)
    • without

      public ObjectNode without(Collection<String> propertyNames)
      Method for removing specified properties out of this ObjectNode.
      Parameters:
      propertyNames - Names of properties to remove
      Returns:
      This node after removing entries
    • putIfAbsent

      public JsonNode putIfAbsent(String propertyName, JsonNode value)
      Method that will set value of specified property if (and only if) it had no set value previously. Note that explicitly set null is a value. Functionally equivalent to: if (get(propertyName) == null) { set(propertyName, value); return null; } else { return get(propertyName); }
      Parameters:
      propertyName - Name of property to set (must not be null)
      value - Value to set to property (if and only if it had no value previously); if null, will be converted to a NullNode first.
      Returns:
      Old value of the property, if any (in which case value was not changed); null if there was no old value (in which case value is now set)
    • remove

      public JsonNode remove(String propertyName)
      Method for removing a property from this ObjectNode. Will return previous value of the property, if such property existed; null if not.
      Returns:
      Value of specified property, if it existed; null if not
    • remove

      public ObjectNode remove(Collection<String> propertyNames)
      Method for removing specified properties out of this ObjectNode.
      Parameters:
      propertyNames - Names of properties to remove
      Returns:
      This node after removing entries
    • removeAll

      public ObjectNode removeAll()
      Method for removing all properties, such that this ObjectNode will contain no properties after call.
      Specified by:
      removeAll in class ContainerNode<ObjectNode>
      Returns:
      This node after removing all entries
    • removeIf

      public ObjectNode removeIf(Predicate<? super JsonNode> predicate)
      Description copied from class: ContainerNode
      Method for removing matching those children (value) nodes container has that match given predicate.
      Specified by:
      removeIf in class ContainerNode<ObjectNode>
      Parameters:
      predicate - Predicate to use for matching: anything matching will be removed
      Returns:
      Container node itself (to allow method call chaining)
    • retain

      public ObjectNode retain(Collection<String> propertyNames)
      Method for removing all properties out of this ObjectNode except for ones specified in argument.
      Parameters:
      propertyNames - Properties to retain in this ObjectNode
      Returns:
      This node (to allow call chaining)
    • retain

      public ObjectNode retain(String... propertyNames)
      Method for removing all properties out of this ObjectNode except for ones specified in argument.
      Parameters:
      propertyNames - Properties to retain in this ObjectNode
      Returns:
      This node (to allow call chaining)
    • putArray

      public ArrayNode putArray(String propertyName)
      Method that will construct an ArrayNode and add it as a property of this ObjectNode, replacing old value, if any.

      NOTE: Unlike all put(...) methods, return value is NOT this ObjectNode, but the newly created ArrayNode instance.

      Returns:
      Newly constructed ArrayNode (NOT the old value, which could be of any type, nor this node)
    • putObject

      public ObjectNode putObject(String propertyName)
      Method that will construct an ObjectNode and add it as a property of this ObjectNode, replacing old value, if any.

      NOTE: Unlike all put(...) methods, return value is NOT this ObjectNode, but the newly created ObjectNode instance.

      Returns:
      Newly constructed ObjectNode (NOT the old value, which could be of any type, nor this node)
    • putPOJO

      public ObjectNode putPOJO(String propertyName, Object pojo)
      Method for adding an opaque Java value as the value of specified property. Value can be serialized like any other property, as long as Jackson can serialize it. Despite term "POJO" this allows use of about any Java type, including Maps, Collections, as well as Beans (POJOs), primitives/wrappers and even JsonNodes. Method is most commonly useful when composing content to serialize from heterogenous sources.

      NOTE: if using JsonNode.toString() (or JsonNode.toPrettyString() support for serialization may be more limited, compared to serializing node with specifically configured ObjectMapper.

      Parameters:
      propertyName - Name of property to set.
      pojo - Java value to set as the property value
      Returns:
      This ObjectNode (to allow chaining)
    • putRawValue

      public ObjectNode putRawValue(String propertyName, RawValue raw)
    • putNull

      public ObjectNode putNull(String propertyName)
      Method for setting value of a property to explicit null value.
      Parameters:
      propertyName - Name of property to set.
      Returns:
      This ObjectNode (to allow chaining)
    • put

      public ObjectNode put(String propertyName, short v)
      Method for setting value of a property to specified numeric value.
      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, Short v)
      Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.
      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, int v)
      Method for setting value of a property to specified numeric value. The underlying JsonNode that will be added is constructed using JsonNodeFactory.numberNode(int), and may be "smaller" (like ShortNode) in cases where value fits within range of a smaller integral numeric value.
      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, Integer v)
      Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.
      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, long v)
      Method for setting value of a property to specified numeric value. The underlying JsonNode that will be added is constructed using JsonNodeFactory.numberNode(long), and may be "smaller" (like IntNode) in cases where value fits within range of a smaller integral numeric value.
      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, Long v)
      Method for setting value of a property to specified numeric value. The underlying JsonNode that will be added is constructed using JsonNodeFactory.numberNode(Long), and may be "smaller" (like IntNode) in cases where value fits within range of a smaller integral numeric value.

      Note that this is alternative to put(String, long) needed to avoid bumping into NPE issues with auto-unboxing.

      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, float v)
      Method for setting value of a property to specified numeric value.
      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, Float v)
      Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.
      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, double v)
      Method for setting value of a property to specified numeric value.
      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, Double v)
      Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.
      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, BigDecimal v)
      Method for setting value of a property to specified numeric value.
      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, BigInteger v)
      Method for setting value of a property to specified numeric value.
      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, String v)
      Method for setting value of a property to specified String value.
      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, boolean v)
      Method for setting value of a property to specified String value.
      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, Boolean v)
      Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.
      Returns:
      This node (to allow chaining)
    • put

      public ObjectNode put(String propertyName, byte[] v)
      Method for setting value of a property to specified binary value
      Returns:
      This node (to allow chaining)
    • equals

      public boolean equals(Object o)
      Description copied from class: JsonNode
      Equality for node objects is defined as full (deep) value equality. This means that it is possible to compare complete JSON trees for equality by comparing equality of root nodes.

      Note: marked as abstract to ensure all implementation classes define it properly and not rely on definition from Object.

      Specified by:
      equals in class JsonNode
    • _childrenEqual

      protected boolean _childrenEqual(ObjectNode other)
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in class BaseJsonNode
    • _put

      protected ObjectNode _put(String propertyName, JsonNode value)