Class JsonValue

  • All Implemented Interfaces:
    java.lang.Iterable<JsonValue>

    public class JsonValue
    extends java.lang.Object
    implements java.lang.Iterable<JsonValue>
    Container for a JSON object, array, string, double, long, boolean, or null.

    JsonValue children are a linked list. Iteration of arrays or objects is easily done using an iterator or the next() field, both shown below. This is much more efficient than accessing children by index when there are many children.

     JsonValue map = ...;
     // Allocates an iterator:
     for (JsonValue entry : map)
            System.out.println(entry.name + " = " + entry.asString());
     // No allocation:
     for (JsonValue entry = map.child; entry != null; entry = entry.next)
            System.out.println(entry.name + " = " + entry.asString());
     
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void addChild​(JsonValue value)
      Adds the specified value after the last child.
      void addChild​(java.lang.String name, JsonValue value)
      Sets the name of the specified value and adds it after the last child.
      boolean asBoolean()
      Returns this value as a boolean.
      boolean[] asBooleanArray()
      Returns the children of this value as a newly allocated boolean array.
      byte asByte()
      Returns this value as a byte.
      byte[] asByteArray()
      Returns the children of this value as a newly allocated byte array.
      char asChar()
      Returns this value as a char.
      char[] asCharArray()
      Returns the children of this value as a newly allocated char array.
      double asDouble()
      Returns this value as a double.
      double[] asDoubleArray()
      Returns the children of this value as a newly allocated double array.
      float asFloat()
      Returns this value as a float.
      float[] asFloatArray()
      Returns the children of this value as a newly allocated float array.
      int asInt()
      Returns this value as an int.
      int[] asIntArray()
      Returns the children of this value as a newly allocated int array.
      long asLong()
      Returns this value as a long.
      long[] asLongArray()
      Returns the children of this value as a newly allocated long array.
      short asShort()
      Returns this value as a short.
      short[] asShortArray()
      Returns the children of this value as a newly allocated short array.
      java.lang.String asString()
      Returns this value as a string.
      java.lang.String[] asStringArray()
      Returns the children of this value as a newly allocated String array.
      JsonValue child()
      Returns the first child for this object or array.
      JsonValue get​(int index)
      Returns the child at the specified index.
      JsonValue get​(java.lang.String name)
      Returns the child with the specified name.
      boolean getBoolean​(int index)
      Finds the child with the specified index and returns it as a boolean.
      boolean getBoolean​(java.lang.String name)
      Finds the child with the specified name and returns it as a boolean.
      boolean getBoolean​(java.lang.String name, boolean defaultValue)
      Finds the child with the specified name and returns it as a boolean.
      byte getByte​(int index)
      Finds the child with the specified index and returns it as a byte.
      byte getByte​(java.lang.String name)
      Finds the child with the specified name and returns it as a byte.
      byte getByte​(java.lang.String name, byte defaultValue)
      Finds the child with the specified name and returns it as a byte.
      char getChar​(int index)
      Finds the child with the specified index and returns it as a char.
      char getChar​(java.lang.String name)
      Finds the child with the specified name and returns it as a char.
      char getChar​(java.lang.String name, char defaultValue)
      Finds the child with the specified name and returns it as a char.
      JsonValue getChild​(java.lang.String name)
      Finds the child with the specified name and returns its first child.
      double getDouble​(int index)
      Finds the child with the specified index and returns it as a double.
      double getDouble​(java.lang.String name)
      Finds the child with the specified name and returns it as a double.
      double getDouble​(java.lang.String name, double defaultValue)
      Finds the child with the specified name and returns it as a double.
      float getFloat​(int index)
      Finds the child with the specified index and returns it as a float.
      float getFloat​(java.lang.String name)
      Finds the child with the specified name and returns it as a float.
      float getFloat​(java.lang.String name, float defaultValue)
      Finds the child with the specified name and returns it as a float.
      int getInt​(int index)
      Finds the child with the specified index and returns it as an int.
      int getInt​(java.lang.String name)
      Finds the child with the specified name and returns it as an int.
      int getInt​(java.lang.String name, int defaultValue)
      Finds the child with the specified name and returns it as an int.
      long getLong​(int index)
      Finds the child with the specified index and returns it as a long.
      long getLong​(java.lang.String name)
      Finds the child with the specified name and returns it as a long.
      long getLong​(java.lang.String name, long defaultValue)
      Finds the child with the specified name and returns it as a long.
      short getShort​(int index)
      Finds the child with the specified index and returns it as a short.
      short getShort​(java.lang.String name)
      Finds the child with the specified name and returns it as a short.
      short getShort​(java.lang.String name, short defaultValue)
      Finds the child with the specified name and returns it as a short.
      java.lang.String getString​(int index)
      Finds the child with the specified index and returns it as a string.
      java.lang.String getString​(java.lang.String name)
      Finds the child with the specified name and returns it as a string.
      java.lang.String getString​(java.lang.String name, java.lang.String defaultValue)
      Finds the child with the specified name and returns it as a string.
      boolean has​(java.lang.String name)
      Returns true if a child with the specified name exists.
      boolean hasChild​(java.lang.String name)
      Returns true if a child with the specified name exists and has a child.
      boolean isArray()  
      boolean isBoolean()  
      boolean isDouble()  
      boolean isEmpty()
      Returns true if there are not children in the array or object.
      boolean isLong()  
      boolean isNull()  
      boolean isNumber()
      Returns true if this is a double or long value.
      boolean isObject()  
      boolean isString()  
      boolean isValue()
      Returns true if this is not an array or object.
      JsonValue.JsonIterator iterator()
      Iterates the children of this array or object.
      JsonValue.JsonIterator iterator​(java.lang.String name)
      Returns an iterator for the child with the specified name, or an empty iterator if no child is found.
      java.lang.String name()
      Returns the name for this object value.
      JsonValue next()
      Returns the next sibling of this value.
      boolean notEmpty()
      Returns true if there are one or more children in the array or object.
      JsonValue parent()
      Returns the parent for this value.
      java.lang.String prettyPrint​(JsonValue.PrettyPrintSettings settings)  
      java.lang.String prettyPrint​(JsonWriter.OutputType outputType, int singleLineColumns)  
      void prettyPrint​(JsonWriter.OutputType outputType, java.io.Writer writer)
      JsonValue prev()
      Returns the previous sibling of this value.
      void remove()
      Removes this value from its parent.
      JsonValue remove​(int index)
      Removes the child with the specified index.
      JsonValue remove​(java.lang.String name)
      Removes the child with the specified name.
      JsonValue require​(int index)
      Returns the child at the specified index.
      JsonValue require​(java.lang.String name)
      Returns the child with the specified name.
      void set​(boolean value)  
      void set​(double value, java.lang.String stringValue)  
      void set​(long value, java.lang.String stringValue)  
      void set​(java.lang.String value)  
      void setName​(java.lang.String name)  
      void setNext​(JsonValue next)
      Sets the next sibling of this value.
      void setPrev​(JsonValue prev)
      Sets the next sibling of this value.
      void setType​(JsonValue.ValueType type)  
      int size()
      Deprecated.
      Use size instead.
      java.lang.String toJson​(JsonWriter.OutputType outputType)  
      java.lang.String toString()  
      java.lang.String trace()
      Returns a human readable string representing the path from the root of the JSON object graph to this value.
      JsonValue.ValueType type()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • name

        public java.lang.String name
      • parent

        public JsonValue parent
        May be null.
      • next

        public JsonValue next
        May be null. When changing this field the parent size() may need to be changed.
      • prev

        public JsonValue prev
        May be null. When changing this field the parent size() may need to be changed.
      • size

        public int size
    • Constructor Detail

      • JsonValue

        public JsonValue​(@Null
                         java.lang.String value)
        Parameters:
        value - May be null.
      • JsonValue

        public JsonValue​(double value)
      • JsonValue

        public JsonValue​(long value)
      • JsonValue

        public JsonValue​(double value,
                         java.lang.String stringValue)
      • JsonValue

        public JsonValue​(long value,
                         java.lang.String stringValue)
      • JsonValue

        public JsonValue​(boolean value)
    • Method Detail

      • get

        @Null
        public JsonValue get​(int index)
        Returns the child at the specified index. This requires walking the linked list to the specified entry, see JsonValue for how to iterate efficiently.
        Returns:
        May be null.
      • get

        @Null
        public JsonValue get​(java.lang.String name)
        Returns the child with the specified name.
        Returns:
        May be null.
      • has

        public boolean has​(java.lang.String name)
        Returns true if a child with the specified name exists.
      • iterator

        public JsonValue.JsonIterator iterator​(java.lang.String name)
        Returns an iterator for the child with the specified name, or an empty iterator if no child is found.
      • require

        public JsonValue require​(int index)
        Returns the child at the specified index. This requires walking the linked list to the specified entry, see JsonValue for how to iterate efficiently.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • require

        public JsonValue require​(java.lang.String name)
        Returns the child with the specified name.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • remove

        @Null
        public JsonValue remove​(int index)
        Removes the child with the specified index. This requires walking the linked list to the specified entry, see JsonValue for how to iterate efficiently.
        Returns:
        May be null.
      • remove

        @Null
        public JsonValue remove​(java.lang.String name)
        Removes the child with the specified name.
        Returns:
        May be null.
      • remove

        public void remove()
        Removes this value from its parent.
      • notEmpty

        public boolean notEmpty()
        Returns true if there are one or more children in the array or object.
      • isEmpty

        public boolean isEmpty()
        Returns true if there are not children in the array or object.
      • size

        @Deprecated
        public int size()
        Deprecated.
        Use size instead. Returns this number of children in the array or object.
      • asString

        @Null
        public java.lang.String asString()
        Returns this value as a string.
        Returns:
        May be null if this value is null.
        Throws:
        java.lang.IllegalStateException - if this an array or object.
      • asFloat

        public float asFloat()
        Returns this value as a float.
        Throws:
        java.lang.IllegalStateException - if this an array or object.
      • asDouble

        public double asDouble()
        Returns this value as a double.
        Throws:
        java.lang.IllegalStateException - if this an array or object.
      • asLong

        public long asLong()
        Returns this value as a long.
        Throws:
        java.lang.IllegalStateException - if this an array or object.
      • asInt

        public int asInt()
        Returns this value as an int.
        Throws:
        java.lang.IllegalStateException - if this an array or object.
      • asBoolean

        public boolean asBoolean()
        Returns this value as a boolean.
        Throws:
        java.lang.IllegalStateException - if this an array or object.
      • asByte

        public byte asByte()
        Returns this value as a byte.
        Throws:
        java.lang.IllegalStateException - if this an array or object.
      • asShort

        public short asShort()
        Returns this value as a short.
        Throws:
        java.lang.IllegalStateException - if this an array or object.
      • asChar

        public char asChar()
        Returns this value as a char.
        Throws:
        java.lang.IllegalStateException - if this an array or object.
      • asStringArray

        public java.lang.String[] asStringArray()
        Returns the children of this value as a newly allocated String array.
        Throws:
        java.lang.IllegalStateException - if this is not an array.
      • asFloatArray

        public float[] asFloatArray()
        Returns the children of this value as a newly allocated float array.
        Throws:
        java.lang.IllegalStateException - if this is not an array.
      • asDoubleArray

        public double[] asDoubleArray()
        Returns the children of this value as a newly allocated double array.
        Throws:
        java.lang.IllegalStateException - if this is not an array.
      • asLongArray

        public long[] asLongArray()
        Returns the children of this value as a newly allocated long array.
        Throws:
        java.lang.IllegalStateException - if this is not an array.
      • asIntArray

        public int[] asIntArray()
        Returns the children of this value as a newly allocated int array.
        Throws:
        java.lang.IllegalStateException - if this is not an array.
      • asBooleanArray

        public boolean[] asBooleanArray()
        Returns the children of this value as a newly allocated boolean array.
        Throws:
        java.lang.IllegalStateException - if this is not an array.
      • asByteArray

        public byte[] asByteArray()
        Returns the children of this value as a newly allocated byte array.
        Throws:
        java.lang.IllegalStateException - if this is not an array.
      • asShortArray

        public short[] asShortArray()
        Returns the children of this value as a newly allocated short array.
        Throws:
        java.lang.IllegalStateException - if this is not an array.
      • asCharArray

        public char[] asCharArray()
        Returns the children of this value as a newly allocated char array.
        Throws:
        java.lang.IllegalStateException - if this is not an array.
      • hasChild

        public boolean hasChild​(java.lang.String name)
        Returns true if a child with the specified name exists and has a child.
      • getChild

        @Null
        public JsonValue getChild​(java.lang.String name)
        Finds the child with the specified name and returns its first child.
        Returns:
        May be null.
      • getString

        public java.lang.String getString​(java.lang.String name,
                                          @Null
                                          java.lang.String defaultValue)
        Finds the child with the specified name and returns it as a string. Returns defaultValue if not found.
        Parameters:
        defaultValue - May be null.
      • getFloat

        public float getFloat​(java.lang.String name,
                              float defaultValue)
        Finds the child with the specified name and returns it as a float. Returns defaultValue if not found.
      • getDouble

        public double getDouble​(java.lang.String name,
                                double defaultValue)
        Finds the child with the specified name and returns it as a double. Returns defaultValue if not found.
      • getLong

        public long getLong​(java.lang.String name,
                            long defaultValue)
        Finds the child with the specified name and returns it as a long. Returns defaultValue if not found.
      • getInt

        public int getInt​(java.lang.String name,
                          int defaultValue)
        Finds the child with the specified name and returns it as an int. Returns defaultValue if not found.
      • getBoolean

        public boolean getBoolean​(java.lang.String name,
                                  boolean defaultValue)
        Finds the child with the specified name and returns it as a boolean. Returns defaultValue if not found.
      • getByte

        public byte getByte​(java.lang.String name,
                            byte defaultValue)
        Finds the child with the specified name and returns it as a byte. Returns defaultValue if not found.
      • getShort

        public short getShort​(java.lang.String name,
                              short defaultValue)
        Finds the child with the specified name and returns it as a short. Returns defaultValue if not found.
      • getChar

        public char getChar​(java.lang.String name,
                            char defaultValue)
        Finds the child with the specified name and returns it as a char. Returns defaultValue if not found.
      • getString

        public java.lang.String getString​(java.lang.String name)
        Finds the child with the specified name and returns it as a string.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getFloat

        public float getFloat​(java.lang.String name)
        Finds the child with the specified name and returns it as a float.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getDouble

        public double getDouble​(java.lang.String name)
        Finds the child with the specified name and returns it as a double.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getLong

        public long getLong​(java.lang.String name)
        Finds the child with the specified name and returns it as a long.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getInt

        public int getInt​(java.lang.String name)
        Finds the child with the specified name and returns it as an int.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getBoolean

        public boolean getBoolean​(java.lang.String name)
        Finds the child with the specified name and returns it as a boolean.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getByte

        public byte getByte​(java.lang.String name)
        Finds the child with the specified name and returns it as a byte.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getShort

        public short getShort​(java.lang.String name)
        Finds the child with the specified name and returns it as a short.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getChar

        public char getChar​(java.lang.String name)
        Finds the child with the specified name and returns it as a char.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getString

        public java.lang.String getString​(int index)
        Finds the child with the specified index and returns it as a string.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getFloat

        public float getFloat​(int index)
        Finds the child with the specified index and returns it as a float.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getDouble

        public double getDouble​(int index)
        Finds the child with the specified index and returns it as a double.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getLong

        public long getLong​(int index)
        Finds the child with the specified index and returns it as a long.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getInt

        public int getInt​(int index)
        Finds the child with the specified index and returns it as an int.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getBoolean

        public boolean getBoolean​(int index)
        Finds the child with the specified index and returns it as a boolean.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getByte

        public byte getByte​(int index)
        Finds the child with the specified index and returns it as a byte.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getShort

        public short getShort​(int index)
        Finds the child with the specified index and returns it as a short.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • getChar

        public char getChar​(int index)
        Finds the child with the specified index and returns it as a char.
        Throws:
        java.lang.IllegalArgumentException - if the child was not found.
      • isArray

        public boolean isArray()
      • isObject

        public boolean isObject()
      • isString

        public boolean isString()
      • isNumber

        public boolean isNumber()
        Returns true if this is a double or long value.
      • isDouble

        public boolean isDouble()
      • isLong

        public boolean isLong()
      • isBoolean

        public boolean isBoolean()
      • isNull

        public boolean isNull()
      • isValue

        public boolean isValue()
        Returns true if this is not an array or object.
      • name

        @Null
        public java.lang.String name()
        Returns the name for this object value.
        Returns:
        May be null.
      • setName

        public void setName​(@Null
                            java.lang.String name)
        Parameters:
        name - May be null.
      • parent

        @Null
        public JsonValue parent()
        Returns the parent for this value.
        Returns:
        May be null.
      • child

        @Null
        public JsonValue child()
        Returns the first child for this object or array.
        Returns:
        May be null.
      • addChild

        public void addChild​(java.lang.String name,
                             JsonValue value)
        Sets the name of the specified value and adds it after the last child.
      • addChild

        public void addChild​(JsonValue value)
        Adds the specified value after the last child.
        Throws:
        java.lang.IllegalStateException - if this is an object and the specified child's name is null.
      • next

        @Null
        public JsonValue next()
        Returns the next sibling of this value.
        Returns:
        May be null.
      • setNext

        public void setNext​(@Null
                            JsonValue next)
        Sets the next sibling of this value. Does not change the parent size().
        Parameters:
        next - May be null.
      • prev

        @Null
        public JsonValue prev()
        Returns the previous sibling of this value.
        Returns:
        May be null.
      • setPrev

        public void setPrev​(@Null
                            JsonValue prev)
        Sets the next sibling of this value. Does not change the parent size().
        Parameters:
        prev - May be null.
      • set

        public void set​(@Null
                        java.lang.String value)
        Parameters:
        value - May be null.
      • set

        public void set​(double value,
                        @Null
                        java.lang.String stringValue)
        Parameters:
        stringValue - May be null if the string representation is the string value of the double (eg, no leading zeros).
      • set

        public void set​(long value,
                        @Null
                        java.lang.String stringValue)
        Parameters:
        stringValue - May be null if the string representation is the string value of the long (eg, no leading zeros).
      • set

        public void set​(boolean value)
      • iterator

        public JsonValue.JsonIterator iterator()
        Iterates the children of this array or object.
        Specified by:
        iterator in interface java.lang.Iterable<JsonValue>
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • trace

        public java.lang.String trace()
        Returns a human readable string representing the path from the root of the JSON object graph to this value.
      • prettyPrint

        public java.lang.String prettyPrint​(JsonWriter.OutputType outputType,
                                            int singleLineColumns)