public class JsonObject extends LinkedHashMap<String,Object> implements Jsonable
Jsoner
,
Serialized FormAbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Constructor and Description |
---|
JsonObject()
Instantiates an empty JsonObject.
|
JsonObject(Map<String,?> map)
Instantiate a new JsonObject by accepting a map's entries, which could lead to de/serialization issues of the
resulting JsonObject since the entry values aren't validated as JSON values.
|
Modifier and Type | Method and Description |
---|---|
BigDecimal |
getBigDecimal(String key)
A convenience method that assumes there is a BigDecimal, Number, or String at the given key.
|
BigDecimal |
getBigDecimalOrDefault(String key,
BigDecimal defaultValue)
A convenience method that assumes there is a BigDecimal, Number, or String at the given key.
|
Boolean |
getBoolean(String key)
A convenience method that assumes there is a Boolean or String value at the given key.
|
Boolean |
getBooleanOrDefault(String key,
boolean defaultValue)
A convenience method that assumes there is a Boolean or String value at the given key.
|
Byte |
getByte(String key)
A convenience method that assumes there is a Number or String value at the given key.
|
Byte |
getByteOrDefault(String key,
byte defaultValue)
A convenience method that assumes there is a Number or String value at the given key.
|
<T extends Collection<?>> |
getCollection(String key)
A convenience method that assumes there is a Collection at the given key.
|
<T extends Collection<?>> |
getCollectionOrDefault(String key,
T defaultValue)
A convenience method that assumes there is a Collection at the given key.
|
Double |
getDouble(String key)
A convenience method that assumes there is a Number or String value at the given key.
|
Double |
getDoubleOrDefault(String key,
double defaultValue)
A convenience method that assumes there is a Number or String value at the given key.
|
<T extends Enum<T>> |
getEnum(String key)
A convenience method that assumes there is a String value at the given key representing a fully qualified name
in
dot notation of an enum.
|
<T extends Enum<T>> |
getEnumOrDefault(String key,
T defaultValue)
A convenience method that assumes there is a String value at the given key representing a fully qualified name
in
dot notation of an enum.
|
Float |
getFloat(String key)
A convenience method that assumes there is a Number or String value at the given key.
|
Float |
getFloatOrDefault(String key,
float defaultValue)
A convenience method that assumes there is a Number or String value at the given key.
|
Integer |
getInteger(String key)
A convenience method that assumes there is a Number or String value at the given key.
|
Integer |
getIntegerOrDefault(String key,
int defaultValue)
A convenience method that assumes there is a Number or String value at the given key.
|
Long |
getLong(String key)
A convenience method that assumes there is a Number or String value at the given key.
|
Long |
getLongOrDefault(String key,
long defaultValue)
A convenience method that assumes there is a Number or String value at the given key.
|
<T extends Map<?,?>> |
getMap(String key)
A convenience method that assumes there is a Map at the given key.
|
<T extends Map<?,?>> |
getMapOrDefault(String key,
T defaultValue)
A convenience method that assumes there is a Map at the given key.
|
Short |
getShort(String key)
A convenience method that assumes there is a Number or String value at the given key.
|
Short |
getShortOrDefault(String key,
short defaultValue)
A convenience method that assumes there is a Number or String value at the given key.
|
String |
getString(String key)
A convenience method that assumes there is a Boolean, Number, or String value at the given key.
|
String |
getStringOrDefault(String key,
String defaultValue)
A convenience method that assumes there is a Boolean, Number, or String value at the given key.
|
String |
toJson()
Serialize to a JSON formatted string.
|
void |
toJson(Writer writable)
Serialize to a JSON formatted stream.
|
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, removeEldestEntry, replaceAll, values
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
equals, hashCode, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, containsKey, equals, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
public JsonObject()
public JsonObject(Map<String,?> map)
map
- represents the mappings to produce the JsonObject with.public BigDecimal getBigDecimal(String key)
key
- representing where the value ought to be stored at.ClassCastException
- if the value didn't match the assumed return type.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number
represents the double or float Infinity or NaN.BigDecimal
,
Object.toString()
public BigDecimal getBigDecimalOrDefault(String key, BigDecimal defaultValue)
key
- representing where the value ought to be stored at.defaultValue
- representing what is returned when the key isn't in the JsonObject.ClassCastException
- if there was a value but didn't match the assumed return types.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number
represents the double or float Infinity or NaN.BigDecimal
,
Object.toString()
public Boolean getBoolean(String key)
key
- representing where the value ought to be stored at.ClassCastException
- if the value didn't match the assumed return type.public Boolean getBooleanOrDefault(String key, boolean defaultValue)
key
- representing where the value ought to be stored at.defaultValue
- representing what is returned when the key isn't in the JsonObject.ClassCastException
- if there was a value but didn't match the assumed return type.public Byte getByte(String key)
key
- representing where the value ought to be stored at.ClassCastException
- if the value didn't match the assumed return type.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number
represents the double or float Infinity or NaN.Number.byteValue()
public Byte getByteOrDefault(String key, byte defaultValue)
key
- representing where the value ought to be stored at.defaultValue
- representing what is returned when the key isn't in the JsonObject.ClassCastException
- if there was a value but didn't match the assumed return type.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number
represents the double or float Infinity or NaN.Number.byteValue()
public <T extends Collection<?>> T getCollection(String key)
T
- the kind of collection to expect at the key. Note unless manually added, collection values will be a
JsonArray.key
- representing where the value ought to be stored at.ClassCastException
- if the value didn't match the assumed return type.public <T extends Collection<?>> T getCollectionOrDefault(String key, T defaultValue)
T
- the kind of collection to expect at the key. Note unless manually added, collection values will be a
JsonArray.key
- representing where the value ought to be stored at.defaultValue
- representing what is returned when the key isn't in the JsonObject.ClassCastException
- if there was a value but didn't match the assumed return type.public Double getDouble(String key)
key
- representing where the value ought to be stored at.ClassCastException
- if the value didn't match the assumed return type.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number
represents the double or float Infinity or NaN.Number.doubleValue()
public Double getDoubleOrDefault(String key, double defaultValue)
key
- representing where the value ought to be stored at.defaultValue
- representing what is returned when the key isn't in the JsonObject.ClassCastException
- if there was a value but didn't match the assumed return type.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number
represents the double or float Infinity or NaN.Number.doubleValue()
public <T extends Enum<T>> T getEnum(String key) throws ClassNotFoundException
T
- the Enum type the value at the key is expected to belong to.key
- representing where the value ought to be stored at.ClassNotFoundException
- if the value was a String but the declaring enum type couldn't be determined with
it.ClassCastException
- if the element at the index was not a String or if the fully qualified enum name is of
the wrong type.IllegalArgumentException
- if an enum type was determined but it doesn't define an enum with the determined
name.Enum.valueOf(Class, String)
public <T extends Enum<T>> T getEnumOrDefault(String key, T defaultValue) throws ClassNotFoundException
T
- the Enum type the value at the key is expected to belong to.key
- representing where the value ought to be stored at.defaultValue
- representing what is returned when the key isn't in the JsonObject.ClassNotFoundException
- if the value was a String but the declaring enum type couldn't be determined with
it.ClassCastException
- if the element at the index was not a String or if the fully qualified enum name is of
the wrong type.IllegalArgumentException
- if an enum type was determined but it doesn't define an enum with the determined
name.Enum.valueOf(Class, String)
public Float getFloat(String key)
key
- representing where the value ought to be stored at.ClassCastException
- if the value didn't match the assumed return type.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number
represents the double or float Infinity or NaN.Number.floatValue()
public Float getFloatOrDefault(String key, float defaultValue)
key
- representing where the value ought to be stored at.defaultValue
- representing what is returned when the key isn't in the JsonObject.ClassCastException
- if there was a value but didn't match the assumed return type.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number
represents the double or float Infinity or NaN.Number.floatValue()
public Integer getInteger(String key)
key
- representing where the value ought to be stored at.ClassCastException
- if the value didn't match the assumed return type.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number
represents the double or float Infinity or NaN.Number.intValue()
public Integer getIntegerOrDefault(String key, int defaultValue)
key
- representing where the value ought to be stored at.defaultValue
- representing what is returned when the key isn't in the JsonObject.ClassCastException
- if there was a value but didn't match the assumed return type.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number
represents the double or float Infinity or NaN.Number.intValue()
public Long getLong(String key)
key
- representing where the value ought to be stored at.ClassCastException
- if the value didn't match the assumed return type.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number
represents the double or float Infinity or NaN.Number.longValue()
public Long getLongOrDefault(String key, long defaultValue)
key
- representing where the value ought to be stored at.defaultValue
- representing what is returned when the key isn't in the JsonObject.ClassCastException
- if there was a value but didn't match the assumed return type.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number
represents the double or float Infinity or NaN.Number.longValue()
public <T extends Map<?,?>> T getMap(String key)
T
- the kind of map to expect at the key. Note unless manually added, Map values will be a JsonObject.key
- representing where the value ought to be stored at.ClassCastException
- if the value didn't match the assumed return type.public <T extends Map<?,?>> T getMapOrDefault(String key, T defaultValue)
T
- the kind of map to expect at the key. Note unless manually added, Map values will be a JsonObject.key
- representing where the value ought to be stored at.defaultValue
- representing what is returned when the key isn't in the JsonObject.ClassCastException
- if there was a value but didn't match the assumed return type.public Short getShort(String key)
key
- representing where the value ought to be stored at.ClassCastException
- if the value didn't match the assumed return type.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number
represents the double or float Infinity or NaN.Number.shortValue()
public Short getShortOrDefault(String key, short defaultValue)
key
- representing where the value ought to be stored at.defaultValue
- representing what is returned when the key isn't in the JsonObject.ClassCastException
- if there was a value but didn't match the assumed return type.NumberFormatException
- if a String isn't a valid representation of a BigDecimal or if the Number
represents the double or float Infinity or NaN.Number.shortValue()
public String getString(String key)
key
- representing where the value ought to be stored at.ClassCastException
- if the value didn't match the assumed return type.public String getStringOrDefault(String key, String defaultValue)
key
- representing where the value ought to be stored at.defaultValue
- representing what is returned when the key isn't in the JsonObject.ClassCastException
- if there was a value but didn't match the assumed return type.public String toJson()
Jsonable
public void toJson(Writer writable) throws IOException
Jsonable
toJson
in interface Jsonable
writable
- where the resulting JSON text should be sent.IOException
- when the writable encounters an I/O error.Apache Camel