org.codehaus.jackson
Class JsonNode

java.lang.Object
  extended by org.codehaus.jackson.JsonNode
All Implemented Interfaces:
Iterable<JsonNode>
Direct Known Subclasses:
BaseJsonNode

public abstract class JsonNode
extends Object
implements Iterable<JsonNode>

Base class for all JSON nodes, which form the basis of JSON Tree Model that Jackson implements. One way to think of these nodes is to considere them similar to DOM nodes in XML DOM trees.

As a general design rule, most accessors ("getters") are included in this base class, to allow for traversing structure without type casts. Most mutators, however, need to be accessed through specific sub-classes. 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).

Actual concrete sub-classes can be found from package org.codehaus.jackson.node.


Constructor Summary
protected JsonNode()
           
 
Method Summary
abstract  JsonToken asToken()
          Method that can be used for efficient type detection when using stream abstraction for traversing nodes.
abstract  boolean equals(Object o)
           Note: marked as abstract to ensure all implementation classes define it properly.
 JsonNode get(int index)
          Method for accessing value of the specified element of an array node.
 JsonNode get(String fieldName)
          Method for accessing value of the specified field of an object node.
 BigInteger getBigIntegerValue()
           
 byte[] getBinaryValue()
          Method to use for accessing binary content of binary nodes (nodes for which isBinary() returns true); or for Text Nodes (ones for which getTextValue() returns non-null value), to read decoded base64 data.
 boolean getBooleanValue()
           
 BigDecimal getDecimalValue()
           
 double getDoubleValue()
           
 Iterator<JsonNode> getElements()
          Method for accessing all value nodes of this Node, iff this node is a Json Array or Object node.
 JsonNode getElementValue(int index)
          Deprecated. Use get(int) instead.
 Iterator<String> getFieldNames()
          Method for accessing names of all fields for this Node, iff this node is a Json Object node.
 JsonNode getFieldValue(String fieldName)
          Deprecated. Use get(String) instead.
 int getIntValue()
           
 long getLongValue()
           
abstract  JsonParser.NumberType getNumberType()
          If this node is a numeric type (as per isNumber()), returns native type that node uses to store the numeric value.
 Number getNumberValue()
          Returns numeric value for this node, if and only if this node is numeric (isNumber() returns true); otherwise returns null
 JsonNode getPath(int index)
          Deprecated. Use path(int) instead
 JsonNode getPath(String fieldName)
          Deprecated. Use path(String) instead
 String getTextValue()
          Method to use for accessing String values.
abstract  String getValueAsText()
          Method that will return valid String representation of the container value, if the node is a value node (method isValueNode() returns true), otherwise null.
 boolean isArray()
           
 boolean isBigDecimal()
           
 boolean isBigInteger()
           
 boolean isBinary()
          Method that can be used to check if this node represents binary data (Base64 encoded).
 boolean isBoolean()
          Method that can be used to check if this node was created from Json boolean value (literals "true" and "false").
 boolean isContainerNode()
          Method that returns true for container nodes: Arrays and Objects.
 boolean isDouble()
           
 boolean isFloatingPointNumber()
           
 boolean isInt()
           
 boolean isIntegralNumber()
           
 boolean isLong()
           
 boolean isMissingNode()
          Method that returns true for "virtual" nodes which represent missing entries constructed by path accessor methods when there is no actual node matching given criteria.
 boolean isNull()
          Method that can be used to check if this node was created from Json liternal null value.
 boolean isNumber()
           
 boolean isObject()
           
 boolean isPojo()
          Method that can be used to check if the node is a wrapper for a POJO ("Plain Old Java Object" aka "bean".
 boolean isTextual()
           
 boolean isValueNode()
          Method that returns true for all value nodes: ones that are not containers, and that do not represent "missing" nodes in the path.
 Iterator<JsonNode> iterator()
          Same as calling getElements(); implemented so that convenience "for-each" loop can be used for looping over elements of Json Array constructs.
abstract  JsonNode path(int index)
          This method is similar to 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 isMissingNode()) will be returned.
abstract  JsonNode path(String fieldName)
          This method is similar to 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 isMissingNode()) will be returned.
 int size()
          Method that returns number of child nodes this node contains: for Array nodes, number of child elements, for Object nodes, number of fields, and for all other nodes 0.
abstract  String toString()
           Note: marked as abstract to ensure all implementation classes define it properly.
abstract  JsonParser traverse()
          Method for constructing a JsonParser instance for iterating over contents of the tree that this node is root of.
abstract  void writeTo(JsonGenerator jg)
          Deprecated. Use methods that are part of JsonGenerator or ObjectMapper instead.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JsonNode

protected JsonNode()
Method Detail

isValueNode

public boolean isValueNode()
Method that returns true for all value nodes: ones that are not containers, and that do not represent "missing" nodes in the path. Such value nodes represent String, Number, Boolean and null values from JSON.

Note: one and only one of methods isValueNode(), isContainerNode() and isMissingNode() ever returns true for any given node.


isContainerNode

public boolean isContainerNode()
Method that returns true for container nodes: Arrays and Objects.

Note: one and only one of methods isValueNode(), isContainerNode() and isMissingNode() ever returns true for any given node.


isMissingNode

public boolean isMissingNode()
Method that returns true for "virtual" nodes which represent missing entries constructed by path accessor methods when there is no actual node matching given criteria.

Note: one and only one of methods isValueNode(), isContainerNode() and isMissingNode() ever returns true for any given node.


isArray

public boolean isArray()
Returns:
True if this node represents Json Array

isObject

public boolean isObject()
Returns:
True if this node represents Json Object

