Interface JsonNode
- All Known Subinterfaces:
JsonArrayNode
,JsonBooleanNode
,JsonNullNode
,JsonNumberNode
,JsonObjectNode
,JsonStringNode
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 beJsonArrayNode
. JsonObjectNode objectNode = jsonNode.asObject(); // Throws IllegalStateException, as the JSON string was parsed as aJsonArrayNode
. 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 Summary
Modifier and TypeMethodDescriptionasArray()
Attempts to return thisJsonNode
as a list of nodes.Attempts to return thisJsonNode
as a boolean.asDouble()
Attempts to return thisJsonNode
as a double.asLong()
Attempts to return thisJsonNode
as a long.asNumber()
Attempts to return thisJsonNode
as a number.asObject()
Attempts to return thisJsonNode
as an object.asString()
Attempts to return thisJsonNode
as a string.getValue()
Retrieves the value for thisJsonNode
.boolean
isArray()
Checks if thisJsonNode
is an array.boolean
Checks if thisJsonNode
is a boolean.boolean
isNull()
Checks if thisJsonNode
is null.boolean
isNumber()
Checks if thisJsonNode
is a number.boolean
isObject()
Checks if thisJsonNode
is an object.boolean
isString()
Checks if thisJsonNode
is a string.static JsonNode
Creates a new instance ofJsonNode
from the supplied json string.Returns thisJsonNode
as its string representation.
-
Method Details
-
getValue
-
toJsonString
-
isArray
boolean isArray()Checks if thisJsonNode
is an array.- Returns:
- True if this JsonNode represents a JSON array.
-
isObject
boolean isObject()Checks if thisJsonNode
is an object.- Returns:
- True if this JsonNode represents a JSON object.
-
isString
boolean isString()Checks if thisJsonNode
is a string.- Returns:
- True if this JsonNode represents a JSON string.
-
isNumber
boolean isNumber()Checks if thisJsonNode
is a number.- Returns:
- True if this JsonNode represents a JSON number.
-
isBoolean
boolean isBoolean()Checks if thisJsonNode
is a boolean.- Returns:
- True if this JsonNode represents a JSON boolean.
-
isNull
boolean isNull()Checks if thisJsonNode
is null.- Returns:
- True if this JsonNode represents a null value.
-
asBoolean
Boolean asBoolean()Attempts to return thisJsonNode
as a boolean.- Throws:
IllegalStateException
- If this JsonNode is not a boolean type.
-
asString
String asString()Attempts to return thisJsonNode
as a string.- Throws:
IllegalStateException
- If this JsonNode is not a string type.
-
asNumber
Number asNumber()Attempts to return thisJsonNode
as a number.- Throws:
IllegalStateException
- If this JsonNode is not a number type.
-
asLong
Long asLong()Attempts to return thisJsonNode
as a long.- Throws:
IllegalStateException
- If this JsonNode is not a number type.
-
asDouble
Double asDouble()Attempts to return thisJsonNode
as a double.- Throws:
IllegalStateException
- If this JsonNode is not a number type.
-
asArray
JsonArrayNode asArray()Attempts to return thisJsonNode
as a list of nodes.- Throws:
IllegalStateException
- If this JsonNode is not an array type.
-
asObject
JsonObjectNode asObject()Attempts to return thisJsonNode
as an object.- Throws:
IllegalStateException
- If this JsonNode is not an object type.
-
jsonNode
Creates a new instance ofJsonNode
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.
-