org.pfsw.text.json
Class JsonObject

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by java.util.LinkedHashMap<java.lang.String,java.lang.Object>
              extended by org.pfsw.text.json.JsonObject
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<java.lang.String,java.lang.Object>, org.pfsw.bif.facet.IReadOnlyNamedValues<java.lang.Object>, org.pfsw.bif.text.IJSONConvertible, org.pfsw.bif.text.IStringRepresentation, JsonType

public class JsonObject
extends java.util.LinkedHashMap<java.lang.String,java.lang.Object>
implements JsonType, org.pfsw.bif.facet.IReadOnlyNamedValues<java.lang.Object>

The Java representation of a JSON object. A typed map that has string keys.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Field Summary
 
Fields inherited from interface org.pfsw.bif.text.IJSONConvertible
JSON_ARRAY_END, JSON_ARRAY_START, JSON_ELEMENT_SEPARATOR, JSON_LITERAL_FALSE, JSON_LITERAL_NULL, JSON_LITERAL_TRUE, JSON_OBJECT_END, JSON_OBJECT_START, JSON_PAIR_SEPARATOR, JSON_STRING_DELIMITER, JSON_STRING_ESCAPE
 
Constructor Summary
JsonObject()
           
JsonObject(int initialCapacity)
           
JsonObject(int initialCapacity, float loadFactor)
           
JsonObject(java.util.Map<? extends java.lang.String,? extends java.lang.Object> map)
           
 
Method Summary
 void appendAsJSONString(java.lang.Appendable output)
           
 java.lang.String asString()
          Returns the JSON string representation of this object.
 java.math.BigDecimal getBigDecimal(org.pfsw.bif.text.IStringConstant name)
          Returns the value for the given name as BigDecimal (if it is one).
 java.math.BigDecimal getBigDecimal(java.lang.String name)
          Returns the value for the given name as BigDecimal (if it is one).
 java.lang.Boolean getBoolean(org.pfsw.bif.text.IStringConstant name)
          Returns the value for the given name as Boolean (if it is one).
 java.lang.Boolean getBoolean(java.lang.String name)
          Returns the value for the given name as Boolean (if it is one).
 java.lang.Integer getInteger(org.pfsw.bif.text.IStringConstant name)
          Returns the value for the given name as Integer (if it is one).
 java.lang.Integer getInteger(java.lang.String name)
          Returns the value for the given name as Integer (if it is one).
 JsonArray getJsonArray(org.pfsw.bif.text.IStringConstant name)
          Returns the value for the given name as JsonArray (if it is one).
 JsonArray getJsonArray(java.lang.String name)
          Returns the value for the given name as JsonArray (if it is one).
 JsonObject getJsonObject(org.pfsw.bif.text.IStringConstant name)
          Returns the value for the given name as JsonObject (if it is one).
 JsonObject getJsonObject(java.lang.String name)
          Returns the value for the given name as JsonObject (if it is one).
 java.lang.Long getLong(org.pfsw.bif.text.IStringConstant name)
          Returns the value for the given name as Long (if it is one).
 java.lang.Long getLong(java.lang.String name)
          Returns the value for the given name as Long (if it is one).
 java.util.Collection<java.lang.String> getNames()
           
 java.lang.Number getNumber(org.pfsw.bif.text.IStringConstant name)
          Returns the value for the given name as Number (if it is one).
 java.lang.Number getNumber(java.lang.String name)
          Returns the value for the given name as Number (if it is one).
 java.lang.String getString(org.pfsw.bif.text.IStringConstant name)
          Returns the value for the given name as String (if it is one).
 java.lang.String getString(java.lang.String name)
          Returns the value for the given name as String (if it is one).
protected
<T> T
getTypedElement(java.lang.String name, java.lang.Class<T> type)
           
 java.lang.Object getValue(org.pfsw.bif.text.IStringConstant name)
           
 java.lang.Object getValue(java.lang.String name)
           
<T> T
getValueOfType(org.pfsw.bif.text.IStringConstant name, java.lang.Class<T> type)
          Returns the value for the given name as type of the given class.
