Package org.apache.camel.util.json
Class JsonObject
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.HashMap<K,V>
-
- java.util.LinkedHashMap<String,Object>
-
- org.apache.camel.util.json.JsonObject
-
public class JsonObject extends LinkedHashMap<String,Object> implements Jsonable
JsonObject is a common non-thread safe data format for string to data mappings. The contents of a JsonObject are only validated as JSON values on serialization.- Since:
- 2.0.0
- See Also:
Jsoner, Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BigDecimalgetBigDecimal(String key)A convenience method that assumes there is a BigDecimal, Number, or String at the given key.BigDecimalgetBigDecimalOrDefault(String key, BigDecimal defaultValue)A convenience method that assumes there is a BigDecimal, Number, or String at the given key.BooleangetBoolean(String key)A convenience method that assumes there is a Boolean or String value at the given key.BooleangetBooleanOrDefault(String key, boolean defaultValue)A convenience method that assumes there is a Boolean or String value at the given key.BytegetByte(String key)A convenience method that assumes there is a Number or String value at the given key.BytegetByteOrDefault(String key, byte defaultValue)A convenience method that assumes there is a Number or String value at the given key.<T extends Collection<?>>
TgetCollection(String key)A convenience method that assumes there is a Collection at the given key.<T extends Collection<?>>
TgetCollectionOrDefault(String key, T defaultValue)A convenience method that assumes there is a Collection at the given key.DoublegetDouble(String key)A convenience method that assumes there is a Number or String value at the given key.DoublegetDoubleOrDefault(String key, double defaultValue)A convenience method that assumes there is a Number or String value at the given key.<T extends Enum<T>>
TgetEnum(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>>
TgetEnumOrDefault(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.FloatgetFloat(String key)A convenience method that assumes there is a Number or String value at the given key.FloatgetFloatOrDefault(String key, float defaultValue)A convenience method that assumes there is a Number or String value at the given key.IntegergetInteger(String key)A convenience method that assumes there is a Number or String value at the given key.IntegergetIntegerOrDefault(String key, int defaultValue)A convenience method that assumes there is a Number or String value at the given key.LonggetLong(String key)A convenience method that assumes there is a Number or String value at the given key.LonggetLongOrDefault(String key, long defaultValue)A convenience method that assumes there is a Number or String value at the given key.<T extends Map<?,?>>
TgetMap(String key)A convenience method that assumes there is a Map at the given key.<T extends Map<?,?>>
TgetMapOrDefault(String key, T defaultValue)A convenience method that assumes there is a Map at the given key.ShortgetShort(String key)A convenience method that assumes there is a Number or String value at the given key.ShortgetShortOrDefault(String key, short defaultValue)A convenience method that assumes there is a Number or String value at the given key.StringgetString(String key)A convenience method that assumes there is a Boolean, Number, or String value at the given key.StringgetStringOrDefault(String key, String defaultValue)A convenience method that assumes there is a Boolean, Number, or String value at the given key.StringtoJson()Serialize to a JSON formatted string.voidtoJson(Writer writable)Serialize to a JSON formatted stream.-
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, removeEldestEntry, replaceAll, values
-
Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
-
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 java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, equals, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
-
-
-
-
Constructor Detail
-
JsonObject
public JsonObject()
Instantiates an empty JsonObject.
-
JsonObject
public 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.- Parameters:
map- represents the mappings to produce the JsonObject with.
-
-
Method Detail
-
getBigDecimal
public BigDecimal getBigDecimal(String key)
A convenience method that assumes there is a BigDecimal, Number, or String at the given key. If a Number is there its Number#toString() is used to construct a new BigDecimal(String). If a String is there it is used to construct a new BigDecimal(String).- Parameters:
key- representing where the value ought to be stored at.- Returns:
- the value stored at the key.
- Throws:
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.- See Also:
BigDecimal,Object.toString()
-
getBigDecimalOrDefault
public BigDecimal getBigDecimalOrDefault(String key, BigDecimal defaultValue)
A convenience method that assumes there is a BigDecimal, Number, or String at the given key. If a Number is there its Number#toString() is used to construct a new BigDecimal(String). If a String is there it is used to construct a new BigDecimal(String).- Parameters:
key- representing where the value ought to be stored at.defaultValue- representing what is returned when the key isn't in the JsonObject.- Returns:
- the value stored at the key or the default provided if the key doesn't exist.
- Throws:
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.- See Also:
BigDecimal,Object.toString()
-
getBoolean
public Boolean getBoolean(String key)
A convenience method that assumes there is a Boolean or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.- Returns:
- the value stored at the key.
- Throws:
ClassCastException- if the value didn't match the assumed return type.
-
getBooleanOrDefault
public Boolean getBooleanOrDefault(String key, boolean defaultValue)
A convenience method that assumes there is a Boolean or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.defaultValue- representing what is returned when the key isn't in the JsonObject.- Returns:
- the value stored at the key or the default provided if the key doesn't exist.
- Throws:
ClassCastException- if there was a value but didn't match the assumed return type.
-
getByte
public Byte getByte(String key)
A convenience method that assumes there is a Number or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.- Returns:
- the value stored at the key (which may involve rounding or truncation).
- Throws:
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.- See Also:
Number.byteValue()
-
getByteOrDefault
public Byte getByteOrDefault(String key, byte defaultValue)
A convenience method that assumes there is a Number or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.defaultValue- representing what is returned when the key isn't in the JsonObject.- Returns:
- the value stored at the key (which may involve rounding or truncation) or the default provided if the key doesn't exist.
- Throws:
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.- See Also:
Number.byteValue()
-
getCollection
public <T extends Collection<?>> T getCollection(String key)
A convenience method that assumes there is a Collection at the given key.- Type Parameters:
T- the kind of collection to expect at the key. Note unless manually added, collection values will be a JsonArray.- Parameters:
key- representing where the value ought to be stored at.- Returns:
- the value stored at the key.
- Throws:
ClassCastException- if the value didn't match the assumed return type.
-
getCollectionOrDefault
public <T extends Collection<?>> T getCollectionOrDefault(String key, T defaultValue)
A convenience method that assumes there is a Collection at the given key.- Type Parameters:
T- the kind of collection to expect at the key. Note unless manually added, collection values will be a JsonArray.- Parameters:
key- representing where the value ought to be stored at.defaultValue- representing what is returned when the key isn't in the JsonObject.- Returns:
- the value stored at the key or the default provided if the key doesn't exist.
- Throws:
ClassCastException- if there was a value but didn't match the assumed return type.
-
getDouble
public Double getDouble(String key)
A convenience method that assumes there is a Number or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.- Returns:
- the value stored at the key (which may involve rounding or truncation).
- Throws:
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.- See Also:
Number.doubleValue()
-
getDoubleOrDefault
public Double getDoubleOrDefault(String key, double defaultValue)
A convenience method that assumes there is a Number or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.defaultValue- representing what is returned when the key isn't in the JsonObject.- Returns:
- the value stored at the key (which may involve rounding or truncation) or the default provided if the key doesn't exist.
- Throws:
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.- See Also:
Number.doubleValue()
-
getEnum
public <T extends Enum<T>> T getEnum(String key) throws ClassNotFoundException
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.- Type Parameters:
T- the Enum type the value at the key is expected to belong to.- Parameters:
key- representing where the value ought to be stored at.- Returns:
- the enum based on the string found at the key, or null if the value paired with the provided key is null.
- Throws:
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.- See Also:
Enum.valueOf(Class, String)
-
getEnumOrDefault
public <T extends Enum<T>> T getEnumOrDefault(String key, T defaultValue) throws ClassNotFoundException
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.- Type Parameters:
T- the Enum type the value at the key is expected to belong to.- Parameters:
key- representing where the value ought to be stored at.defaultValue- representing what is returned when the key isn't in the JsonObject.- Returns:
- the enum based on the string found at the key, or the defaultValue provided if the key doesn't exist, or null if the value paired with provided key is null.
- Throws:
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.- See Also:
Enum.valueOf(Class, String)
-
getFloat
public Float getFloat(String key)
A convenience method that assumes there is a Number or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.- Returns:
- the value stored at the key (which may involve rounding or truncation).
- Throws:
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.- See Also:
Number.floatValue()
-
getFloatOrDefault
public Float getFloatOrDefault(String key, float defaultValue)
A convenience method that assumes there is a Number or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.defaultValue- representing what is returned when the key isn't in the JsonObject.- Returns:
- the value stored at the key (which may involve rounding or truncation) or the default provided if the key doesn't exist.
- Throws:
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.- See Also:
Number.floatValue()
-
getInteger
public Integer getInteger(String key)
A convenience method that assumes there is a Number or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.- Returns:
- the value stored at the key (which may involve rounding or truncation).
- Throws:
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.- See Also:
Number.intValue()
-
getIntegerOrDefault
public Integer getIntegerOrDefault(String key, int defaultValue)
A convenience method that assumes there is a Number or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.defaultValue- representing what is returned when the key isn't in the JsonObject.- Returns:
- the value stored at the key (which may involve rounding or truncation) or the default provided if the key doesn't exist.
- Throws:
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.- See Also:
Number.intValue()
-
getLong
public Long getLong(String key)
A convenience method that assumes there is a Number or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.- Returns:
- the value stored at the key (which may involve rounding or truncation).
- Throws:
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.- See Also:
Number.longValue()
-
getLongOrDefault
public Long getLongOrDefault(String key, long defaultValue)
A convenience method that assumes there is a Number or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.defaultValue- representing what is returned when the key isn't in the JsonObject.- Returns:
- the value stored at the key (which may involve rounding or truncation) or the default provided if the key doesn't exist.
- Throws:
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.- See Also:
Number.longValue()
-
getMap
public <T extends Map<?,?>> T getMap(String key)
A convenience method that assumes there is a Map at the given key.- Type Parameters:
T- the kind of map to expect at the key. Note unless manually added, Map values will be a JsonObject.- Parameters:
key- representing where the value ought to be stored at.- Returns:
- the value stored at the key.
- Throws:
ClassCastException- if the value didn't match the assumed return type.
-
getMapOrDefault
public <T extends Map<?,?>> T getMapOrDefault(String key, T defaultValue)
A convenience method that assumes there is a Map at the given key.- Type Parameters:
T- the kind of map to expect at the key. Note unless manually added, Map values will be a JsonObject.- Parameters:
key- representing where the value ought to be stored at.defaultValue- representing what is returned when the key isn't in the JsonObject.- Returns:
- the value stored at the key or the default provided if the key doesn't exist.
- Throws:
ClassCastException- if there was a value but didn't match the assumed return type.
-
getShort
public Short getShort(String key)
A convenience method that assumes there is a Number or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.- Returns:
- the value stored at the key (which may involve rounding or truncation).
- Throws:
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.- See Also:
Number.shortValue()
-
getShortOrDefault
public Short getShortOrDefault(String key, short defaultValue)
A convenience method that assumes there is a Number or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.defaultValue- representing what is returned when the key isn't in the JsonObject.- Returns:
- the value stored at the key (which may involve rounding or truncation) or the default provided if the key doesn't exist.
- Throws:
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.- See Also:
Number.shortValue()
-
getString
public String getString(String key)
A convenience method that assumes there is a Boolean, Number, or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.- Returns:
- the value stored at the key.
- Throws:
ClassCastException- if the value didn't match the assumed return type.
-
getStringOrDefault
public String getStringOrDefault(String key, String defaultValue)
A convenience method that assumes there is a Boolean, Number, or String value at the given key.- Parameters:
key- representing where the value ought to be stored at.defaultValue- representing what is returned when the key isn't in the JsonObject.- Returns:
- the value stored at the key or the default provided if the key doesn't exist.
- Throws:
ClassCastException- if there was a value but didn't match the assumed return type.
-
toJson
public String toJson()
Description copied from interface:JsonableSerialize to a JSON formatted string.
-
toJson
public void toJson(Writer writable) throws IOException
Description copied from interface:JsonableSerialize to a JSON formatted stream.- Specified by:
toJsonin interfaceJsonable- Parameters:
writable- where the resulting JSON text should be sent.- Throws:
IOException- when the writable encounters an I/O error.
-
-