public class JsonObject extends java.util.LinkedHashMap<java.lang.String,java.lang.Object> implements JsonType, IReadOnlyNamedValues<java.lang.Object>
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 and Description |
---|
JsonObject() |
JsonObject(int initialCapacity) |
JsonObject(int initialCapacity,
float loadFactor) |
JsonObject(java.util.Map<? extends java.lang.String,? extends java.lang.Object> map) |
Modifier and Type | Method and Description |
---|---|
void |
appendAsJSONString(java.lang.Appendable output)
The receiver of this method must append its internal state as JSON string
representation to the given buffer.
|
java.lang.String |
asString()
Returns the JSON string representation of this object.
|
static JsonObject |
create()
Returns a new empty JSON object.
|
java.math.BigDecimal |
getBigDecimal(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(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(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(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(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(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()
Returns all names (maybe empty but never null).
|
java.lang.Number |
getNumber(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(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(IStringConstant name) |
java.lang.Object |
getValue(java.lang.String name)
Returns the value associated with the given name.
|
<T> T |
getValueOfType(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 |
hasField(IStringConstant name)
Returns whether or not this JSON object contains a field with the given name.
|
boolean |
hasField(java.lang.String name)
Returns whether or not this JSON object contains a field with the given name.
|
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(IStringConstant name,
java.lang.Object value)
Adds the given element if it is a valid JSON object type.
|
JsonObject |
setField(java.lang.String name,
java.lang.Object value)
Adds the given element if it is a valid JSON object type.
|
java.lang.String |
toJSON()
The receiver of this method returns its internal state as JSON string
representation.
|
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, removeEldestEntry, replaceAll, values
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, size
finalize, getClass, notify, notifyAll, wait, wait, wait
isEmpty
public JsonObject()
public JsonObject(int initialCapacity, float loadFactor)
public JsonObject(int initialCapacity)
public JsonObject(java.util.Map<? extends java.lang.String,? extends java.lang.Object> map)
public static JsonObject create()
public java.lang.Object put(java.lang.String key, java.lang.Object value)
Valid types are:
put
in interface java.util.Map<java.lang.String,java.lang.Object>
put
in class java.util.HashMap<java.lang.String,java.lang.Object>
java.lang.IllegalArgumentException
- If the given object is not of a valid JSON type.public JsonObject setField(java.lang.String name, java.lang.Object value)
Valid types are:
java.lang.IllegalArgumentException
- If the given object is not of a valid JSON type.public JsonObject setField(IStringConstant name, java.lang.Object value)
Valid types are:
java.lang.IllegalArgumentException
- If the given object is not of a valid JSON type.public JsonObject getJsonObject(java.lang.String name)
name
- The name of the object to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no JsonObject.public JsonObject getJsonObject(IStringConstant name)
name
- The name of the object to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no JsonObject.public JsonArray getJsonArray(java.lang.String name)
name
- The name of the array to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no JsonArray.public JsonArray getJsonArray(IStringConstant name)
name
- The name of the array to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no JsonArray.public java.lang.String getString(java.lang.String name)
name
- The name of the value to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no String.public java.lang.String getString(IStringConstant name)
name
- The name of the value to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no String.public java.lang.Boolean getBoolean(java.lang.String name)
name
- The name of the value to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no Boolean.public java.lang.Boolean getBoolean(IStringConstant name)
name
- The name of the value to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no Boolean.public java.lang.Number getNumber(java.lang.String name)
name
- The name of the value to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no Number.public java.lang.Number getNumber(IStringConstant name)
name
- The name of the value to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no Number.public java.lang.Integer getInteger(java.lang.String name)
name
- The name of the value to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no Integer.public java.lang.Integer getInteger(IStringConstant name)
name
- The name of the value to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no Integer.public java.lang.Long getLong(java.lang.String name)
name
- The name of the value to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no Long.public java.lang.Long getLong(IStringConstant name)
name
- The name of the value to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no Long.public java.math.BigDecimal getBigDecimal(java.lang.String name)
name
- The name of the value to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no BigDecimal.public java.math.BigDecimal getBigDecimal(IStringConstant name)
name
- The name of the value to look for (must not be null).java.lang.ClassCastException
- If the object associated to the given name is no BigDecimal.public <T> T getValueOfType(java.lang.String name, java.lang.Class<T> type)
name
- The name of the value to look for (must not be null).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.public <T> T getValueOfType(IStringConstant name, java.lang.Class<T> type)
name
- The name of the value to look for (must not be null).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.public boolean isObject()
public boolean isArray()
public java.util.Collection<java.lang.String> getNames()
IReadOnlyNamedValues
getNames
in interface IReadOnlyNamedValues<java.lang.Object>
public java.lang.Object getValue(java.lang.String name)
IReadOnlyNamedValues
getValue
in interface IReadOnlyNamedValues<java.lang.Object>
name
- The identifier for which the associated value must be returned (must not be null).public java.lang.Object getValue(IStringConstant name)
public boolean hasField(java.lang.String name)
public boolean hasField(IStringConstant name)
public java.lang.String asString()
asString
in interface IStringRepresentation
public void appendAsJSONString(java.lang.Appendable output)
IJSONConvertible
appendAsJSONString
in interface IJSONConvertible
output
- The output target to which to append the JSON string (must not be null).public java.lang.String toJSON()
IJSONConvertible
toJSON
in interface IJSONConvertible
protected <T> T getTypedElement(java.lang.String name, java.lang.Class<T> type)