com.badlogic.gdx.utils
Class JsonValue

java.lang.Object
  extended by com.badlogic.gdx.utils.JsonValue
All Implemented Interfaces:
Iterable<JsonValue>

public class JsonValue
extends Object
implements 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 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());
 

Author:
Nathan Sweet

Nested Class Summary
 class JsonValue.JsonIterator
           
static class JsonValue.ValueType
           
 
Field Summary
 JsonValue child
          May be null.
 String name
           
 JsonValue next
          May be null.
 JsonValue prev
          May be null.
 int size
           
 
Constructor Summary
JsonValue(boolean value)
           
JsonValue(double value)
           
JsonValue(JsonValue.ValueType type)
           
JsonValue(long value)
           
JsonValue(String value)
           
 
Method Summary
 boolean asBoolean()
          Returns this value as a boolean.
 double asDouble()
          Returns this value as a double.
 float asFloat()
          Returns this value as a float.
 int asInt()
          Returns this value as an int.
 long asLong()
          Returns this value as a long.
 String asString()
          Returns this value as a string.
 JsonValue child()
          Returns the first child for this object or array.
 JsonValue get(int index)
          Returns the child at the specified index.
 JsonValue get(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(String name)
          Finds the child with the specified name and returns it as a boolean.
 boolean getBoolean(String name, boolean defaultValue)
          Finds the child with the specified name and returns it as a boolean.
 JsonValue getChild(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(String name)
          Finds the child with the specified name and returns it as a double.
 double getDouble(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(String name)
          Finds the child with the specified name and returns it as a float.
 float getFloat(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(String name)
          Finds the child with the specified name and returns it as an int.
 int getInt(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(String name)
          Finds the child with the specified name and returns it as a long.
 long getLong(String name, long defaultValue)
          Finds the child with the specified name and returns it as a long.
 String getString(int index)
          Finds the child with the specified index and returns it as a string.
 String getString(String name)
          Finds the child with the specified name and returns it as a string.
 String getString(String name, String defaultValue)
          Finds the child with the specified name and returns it as a string.
 boolean hasChild(String name)
          Returns true if a child with the specified name exists and has a child.
 boolean isArray()
           
 boolean isBoolean()
           
 boolean isDouble()
           
 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()
           
 String name()
          Returns the name for this object value.
 JsonValue next()
          Returns the next sibling of this value.
 String prettyPrint(JsonWriter.OutputType outputType, int singleLineColumns)
           
 JsonValue prev()
          Returns the previous sibling of this value.
 JsonValue remove(int index)
          Removes the child with the specified name.
 JsonValue remove(String name)
          Removes the child with the specified name.
 JsonValue require(int index)
          Returns the child at the specified index.
 JsonValue require(String name)
          Returns the child with the specified name.
 void set(boolean value)
           
 void set(double value)
           
 void set(long value)
           
 void set(String value)
           
 void setName(String name)
           
 void setNext(JsonValue next)
           
 void setPrev(JsonValue prev)
           
 void setType(JsonValue.ValueType type)
           
 int size()
          Deprecated. Use the size property instead. Returns this number of children in the array or object.
 String toString()
           
 JsonValue.ValueType type()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

name

public String name

child

public JsonValue child
May be null.


next

public JsonValue next
May be null.


prev

public JsonValue prev
May be null.


size

public int size
Constructor Detail

JsonValue

public JsonValue(JsonValue.ValueType type)

JsonValue

public JsonValue(String value)
Parameters:
value - May be null.

JsonValue

public JsonValue(double value)

JsonValue

public JsonValue(long value)

JsonValue

public JsonValue(boolean value)
Method Detail

get

public JsonValue get(int index)
Returns the child at the specified index.

Returns:
May be null.

get

public JsonValue get(String name)
Returns the child with the specified name.

Returns:
May be null.

require

public JsonValue require(int index)
Returns the child at the specified index.

Throws:
IllegalArgumentException - if the child was not found.

require

public JsonValue require(String name)
Returns the child with the specified name.

Throws:
IllegalArgumentException - if the child was not found.

remove

public JsonValue remove(int index)
Removes the child with the specified name.

Returns:
May be null.

remove

public JsonValue remove(String name)
Removes the child with the specified name.

Returns:
May be null.

size

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


asString

public String 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.

hasChild

public boolean hasChild(String name)
Returns true if a child with the specified name exists and has a child.


getChild

public JsonValue getChild(String name)
Finds the child with the specified name and returns its first child.

Returns:
May be null.

getString

public String getString(String name,
                        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(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(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(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(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(String name,
                          boolean defaultValue)
Finds the child with the specified name and returns it as a boolean. Returns defaultValue if not found.


getString

public String getString(String name)
Finds the child with the specified name and returns it as a string.

Throws:
IllegalArgumentException - if the child was not found.

getFloat

public float getFloat(String name)
Finds the child with the specified name and returns it as a float.

Throws:
IllegalArgumentException - if the child was not found.

getDouble

public double getDouble(String name)
Finds the child with the specified name and returns it as a double.

Throws:
IllegalArgumentException - if the child was not found.

getLong

public long getLong(String name)
Finds the child with the specified name and returns it as a long.

Throws:
IllegalArgumentException - if the child was not found.

getInt

public int getInt(String name)
Finds the child with the specified name and returns it as an int.

Throws:
IllegalArgumentException - if the child was not found.

getBoolean

public boolean getBoolean(String name)
Finds the child with the specified name and returns it as a boolean.

Throws:
IllegalArgumentException - if the child was not found.

getString

public String getString(int index)
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.

type

public JsonValue.ValueType type()

setType

public void setType(JsonValue.ValueType type)

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

public String name()
Returns the name for this object value.

Returns:
May be null.

setName

public void setName(String name)

child

public JsonValue child()
Returns the first child for this object or array.

Returns:
May be null.

next

public JsonValue next()
Returns the next sibling of this value.

Returns:
May be null.

setNext

public void setNext(JsonValue next)

prev

public JsonValue prev()
Returns the previous sibling of this value.

Returns:
May be null.

setPrev

public void setPrev(JsonValue prev)

set

public void set(String value)
Parameters:
value - May be null.

set

public void set(double value)

set

public void set(long value)

set

public void set(boolean value)

toString

public String toString()
Overrides:
toString in class Object

prettyPrint

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

iterator

public JsonValue.JsonIterator iterator()
Specified by:
iterator in interface Iterable<JsonValue>


Copyright © 2013. All Rights Reserved.