isPojo

public boolean isPojo()
Method that can be used to check if the node is a wrapper for a POJO ("Plain Old Java Object" aka "bean". Returns true only for instances of POJONode.

Returns:
True if this node wraps a POJO

isNumber

public boolean isNumber()
Returns:
True if this node represents a numeric Json value

isIntegralNumber

public boolean isIntegralNumber()
Returns:
True if this node represents an integral (integer) numeric Json value

isFloatingPointNumber

public boolean isFloatingPointNumber()
Returns:
True if this node represents a non-integral numeric Json value

isInt

public boolean isInt()
Returns:
True if this node represents an integral numeric Json value that withs in Java int value space

isLong

public boolean isLong()
Returns:
True if this node represents an integral numeric Json value that fits in Java long value space (but not int value space, i.e. isInt() returns false)

isDouble

public boolean isDouble()

isBigDecimal

public boolean isBigDecimal()

isBigInteger

public boolean isBigInteger()

isTextual

public boolean isTextual()

isBoolean

public boolean isBoolean()
Method that can be used to check if this node was created from Json boolean value (literals "true" and "false").


isNull

public boolean isNull()
Method that can be used to check if this node was created from Json liternal null value.


isBinary

public boolean isBinary()
Method that can be used to check if this node represents binary data (Base64 encoded). Although this will be externally written as Json String value, isTextual() will return false if this method returns true.

Returns:
True if this node represents base64 encoded binary data

asToken

public abstract JsonToken asToken()
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)

Since:
1.3

getNumberType

public abstract JsonParser.NumberType getNumberType()
If this node is a numeric type (as per isNumber()), returns native type that node uses to store the numeric value.


getTextValue

public String getTextValue()
Method to use for accessing String values. Does NOT do any conversions for non-String value nodes; for non-String values (ones for which isTextual() returns false) null will be returned. For String values, null is never returned (but empty Strings may be)

Returns:
Textual value this node contains, iff it is a textual json node (comes from Json String value entry)

getBinaryValue

public byte[] getBinaryValue()
                      throws IOException
Method to use for accessing binary content of binary nodes (nodes for which isBinary() returns true); or for Text Nodes (ones for which getTextValue() returns non-null value), to read decoded base64 data. For other types of nodes, returns null.

Returns:
Binary data this node contains, iff it is a binary node; null otherwise
Throws:
IOException

getBooleanValue

public boolean getBooleanValue()

getNumberValue

public Number getNumberValue()
Returns numeric value for this node, if and only if this node is numeric (isNumber() returns true); otherwise returns null

Returns:
Number value this node contains, if any (null for non-number nodes).

getIntValue

public int getIntValue()

getLongValue

public long getLongValue()

getDoubleValue

public double getDoubleValue()

getDecimalValue

public BigDecimal getDecimalValue()

getBigIntegerValue

public BigInteger getBigIntegerValue()

get

public JsonNode get(int index)
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.

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

Returns:
Node that represent value of the specified field, if this node is an object and has value for the specified field. Null otherwise.

getFieldValue

@Deprecated
public final JsonNode getFieldValue(String fieldName)
Deprecated. Use get(String) instead.

Alias for get(String).


getElementValue

@Deprecated
public final JsonNode getElementValue(int index)
Deprecated. Use get(int) instead.

Alias for get(int).


getValueAsText

public abstract String getValueAsText()
Method that will return valid String representation of the container value, if the node is a value node (method isValueNode() returns true), otherwise null.

Note: to serialize nodes of any type, you should call toString() instead.


size

public int size()
Method that returns number of child nodes this node contains: for Array nodes, number of child elements, for Object nodes, number of fields, and for all other nodes 0.

Returns:
For non-container nodes returns 0; for arrays number of contained elements, and for objects number of fields.

iterator

public final Iterator<JsonNode> iterator()
Same as calling getElements(); implemented so that convenience "for-each" loop can be used for looping over elements of Json Array constructs.

Specified by:
iterator in interface Iterable<JsonNode>

getElements

public Iterator<JsonNode> getElements()
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.


getFieldNames

public Iterator<String> getFieldNames()
Method for accessing names of all fields for this Node, iff this node is a Json Object node.


path

public abstract JsonNode path(String fieldName)
This method is similar to 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 isMissingNode()) will be returned. This allows for convenient and safe chained access via path calls.


getPath

@Deprecated
public final JsonNode getPath(String fieldName)
Deprecated. Use path(String) instead

Alias of path(String).


path

public abstract JsonNode path(int index)
This method is similar to 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 isMissingNode()) will be returned. This allows for convenient and safe chained access via path calls.


getPath

@Deprecated
public final JsonNode getPath(int index)
Deprecated. Use path(int) instead

Alias of path(int).


writeTo

public abstract void writeTo(JsonGenerator jg)
                      throws IOException,
                             JsonGenerationException
Deprecated. Use methods that are part of JsonGenerator or ObjectMapper instead.

Method that can be called to serialize this node and all of its descendants using specified JSON generator.

Throws:
IOException
JsonGenerationException

traverse

public abstract JsonParser traverse()
Method for constructing a JsonParser instance for iterating over contents of the tree that this node is root of. Functionally equivalent to first serializing tree using writeTo(org.codehaus.jackson.JsonGenerator) and then re-parsing but much more efficient.


toString

public abstract String toString()

Note: marked as abstract to ensure all implementation classes define it properly.

Overrides:
toString in class Object

equals

public abstract boolean equals(Object o)

Note: marked as abstract to ensure all implementation classes define it properly.

Overrides:
equals in class Object