Interface JsonNode

All Known Subinterfaces:
JsonArrayNode, JsonBooleanNode, JsonNullNode, JsonNumberNode, JsonObjectNode, JsonStringNode

public interface JsonNode

This interface is used to represent a JSON node, and acts as the base for all other JsonNode types (see subinterfaces).

You can create a JsonNode from a raw JSON string using jsonNode(String), which will attempt to parse the JSON into a specific JsonNode type.

To retrieve the created JsonNode as a JsonArray, or a JsonObject, call asArray() or asObject() respectively. This provides additional utility for the specific JSON type.

Note: If you are parsing a JsonObject, you can use JsonObjectNode and its utility methods. Call asObject() on your JsonNode.

Note: Attempting to retrieve the JsonNode as the wrong type will throw an IllegalStateException.

     JsonNode jsonNode = JsonNode.create("[]");       // The string parsed, and the underlying type will be JsonArrayNode.
     JsonObjectNode objectNode = jsonNode.asObject(); // Throws IllegalStateException, as the JSON string was parsed as a JsonArrayNode.
     JsonArrayNode arrayNode = jsonNode.asArray();    // Successful
 

Each specific JsonNode type has a corresponding factory method:

     // Create a JsonStringNode with the value "foo"
     JsonStringNode stringNode = JsonStringNode.jsonStringNode("foo");

     // Create a JsonNumberNode with the value 2.5
     JsonNumberNode numberNode = JsonNumberNode.jsonNumberNode(2.5);

     // Create a JsonArrayNode containing the two above nodes
     JsonArrayNode arrayNode = JsonArrayNode.jsonArrayNode(
         stringNode,
         numberNode
     );
 

Any JsonNode can return its JSON string representation:

     JsonArrayNode arrayNode = JsonArrayNode.jsonArrayNode(
            stringNode,
            numberNode
     );

     String arrayNodeAsJson = arrayNode.toJsonString();

     System.out.println(arrayNodeAsJson);
 

Prints:

     [
       "foo",
       2.5
     ]
 
  • Method Details

    • getValue

      Object getValue()
      Retrieves the value for this JsonNode.
      Returns:
      The value for this JsonNode.
    • toJsonString

      String toJsonString()
      Returns this JsonNode as its string representation.
      Returns:
      The JsonNode in JSON string format.
    • isArray

      boolean isArray()
      Checks if this JsonNode is an array.
      Returns:
      True if this JsonNode represents a JSON array.
    • isObject

      boolean isObject()
      Checks if this JsonNode is an object.
      Returns:
      True if this JsonNode represents a JSON object.
    • isString

      boolean isString()
      Checks if this JsonNode is a string.
      Returns:
      True if this JsonNode represents a JSON string.
    • isNumber

      boolean isNumber()
      Checks if this JsonNode is a number.
      Returns:
      True if this JsonNode represents a JSON number.
    • isBoolean

      boolean isBoolean()
      Checks if this JsonNode is a boolean.
      Returns:
      True if this JsonNode represents a JSON boolean.
    • isNull

      boolean isNull()
      Checks if this JsonNode is null.
      Returns:
      True if this JsonNode represents a null value.
    • asBoolean

      Boolean asBoolean()
      Attempts to return this JsonNode as a boolean.
      Throws:
      IllegalStateException - If this JsonNode is not a boolean type.
    • asString

      String asString()
      Attempts to return this JsonNode as a string.
      Throws:
      IllegalStateException - If this JsonNode is not a string type.
    • asNumber

      Number asNumber()
      Attempts to return this JsonNode as a number.
      Throws:
      IllegalStateException - If this JsonNode is not a number type.
    • asLong

      Long asLong()
      Attempts to return this JsonNode as a long.
      Throws:
      IllegalStateException - If this JsonNode is not a number type.
    • asDouble

      Double asDouble()
      Attempts to return this JsonNode as a double.
      Throws:
      IllegalStateException - If this JsonNode is not a number type.
    • asArray

      JsonArrayNode asArray()
      Attempts to return this JsonNode as a list of nodes.
      Throws:
      IllegalStateException - If this JsonNode is not an array type.
    • asObject

      JsonObjectNode asObject()
      Attempts to return this JsonNode as an object.
      Throws:
      IllegalStateException - If this JsonNode is not an object type.
    • jsonNode

      static JsonNode jsonNode(String json)
      Creates a new instance of JsonNode from the supplied json string.
      Parameters:
      json - The JSON string, which may use single quotes in place of double quotes.
      Returns:
      A new JsonNode instance.
      Throws:
      JsonParseException - If the string is not valid JSON.