Package arc.util.serialization
Class JsonValue
java.lang.Object
arc.util.serialization.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 a for loop, either with the enhanced
for loop syntactic sugar or like the example below. This is much more efficient than accessing children by index when there are
many children.
JsonValue map = ...;
for (JsonValue entry = map.child; entry != null; entry = entry.next)
System.out.println(entry.name + " = " + entry.asString());
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds the specified value after the last child.voidSets the name of the specified value and adds it after the last child.booleanReturns this value as a boolean.boolean[]Returns the children of this value as a newly allocated boolean array.byteasByte()Returns this value as a byte.byte[]Returns the children of this value as a newly allocated byte array.charasChar()Returns this value as a char.char[]Returns the children of this value as a newly allocated char array.doubleasDouble()Returns this value as a double.double[]Returns the children of this value as a newly allocated double array.floatasFloat()Returns this value as a float.float[]Returns the children of this value as a newly allocated float array.intasInt()Returns this value as an int.int[]Returns the children of this value as a newly allocated int array.longasLong()Returns this value as a long.long[]Returns the children of this value as a newly allocated long array.shortasShort()Returns this value as a short.short[]Returns the children of this value as a newly allocated short array.asString()Returns this value as a string.String[]Returns the children of this value as a newly allocated String array.child()Returns the first child for this object or array.copy()Makes a copy of thisJsonValue.get(int index) Returns the child at the specified index.Returns the child with the specified name.booleangetBoolean(int index) Finds the child with the specified index and returns it as a boolean.booleangetBoolean(String name) Finds the child with the specified name and returns it as a boolean.booleangetBoolean(String name, boolean defaultValue) Finds the child with the specified name and returns it as a boolean.bytegetByte(int index) Finds the child with the specified index and returns it as a byte.byteFinds the child with the specified name and returns it as a byte.byteFinds the child with the specified name and returns it as a byte.chargetChar(int index) Finds the child with the specified index and returns it as a char.charFinds the child with the specified name and returns it as a char.charFinds the child with the specified name and returns it as a char.Finds the child with the specified name and returns its first child.doublegetDouble(int index) Finds the child with the specified index and returns it as a double.doubleFinds the child with the specified name and returns it as a double.doubleFinds the child with the specified name and returns it as a double.floatgetFloat(int index) Finds the child with the specified index and returns it as a float.floatFinds the child with the specified name and returns it as a float.floatFinds the child with the specified name and returns it as a float.intgetInt(int index) Finds the child with the specified index and returns it as an int.intFinds the child with the specified name and returns it as an int.intFinds the child with the specified name and returns it as an int.longgetLong(int index) Finds the child with the specified index and returns it as a long.longFinds the child with the specified name and returns it as a long.longFinds the child with the specified name and returns it as a long.shortgetShort(int index) Finds the child with the specified index and returns it as a short.shortFinds the child with the specified name and returns it as a short.shortFinds the child with the specified name and returns it as a short.getString(int index) Finds the child with the specified index and returns it as a string.Finds the child with the specified name and returns it as a string.Finds the child with the specified name and returns it as a string.booleanReturns true if a child with the specified name exists.booleanReturns true if a child with the specified name exists and has a child.booleanisArray()booleanbooleanisDouble()booleanisLong()booleanisNull()booleanisNumber()Returns true if this is a double or long value.booleanisObject()booleanisString()booleanisValue()Returns true if this is not an array or object.iterator()name()Returns the name for this object value.next()Returns the next sibling of this value.parent()Returns the parent for this value.prettyPrint(JsonWriter.OutputType outputType) voidprettyPrint(JsonWriter.OutputType outputType, Writer writer) More efficient thanprettyPrint(PrettyPrintSettings)butPrettyPrintSettings#singleLineColumnsandPrettyPrintSettings#wrapNumericArraysare not supported.prev()Returns the previous sibling of this value.remove(int index) Removes the child with the specified index.Removes the child with the specified name.require(int index) Returns the child at the specified index.Returns the child with the specified name.voidset(boolean value) voidvoidvoidvoidvoidvoidvoidsetType(JsonValue.ValueType type) toJson(JsonWriter.OutputType outputType) voidtoJson(Writer writer, JsonWriter.OutputType outputType) toString()trace()Returns a human readable string representing the path from the root of the JSON object graph to this value.type()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
name
-
child
-
next
-
prev
-
parent
-
size
public int size
-
-
Constructor Details
-
JsonValue
-
JsonValue
- Parameters:
value- May be null.
-
JsonValue
public JsonValue(double value) -
JsonValue
public JsonValue(long value) -
JsonValue
-
JsonValue
-
JsonValue
public JsonValue(boolean value)
-
-
Method Details
-
copy
-
get
Returns the child at the specified index. This requires walking the linked list to the specified entry, seeJsonValuefor how to iterate efficiently.- Returns:
- May be null.
-
get
Returns the child with the specified name.- Returns:
- May be null.
-
has
Returns true if a child with the specified name exists. -
require
Returns the child at the specified index. This requires walking the linked list to the specified entry, seeJsonValuefor how to iterate efficiently.- Throws:
IllegalArgumentException- if the child was not found.
-
require
Returns the child with the specified name.- Throws:
IllegalArgumentException- if the child was not found.
-
remove
Removes the child with the specified index. This requires walking the linked list to the specified entry, seeJsonValuefor how to iterate efficiently.- Returns:
- May be null.
-
remove
Removes the child with the specified name.- Returns:
- May be null.
-
asString
Returns this value as a string.- Returns:
- May be null if this value is null.
- Throws:
IllegalStateException- if this an array or object.
-
asFloat
public float asFloat()Returns this value as a float.- Throws:
IllegalStateException- if this an array or object.
-
asDouble
public double asDouble()Returns this value as a double.- Throws:
IllegalStateException- if this an array or object.
-
asLong
public long asLong()Returns this value as a long.- Throws:
IllegalStateException- if this an array or object.
-
asInt
public int asInt()Returns this value as an int.- Throws:
IllegalStateException- if this an array or object.
-
asBoolean
public boolean asBoolean()Returns this value as a boolean.- Throws:
IllegalStateException- if this an array or object.
-
asByte
public byte asByte()Returns this value as a byte.- Throws:
IllegalStateException- if this an array or object.
-
asShort
public short asShort()Returns this value as a short.- Throws:
IllegalStateException- if this an array or object.
-
asChar
public char asChar()Returns this value as a char.- Throws:
IllegalStateException- if this an array or object.
-
asStringArray
Returns the children of this value as a newly allocated String array.- Throws:
IllegalStateException- if this is not an array.
-
asFloatArray
public float[] asFloatArray()Returns the children of this value as a newly allocated float array.- Throws:
IllegalStateException- if this is not an array.
-
asDoubleArray
public double[] asDoubleArray()Returns the children of this value as a newly allocated double array.- Throws:
IllegalStateException- if this is not an array.
-
asLongArray
public long[] asLongArray()Returns the children of this value as a newly allocated long array.- Throws:
IllegalStateException- if this is not an array.
-
asIntArray
public int[] asIntArray()Returns the children of this value as a newly allocated int array.- Throws:
IllegalStateException- if this is not an array.
-
asBooleanArray
public boolean[] asBooleanArray()Returns the children of this value as a newly allocated boolean array.- Throws:
IllegalStateException- if this is not an array.
-
asByteArray
public byte[] asByteArray()Returns the children of this value as a newly allocated byte array.- Throws:
IllegalStateException- if this is not an array.
-
asShortArray
public short[] asShortArray()Returns the children of this value as a newly allocated short array.- Throws:
IllegalStateException- if this is not an array.
-
asCharArray
public char[] asCharArray()Returns the children of this value as a newly allocated char array.- Throws:
IllegalStateException- if this is not an array.
-
hasChild
Returns true if a child with the specified name exists and has a child. -
getChild
Finds the child with the specified name and returns its first child.- Returns:
- May be null.
-
getString
Finds the child with the specified name and returns it as a string. Returns defaultValue if not found.- Parameters:
defaultValue- May be null.
-
getFloat
Finds the child with the specified name and returns it as a float. Returns defaultValue if not found. -
getDouble
Finds the child with the specified name and returns it as a double. Returns defaultValue if not found. -
getLong
Finds the child with the specified name and returns it as a long. Returns defaultValue if not found. -
getInt
Finds the child with the specified name and returns it as an int. Returns defaultValue if not found. -
getBoolean
Finds the child with the specified name and returns it as a boolean. Returns defaultValue if not found. -
getByte
Finds the child with the specified name and returns it as a byte. Returns defaultValue if not found. -
getShort
Finds the child with the specified name and returns it as a short. Returns defaultValue if not found. -
getChar
Finds the child with the specified name and returns it as a char. Returns defaultValue if not found. -
getString
Finds the child with the specified name and returns it as a string.- Throws:
IllegalArgumentException- if the child was not found.
-
getFloat
Finds the child with the specified name and returns it as a float.- Throws:
IllegalArgumentException- if the child was not found.
-
getDouble
Finds the child with the specified name and returns it as a double.- Throws:
IllegalArgumentException- if the child was not found.
-
getLong
Finds the child with the specified name and returns it as a long.- Throws:
IllegalArgumentException- if the child was not found.
-
getInt
Finds the child with the specified name and returns it as an int.- Throws:
IllegalArgumentException- if the child was not found.
-
getBoolean
Finds the child with the specified name and returns it as a boolean.- Throws:
IllegalArgumentException- if the child was not found.
-
getByte
Finds the child with the specified name and returns it as a byte.- Throws:
IllegalArgumentException- if the child was not found.
-
getShort
Finds the child with the specified name and returns it as a short.- Throws:
IllegalArgumentException- if the child was not found.
-
getChar
Finds the child with the specified name and returns it as a char.- Throws:
IllegalArgumentException- if the child was not found.
-
getString
Finds the child with the specified index and returns it as a string.- Throws:
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:
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:
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:
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:
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:
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:
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:
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:
IllegalArgumentException- if the child was not found.
-
type
-
setType
-
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
Returns the name for this object value.- Returns:
- May be null.
-
setName
-
parent
Returns the parent for this value.- Returns:
- May be null.
-
child
Returns the first child for this object or array.- Returns:
- May be null.
-
addChild
Sets the name of the specified value and adds it after the last child. -
addChild
Adds the specified value after the last child. -
next
Returns the next sibling of this value.- Returns:
- May be null.
-
setNext
-
prev
Returns the previous sibling of this value.- Returns:
- May be null.
-
setPrev
-
set
- Parameters:
value- May be null.
-
set
- Parameters:
stringValue- May be null if the string representation is the string value of the double (eg, no leading zeros).
-
set
- 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) -
toJson
-
toJson
- Throws:
IOException
-
toString
-
prettyPrint
-
prettyPrint
More efficient thanprettyPrint(PrettyPrintSettings)butPrettyPrintSettings#singleLineColumnsandPrettyPrintSettings#wrapNumericArraysare not supported.- Throws:
IOException
-
iterator
-
trace
Returns a human readable string representing the path from the root of the JSON object graph to this value.
-