<T> T
getValueOfType(java.lang.String name, java.lang.Class<T> type)
          Returns the value for the given name as type of the given class.
 boolean isArray()
          Returns false because this is no JSON array representation (in Java).
 boolean isObject()
          Returns true because this is a JSON object representation (in Java).
 java.lang.Object put(java.lang.String key, java.lang.Object value)
          Adds the given element if it is a valid JSON object type.
 JsonObject setField(org.pfsw.bif.text.IStringConstant name, java.lang.Object value)
          Adds the given element if it is a valid JSON object type.
 java.lang.String toJSON()
           
 
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, get, removeEldestEntry
 
Methods inherited from class java.util.HashMap
clone, containsKey, entrySet, isEmpty, keySet, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.pfsw.bif.facet.IReadOnlyNamedValues
isEmpty
 
Methods inherited from interface java.util.Map
containsKey, entrySet, equals, hashCode, isEmpty, keySet, putAll, remove, size, values
 

Constructor Detail

JsonObject

public JsonObject()

JsonObject

public JsonObject(int initialCapacity,
                  float loadFactor)

JsonObject

public JsonObject(int initialCapacity)

JsonObject

public JsonObject(java.util.Map<? extends java.lang.String,? extends java.lang.Object> map)
Method Detail

put

public java.lang.Object put(java.lang.String key,
                            java.lang.Object value)
Adds the given element if it is a valid JSON object type.

Valid types are:

Objects of other types will cause an exception.

Specified by:
put in interface java.util.Map<java.lang.String,java.lang.Object>
Overrides:
put in class java.util.HashMap<java.lang.String,java.lang.Object>
Returns:
true if the object was added.
Throws:
java.lang.IllegalArgumentException - If the given object is not of a valid JSON type.

setField

public JsonObject setField(org.pfsw.bif.text.IStringConstant name,
                           java.lang.Object value)
Adds the given element if it is a valid JSON object type.

Valid types are:

Objects of other types will cause an exception.

Returns:
this JSON object.
Throws:
java.lang.IllegalArgumentException - If the given object is not of a valid JSON type.

getJsonObject

public JsonObject getJsonObject(java.lang.String name)
Returns the value for the given name as JsonObject (if it is one).

Parameters:
name - The name of the object to look for (must not be null).
Returns:
The found JsonObject or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no JsonObject.

getJsonObject

public JsonObject getJsonObject(org.pfsw.bif.text.IStringConstant name)
Returns the value for the given name as JsonObject (if it is one).

Parameters:
name - The name of the object to look for (must not be null).
Returns:
The found JsonObject or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no JsonObject.

getJsonArray

public JsonArray getJsonArray(java.lang.String name)
Returns the value for the given name as JsonArray (if it is one).

Parameters:
name - The name of the array to look for (must not be null).
Returns:
The found JsonArray or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no JsonArray.

getJsonArray

public JsonArray getJsonArray(org.pfsw.bif.text.IStringConstant name)
Returns the value for the given name as JsonArray (if it is one).

Parameters:
name - The name of the array to look for (must not be null).
Returns:
The found JsonArray or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no JsonArray.

getString

public java.lang.String getString(java.lang.String name)
Returns the value for the given name as String (if it is one).

Parameters:
name - The name of the value to look for (must not be null).
Returns:
The found String or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no String.

getString

public java.lang.String getString(org.pfsw.bif.text.IStringConstant name)
Returns the value for the given name as String (if it is one).

Parameters:
name - The name of the value to look for (must not be null).
Returns:
The found String or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no String.

getBoolean

public java.lang.Boolean getBoolean(java.lang.String name)
Returns the value for the given name as Boolean (if it is one).

Parameters:
name - The name of the value to look for (must not be null).
Returns:
The found Boolean or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no Boolean.

getBoolean

public java.lang.Boolean getBoolean(org.pfsw.bif.text.IStringConstant name)
Returns the value for the given name as Boolean (if it is one).

Parameters:
name - The name of the value to look for (must not be null).
Returns:
The found Boolean or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no Boolean.

getNumber

public java.lang.Number getNumber(java.lang.String name)
Returns the value for the given name as Number (if it is one).

