net.sourceforge.javajson
Class JsonObject

java.lang.Object
  extended by net.sourceforge.javajson.JsonObject
All Implemented Interfaces:
Iterable<String>

public class JsonObject
extends Object
implements Iterable<String>

Simpler implementation of Json that throws less exceptions. For all the getXXX method, if the item is not found, it returns null, false, or 0. If they key is null or otherwise invalid, it throws an exception.

Author:
mdeanda

Constructor Summary
JsonObject()
           
 
Method Summary
 JsonObject accumulate(String key, boolean val)
          Accumlates multiple values into an array.
 JsonObject accumulate(String key, double val)
          Accumlates multiple values into an array.
 JsonObject accumulate(String key, float val)
          Accumlates multiple values into an array.
 JsonObject accumulate(String key, int val)
          Accumlates multiple values into an array.
 JsonObject accumulate(String key, JsonArray val)
          Accumlates multiple values into an array.
 JsonObject accumulate(String key, JsonObject val)
          Accumlates multiple values into an array.
 JsonObject accumulate(String key, String val)
          Accumlates multiple values into an array.
 boolean getBoolean(String key)
           
 double getDouble(String key)
           
 float getFloat(String key)
           
 int getInt(String key)
           
 JsonArray getJsonArray(String key)
           
 JsonObject getJsonObject(String key)
           
 long getLong(String key)
           
 String getString(String key)
           
 boolean hasKey(String key)
           
 boolean isBoolean(String key)
          Checks if the item at a key is a boolean.
 boolean isDouble(String key)
          Checks if the item at a key is a double.
 boolean isFloat(String key)
          Checks if the item at a key is a float.
 boolean isInt(String key)
          Checks if the item at a key is a int.
 boolean isJsonArray(String key)
          Checks if the item at a key is an array.
 boolean isJsonObject(String key)
          Checks if the item at a key is an object.
 boolean isLong(String key)
          Checks if the item at a key is a long.
 boolean isNull(String key)
          Checks if the field contains a null value (different than hasKey because it can have the key but be null)
 boolean isSimilar(JsonObject obj)
          checks that this object contains all fields as passed object.
 boolean isString(String key)
          Checks if the item at a key is an string.
 Iterator<String> iterator()
           
static JsonObject parse(InputStream is)
          Parses a string to a json object.
static JsonObject parse(Reader reader)
          Parses a string to a json object.
static JsonObject parse(String input)
          Parses a string to a json object.
 JsonObject put(String key, boolean value)
           
 JsonObject put(String key, double value)
           
 JsonObject put(String key, float value)
           
 JsonObject put(String key, int value)
           
 JsonObject put(String key, JsonArray value)
           
 JsonObject put(String key, JsonObject value)
           
 JsonObject put(String key, long value)
           
 void put(String key, Object value)
           
 JsonObject put(String key, String value)
           
 int size()
           
 String toString()
           
 String toString(int spacing)
          Returns a nicely formatted string
protected  String toString(int spacing, int margin)
          Called by toString(int) to keep track of the spacing
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JsonObject

public JsonObject()
Method Detail

parse

public static JsonObject parse(String input)
                        throws JsonException
Parses a string to a json object.

Throws:
JsonException

parse

public static JsonObject parse(Reader reader)
                        throws JsonException
Parses a string to a json object.

Throws:
JsonException

parse

public static JsonObject parse(InputStream is)
                        throws JsonException
Parses a string to a json object.

Throws:
JsonException

accumulate

public JsonObject accumulate(String key,
                             boolean val)
Accumlates multiple values into an array. This is a convenience function for creating an array, adding it to the object, then adding to the array. If there is already a value for this key, it will be the first item in the array

Parameters:
key -
val -

accumulate

public JsonObject accumulate(String key,
                             double val)
Accumlates multiple values into an array. This is a convenience function for creating an array, adding it to the object, then adding to the array. If there is already a value for this key, it will be the first item in the array

Parameters:
key -
val -

accumulate

public JsonObject accumulate(String key,
                             float val)
