Package io.micronaut.json.tree
Class JsonNode
- java.lang.Object
-
- io.micronaut.json.tree.JsonNode
-
- Direct Known Subclasses:
JsonArray,JsonObject
public abstract class JsonNode extends java.lang.ObjectImmutable class representing a json node. Json nodes can be either scalar (string, number, boolean, null) or containers (object, array).- Since:
- 3.1
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description java.lang.StringcoerceStringValue()Attempt to coerce this node to a string.static JsonNodecreateArrayNode(java.util.List<JsonNode> nodes)static JsonNodecreateBooleanNode(boolean value)static JsonNodecreateNumberNode(double value)static JsonNodecreateNumberNode(float value)static JsonNodecreateNumberNode(int value)static JsonNodecreateNumberNode(long value)static JsonNodecreateNumberNode(java.math.BigDecimal value)static JsonNodecreateNumberNode(java.math.BigInteger value)static JsonNodecreateNumberNodeImpl(java.lang.Number value)Hidden, so that we don't have to check that the number type is supported.static JsonNodecreateObjectNode(java.util.Map<java.lang.String,JsonNode> nodes)static JsonNodecreateStringNode(java.lang.String value)abstract java.lang.Iterable<java.util.Map.Entry<java.lang.String,JsonNode>>entries()abstract JsonNodeget(int index)abstract JsonNodeget(java.lang.String fieldName)java.math.BigDecimalgetBigDecimalValue()java.math.BigIntegergetBigIntegerValue()booleangetBooleanValue()doublegetDoubleValue()floatgetFloatValue()intgetIntValue()longgetLongValue()java.lang.NumbergetNumberValue()java.lang.StringgetStringValue()booleanisArray()booleanisBoolean()booleanisContainerNode()booleanisNull()booleanisNumber()booleanisObject()booleanisString()booleanisValueNode()static JsonNodenullNode()abstract intsize()abstract java.lang.Iterable<JsonNode>values()
-
-
-
Method Detail
-
nullNode
@NonNull public static JsonNode nullNode()
- Returns:
- The singleton node representing
null.
-
createArrayNode
@NonNull public static JsonNode createArrayNode(@NonNull java.util.List<JsonNode> nodes)
- Parameters:
nodes- The nodes in this array. Must not be modified after this method is called.- Returns:
- The immutable array node.
-
createObjectNode
@NonNull public static JsonNode createObjectNode(java.util.Map<java.lang.String,JsonNode> nodes)
- Parameters:
nodes- The nodes in this object. Must not be modified after this method is called.- Returns:
- The immutable array node.
-
createBooleanNode
@NonNull public static JsonNode createBooleanNode(boolean value)
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given boolean value.
-
createStringNode
@NonNull public static JsonNode createStringNode(@NonNull java.lang.String value)
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given string value.
-
createNumberNodeImpl
@Internal public static JsonNode createNumberNodeImpl(java.lang.Number value)
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
@NonNull public static JsonNode createNumberNode(int value)
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given numeric value.
-
createNumberNode
@NonNull public static JsonNode createNumberNode(long value)
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given numeric value.
-
createNumberNode
@NonNull public static JsonNode createNumberNode(@NonNull java.math.BigDecimal value)
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given numeric value.
-
createNumberNode
@NonNull public static JsonNode createNumberNode(float value)
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given numeric value.
-
createNumberNode
@NonNull public static JsonNode createNumberNode(double value)
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given numeric value.
-
createNumberNode
@NonNull public static JsonNode createNumberNode(@NonNull java.math.BigInteger value)
- Parameters:
value- The value of the node.- Returns:
- A json node representing the given numeric value.
-
isNumber
public boolean isNumber()
- Returns:
trueiff this is a number node.
-
getNumberValue
@NonNull public java.lang.Number getNumberValue()
- Returns:
- The raw numeric value of this node. Always full precision.
- Throws:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.IllegalStateException- if this is not a number node.
-
getBigIntegerValue
@NonNull public final java.math.BigInteger getBigIntegerValue()
- Returns:
- The value of this number node, converted to
BigInteger. May lose the decimal part. - Throws:
java.lang.IllegalStateException- if this is not a number node.
-
getBigDecimalValue
@NonNull public final java.math.BigDecimal getBigDecimalValue()
- Returns:
- The value of this number node, converted to
BigDecimal. - Throws:
java.lang.IllegalStateException- if this is not a number node.
-
isString
public boolean isString()
- Returns:
trueiff this is a string node.
-
getStringValue
@NonNull public java.lang.String getStringValue()
- Returns:
- The value of this string node.
- Throws:
java.lang.IllegalStateException- if this is not a string node.
-
coerceStringValue
@NonNull public java.lang.String coerceStringValue()
Attempt to coerce this node to a string.- Returns:
- The coerced string value.
- Throws:
java.lang.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:
java.lang.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
@NonNull public abstract java.lang.Iterable<JsonNode> values()
- Returns:
- An
Iterableof all values of this array or object node. - Throws:
java.lang.IllegalStateException- if this is not a container node.
-
entries
@NonNull public abstract java.lang.Iterable<java.util.Map.Entry<java.lang.String,JsonNode>> entries()
- Returns:
- An
Iterableof all entries of this object node. - Throws:
java.lang.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
@Nullable public abstract JsonNode get(@NonNull java.lang.String fieldName)
- 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
@Nullable public abstract JsonNode get(int index)
- 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.
-
-