Parameters:
name - The name of the value to look for (must not be null).
Returns:
The found Number or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no Number.

getNumber

public java.lang.Number getNumber(org.pfsw.bif.text.IStringConstant name)
Returns the value for the given name as Number (if it is one).

Parameters:
name - The name of the value to look for (must not be null).
Returns:
The found Number or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no Number.

getInteger

public java.lang.Integer getInteger(java.lang.String name)
Returns the value for the given name as Integer (if it is one).

Parameters:
name - The name of the value to look for (must not be null).
Returns:
The found Integer or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no Integer.

getInteger

public java.lang.Integer getInteger(org.pfsw.bif.text.IStringConstant name)
Returns the value for the given name as Integer (if it is one).

Parameters:
name - The name of the value to look for (must not be null).
Returns:
The found Integer or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no Integer.

getLong

public java.lang.Long getLong(java.lang.String name)
Returns the value for the given name as Long (if it is one).

Parameters:
name - The name of the value to look for (must not be null).
Returns:
The found Long or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no Long.

getLong

public java.lang.Long getLong(org.pfsw.bif.text.IStringConstant name)
Returns the value for the given name as Long (if it is one).

Parameters:
name - The name of the value to look for (must not be null).
Returns:
The found Long or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no Long.

getBigDecimal

public java.math.BigDecimal getBigDecimal(java.lang.String name)
Returns the value for the given name as BigDecimal (if it is one).

Parameters:
name - The name of the value to look for (must not be null).
Returns:
The found BigDecimal or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no BigDecimal.

getBigDecimal

public java.math.BigDecimal getBigDecimal(org.pfsw.bif.text.IStringConstant name)
Returns the value for the given name as BigDecimal (if it is one).

Parameters:
name - The name of the value to look for (must not be null).
Returns:
The found BigDecimal or null if not existent.
Throws:
java.lang.ClassCastException - If the object associated to the given name is no BigDecimal.

getValueOfType

public <T> T getValueOfType(java.lang.String name,
                            java.lang.Class<T> type)
Returns the value for the given name as type of the given class.

Parameters:
name - The name of the value to look for (must not be null).
Returns:
The found object or null if not existent.
Throws:
java.lang.IllegalArgumentException - If the specified type is no valid JSON type.
java.lang.ClassCastException - If the object associated to the given name is not of the specified type.

getValueOfType

public <T> T getValueOfType(org.pfsw.bif.text.IStringConstant name,
                            java.lang.Class<T> type)
Returns the value for the given name as type of the given class.

Parameters:
name - The name of the value to look for (must not be null).
Returns:
The found object or null if not existent.
Throws:
java.lang.IllegalArgumentException - If the specified type is no valid JSON type.
java.lang.ClassCastException - If the object associated to the given name is not of the specified type.

isObject

public boolean isObject()
Returns true because this is a JSON object representation (in Java).

Specified by:
isObject in interface JsonType

isArray

public boolean isArray()
Returns false because this is no JSON array representation (in Java).

Specified by:
isArray in interface JsonType

getNames

public java.util.Collection<java.lang.String> getNames()
Specified by:
getNames in interface org.pfsw.bif.facet.IReadOnlyNamedValues<java.lang.Object>

getValue

public java.lang.Object getValue(java.lang.String name)
Specified by:
getValue in interface org.pfsw.bif.facet.IReadOnlyNamedValues<java.lang.Object>

getValue

public java.lang.Object getValue(org.pfsw.bif.text.IStringConstant name)

asString

public java.lang.String asString()
Returns the JSON string representation of this object.

Specified by:
asString in interface org.pfsw.bif.text.IStringRepresentation

appendAsJSONString

public void appendAsJSONString(java.lang.Appendable output)
Specified by:
appendAsJSONString in interface org.pfsw.bif.text.IJSONConvertible

toJSON

public java.lang.String toJSON()
Specified by:
toJSON in interface org.pfsw.bif.text.IJSONConvertible

getTypedElement

protected <T> T getTypedElement(java.lang.String name,
                                java.lang.Class<T> type)