Class JsonNode

java.lang.Object
io.micronaut.json.tree.JsonNode
Direct Known Subclasses:
JsonArray, JsonObject

public abstract class JsonNode extends Object
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 Details

    • from

      @NonNull public static @NonNull JsonNode from(Object value)
      Create a new JsonNode representing this value.
      Parameters:
      value - to be converted to JsonNode
      Returns:
      The JsonNode representing this value
      Since:
      4.0.0
    • nullNode

      @NonNull public static @NonNull JsonNode nullNode()
      Returns:
      The singleton node representing null.
    • createArrayNode

      @NonNull public static @NonNull JsonNode createArrayNode(@NonNull @NonNull 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 @NonNull JsonNode createObjectNode(Map<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 @NonNull JsonNode createBooleanNode(boolean value)
      Parameters:
      value - The value of the node.
      Returns:
      A json node representing the given boolean value.
    • createStringNode

      @NonNull public static @NonNull JsonNode createStringNode(@NonNull @NonNull String value)
      Parameters:
      value - The value of the node.
      Returns:
      A json node representing the given string value.
    • createNumberNodeImpl

      @Internal public static JsonNode createNumberNodeImpl(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 @NonNull JsonNode createNumberNode(int value)
      Parameters:
      value - The value of the node.
      Returns:
      A json node representing the given numeric value.
    • createNumberNode

      @NonNull public static @NonNull JsonNode createNumberNode(long value)
      Parameters:
      value - The value of the node.
      Returns:
      A json node representing the given numeric value.
    • createNumberNode

      @NonNull public static @NonNull JsonNode createNumberNode(@NonNull @NonNull BigDecimal value)
      Parameters:
      value - The value of the node.
      Returns:
      A json node representing the given numeric value.
    • createNumberNode

      @NonNull public static @NonNull JsonNode createNumberNode(float value)
      Parameters:
      value - The value of the node.
      Returns:
      A json node representing the given numeric value.
    • createNumberNode

      @NonNull public static @NonNull JsonNode createNumberNode(double value)
      Parameters:
      value - The value of the node.
      Returns:
      A json node representing the given numeric value.
    • createNumberNode

      @NonNull public static @NonNull JsonNode createNumberNode(@NonNull @NonNull BigInteger value)
      Parameters:
      value - The value of the node.
      Returns:
      A json node representing the given numeric value.
    • getValue

      @Nullable public abstract @Nullable Object getValue()
      Get the value reprinting this node.
      Returns:
      The value of the node
      Since:
      4.0.0
    • isNumber

      public boolean isNumber()
      Returns:
      true iff this is a number node.
    • getNumberValue

      @NonNull public @NonNull Number 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

      @NonNull public final @NonNull BigInteger 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

      @NonNull public final @NonNull BigDecimal 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:
      true iff this is a string node.
    • getStringValue

      @NonNull public @NonNull String getStringValue()
      Returns:
      The value of this string node.
      Throws:
      IllegalStateException - if this is not a string node.
    • coerceStringValue

      @NonNull public @NonNull String 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:
      true iff 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:
      true iff this is the null node.
    • size

      public abstract int size()
      Returns:
      The number of immediate children of this node, or 0 if this is not a container node.
    • values

      @NonNull public abstract @NonNull Iterable<JsonNode> values()
      Returns:
      An Iterable of all values of this array or object node.
      Throws:
      IllegalStateException - if this is not a container node.
    • entries

      @NonNull public abstract @NonNull Iterable<Map.Entry<String,JsonNode>> entries()
      Returns:
      An Iterable of all entries of this object node.
      Throws:
      IllegalStateException - if this is not an object node.
    • isValueNode

      public boolean isValueNode()
      Returns:
      true iff this node is a value node (string, number, boolean, null).
    • isContainerNode

      public boolean isContainerNode()
      Returns:
      true iff this node is a container node (array or object).
    • isArray

      public boolean isArray()
      Returns:
      true iff this node is an array node.
    • isObject

      public boolean isObject()
      Returns:
      true iff this node is an object node.
    • get

      @Nullable public abstract @Nullable JsonNode get(@NonNull @NonNull String fieldName)
      Parameters:
      fieldName - The field name.
      Returns:
      The field with the given name, or null if there is no such field or this is not an object.
    • get

      @Nullable public abstract @Nullable JsonNode get(int index)
      Parameters:
      index - The index into this array.
      Returns:
      The field at the given index, or null if there is no such field or this is not an array.