Class JsonNode

  • Direct Known Subclasses:
    JsonArray, JsonObject

    public abstract class JsonNode
    extends java.lang.Object
    Immutable class representing a json node. Json nodes can be either scalar (string, number, boolean, null) or containers (object, array).
    Since:
    3.1
    • 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:
        true iff 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:
        true iff 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:
        true iff 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:
        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 java.lang.Iterable<JsonNode> values()
        Returns:
        An Iterable of 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 Iterable of all entries of this object node.
        Throws:
        java.lang.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 JsonNode get​(@NonNull
                                     java.lang.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 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.