Package org.apache.camel.util.json
Class JsonObject
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Constructor Summary
ConstructorsConstructorDescriptionInstantiates 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
Modifier and TypeMethodDescriptiongetBigDecimal(String key) A convenience method that assumes there is a BigDecimal, Number, or String at the given key.getBigDecimalOrDefault(String key, BigDecimal defaultValue) A convenience method that assumes there is a BigDecimal, Number, or String at the given key.getBoolean(String key) A convenience method that assumes there is a Boolean or String value at the given key.getBooleanOrDefault(String key, boolean defaultValue) A convenience method that assumes there is a Boolean or String value at the given key.A convenience method that assumes there is a Number or String value at the given key.getByteOrDefault(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.A convenience method that assumes there is a Number or String value at the given key.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>>
TA 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.A convenience method that assumes there is a Number or String value at the given key.getFloatOrDefault(String key, float defaultValue) A convenience method that assumes there is a Number or String value at the given key.getInteger(String key) A convenience method that assumes there is a Number or String value at the given key.getIntegerOrDefault(String key, int defaultValue) A convenience method that assumes there is a Number or String value at the given key.A convenience method that assumes there is a Number or String value at the given key.getLongOrDefault(String key, long defaultValue) A convenience method that assumes there is a Number or String value at the given key.<T extends Map<?,?>>
TA 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.A convenience method that assumes there is a Number or String value at the given key.getShortOrDefault(String key, short defaultValue) A convenience method that assumes there is a Number or String value at the given key.A convenience method that assumes there is a Boolean, Number, or String value at the given key.getStringOrDefault(String key, String defaultValue) A convenience method that assumes there is a Boolean, Number, or String value at the given key.toJson()Serialize to a JSON formatted string.voidSerialize to a JSON formatted stream.Methods inherited from class java.util.LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, removeEldestEntry, replaceAll, valuesMethods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, sizeMethods inherited from class java.util.AbstractMap
equals, hashCode, toStringMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, equals, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
-
Constructor Details
-
JsonObject
public JsonObject()Instantiates an empty JsonObject. -
JsonObject
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 Details
-
getBigDecimal
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:
-
getBigDecimalOrDefault
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:
-
getBoolean
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
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
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:
-
getByteOrDefault
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:
-
getCollection
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
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
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:
-
getDoubleOrDefault
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:
-
getEnum
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:
-
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:
-
getFloat
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:
-
getFloatOrDefault
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:
-
getInteger
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:
-
getIntegerOrDefault
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:
-
getLong
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:
-
getLongOrDefault
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:
-
getMap
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
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
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:
-
getShortOrDefault
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:
-
getString
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
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
Description copied from interface:JsonableSerialize to a JSON formatted string. -
toJson
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.
-