Accumlates multiple values into an array. This is a convenience function for creating an array, adding it to the object, then adding to the array. If there is already a value for this key, it will be the first item in the array

Parameters:
key -
val -

accumulate

public JsonObject accumulate(String key,
                             int val)
Accumlates multiple values into an array. This is a convenience function for creating an array, adding it to the object, then adding to the array. If there is already a value for this key, it will be the first item in the array

Parameters:
key -
val -

accumulate

public JsonObject accumulate(String key,
                             JsonArray val)
Accumlates multiple values into an array. This is a convenience function for creating an array, adding it to the object, then adding to the array. If there is already a value for this key, it will be the first item in the array

Parameters:
key -
val -

accumulate

public JsonObject accumulate(String key,
                             JsonObject val)
Accumlates multiple values into an array. This is a convenience function for creating an array, adding it to the object, then adding to the array. If there is already a value for this key, it will be the first item in the array

Parameters:
key -
val -

accumulate

public JsonObject accumulate(String key,
                             String val)
Accumlates multiple values into an array. This is a convenience function for creating an array, adding it to the object, then adding to the array. If there is already a value for this key, it will be the first item in the array

Parameters:
key -
val -

getBoolean

public boolean getBoolean(String key)

getDouble

public double getDouble(String key)

getFloat

public float getFloat(String key)

getInt

public int getInt(String key)

getJsonArray

public JsonArray getJsonArray(String key)

getJsonObject

public JsonObject getJsonObject(String key)

getLong

public long getLong(String key)

getString

public String getString(String key)

hasKey

public boolean hasKey(String key)

isBoolean

public boolean isBoolean(String key)
Checks if the item at a key is a boolean. See JsonValue.isBoolean() for more information

Parameters:
key -
Returns:
True if the object contains the item and is a boolean

isDouble

public boolean isDouble(String key)
Checks if the item at a key is a double. See JsonValue.isDouble() for more information

Parameters:
key -

isFloat

public boolean isFloat(String key)
Checks if the item at a key is a float. See JsonValue.isFloat() for more information

Parameters:
key -

isInt

public boolean isInt(String key)
Checks if the item at a key is a int. See JsonValue.isInt() for more information

Parameters:
key -

isLong

public boolean isLong(String key)
Checks if the item at a key is a long. See JsonValue.isLong() for more information

Parameters:
key -

isJsonArray

public boolean isJsonArray(String key)
Checks if the item at a key is an array. See JsonValue.isJsonArray() for more information

Parameters:
key -
Returns:
True if the object contains the item and is a json array

isJsonObject

public boolean isJsonObject(String key)
Checks if the item at a key is an object. See JsonValue.isJsonObject() for more information

Parameters:
key -
Returns:
True if the object contains the item and is a json object

isNull

public boolean isNull(String key)
Checks if the field contains a null value (different than hasKey because it can have the key but be null)


isSimilar

public boolean isSimilar(JsonObject obj)
checks that this object contains all fields as passed object. also checks that types match. if this object contains more fields then they can still be similar, but calling in reverse order may return false

Parameters:
obj -
Returns:

isString

public boolean isString(String key)
Checks if the item at a key is an string. See JsonValue.isString() for more information

Parameters:
key -
Returns:
True if the object contains the item and is a string

iterator

public Iterator<String> iterator()
Specified by:
iterator in interface Iterable<String>

put

public void put(String key,
                Object value)

put

public JsonObject put(String key,
                      boolean value)

put

public JsonObject put(String key,
                      double value)

put

public JsonObject put(String key,
                      float value)

put

public JsonObject put(String key,
                      int value)

put

public JsonObject put(String key,
                      JsonArray value)

put

public JsonObject put(String key,
                      JsonObject value)

put

public JsonObject put(String key,
                      long value)

put

public JsonObject put(String key,
                      String value)

size

public int size()

toString

public String toString()
Overrides:
toString in class Object

toString

public String toString(int spacing)
Returns a nicely formatted string

Parameters:
spacing -
Returns:

toString

protected String toString(int spacing,
                          int margin)
Called by toString(int) to keep track of the spacing

Parameters:
spacing -
margin -
Returns: