Package io.micronaut.json.tree
Class JsonNode
java.lang.Object
io.micronaut.json.tree.JsonNode
- Direct Known Subclasses:
JsonArray,JsonObject
Immutable class representing a json node. Json nodes can be either scalar (string, number, boolean, null) or
containers (object, array).
- Since:
- 3.1
- Author:
- Jonas Konrad
-
Method Summary
Modifier and TypeMethodDescriptionAttempt to coerce this node to a string.createArrayNode(@NonNull List<JsonNode> nodes) createBooleanNode(boolean value) createNumberNode(double value) createNumberNode(float value) createNumberNode(int value) createNumberNode(long value) createNumberNode(@NonNull BigDecimal value) createNumberNode(@NonNull BigInteger value) static JsonNodecreateNumberNodeImpl(Number value) Hidden, so that we don't have to check that the number type is supported.createObjectNode(Map<String, JsonNode> nodes) createStringNode(@NonNull String value) entries()Create a newJsonNoderepresenting this value.get(int index) final @NonNull BigDecimalfinal @NonNull BigIntegerbooleanfinal doublefinal floatfinal intfinal longgetValue()Get the value reprinting this node.booleanisArray()booleanbooleanbooleanisNull()booleanisNumber()booleanisObject()booleanisString()booleannullNode()abstract intsize()values()
-
Method Details
-
from
Create a newJsonNoderepresenting this value. -
nullNode
- Returns:
- The singleton node representing
null.
-
createArrayNode
- Parameters:
nodes- The nodes in this array. Must not be modified after this method is called.- Returns:
- The immutable array node.
-
createObjectNode
- Parameters:
nodes- The nodes in this object. Must not be modified after this method is called.- Returns:
- The immutable array node.
-
createBooleanNode
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given boolean value.
-
createStringNode
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given string value.
-
createNumberNodeImpl
Hidden, so that we don't have to check that the number type is supported.- Parameters:
value- The raw numeric value.- Returns:
- The number node.
-
createNumberNode
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given numeric value.
-
createNumberNode
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given numeric value.
-
createNumberNode
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given numeric value.
-
createNumberNode
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given numeric value.
-
createNumberNode
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given numeric value.
-
createNumberNode
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given numeric value.
-
getValue
Get the value reprinting this node.- Returns:
- The value of the node
- Since:
- 4.0.0
-
isNumber
public boolean isNumber()- Returns:
trueiff this is a number node.
-
getNumberValue
- Returns:
- The raw numeric value of this node. Always full precision.
- Throws:
IllegalStateException- if this is not a number node.
-
getIntValue
public final int getIntValue()- Returns:
- The value of this number node, converted to
int. May lose precision. - Throws:
IllegalStateException- if this is not a number node.
-
getLongValue
public final long getLongValue()- Returns:
- The value of this number node, converted to
long. May lose precision. - Throws:
IllegalStateException- if this is not a number node.
-
getFloatValue
public final float getFloatValue()- Returns:
- The value of this number node, converted to
float. May lose precision. - Throws:
IllegalStateException- if this is not a number node.
-
getDoubleValue
public final double getDoubleValue()- Returns:
- The value of this number node, converted to
double. May lose precision. - Throws:
IllegalStateException- if this is not a number node.
-
getBigIntegerValue
- Returns:
- The value of this number node, converted to
BigInteger. May lose the decimal part. - Throws:
IllegalStateException- if this is not a number node.
-
getBigDecimalValue
- Returns:
- The value of this number node, converted to
BigDecimal. - Throws:
IllegalStateException- if this is not a number node.
-
isString
public boolean isString()- Returns:
trueiff this is a string node.
-
getStringValue
- Returns:
- The value of this string node.
- Throws:
IllegalStateException- if this is not a string node.
-
coerceStringValue
Attempt to coerce this node to a string.- Returns:
- The coerced string value.
- Throws:
IllegalStateException- if this node is not a scalar value
-
isBoolean
public boolean isBoolean()- Returns:
trueiff this is a boolean node.
-
getBooleanValue
public boolean getBooleanValue()- Returns:
- The value of this boolean node.
- Throws:
IllegalStateException- if this is not a boolean node.
-
isNull
public boolean isNull()- Returns:
trueiff this is the null node.
-
size
public abstract int size()- Returns:
- The number of immediate children of this node, or
0if this is not a container node.
-
values
- Returns:
- An
Iterableof all values of this array or object node. - Throws:
IllegalStateException- if this is not a container node.
-
entries
- Returns:
- An
Iterableof all entries of this object node. - Throws:
IllegalStateException- if this is not an object node.
-
isValueNode
public boolean isValueNode()- Returns:
trueiff this node is a value node (string, number, boolean, null).
-
isContainerNode
public boolean isContainerNode()- Returns:
trueiff this node is a container node (array or object).
-
isArray
public boolean isArray()- Returns:
trueiff this node is an array node.
-
isObject
public boolean isObject()- Returns:
trueiff this node is an object node.
-
get
- Parameters:
fieldName- The field name.- Returns:
- The field with the given name, or
nullif there is no such field or this is not an object.
-
get
- Parameters:
index- The index into this array.- Returns:
- The field at the given index, or
nullif there is no such field or this is not an array.
-