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. Meaning all values added to a JsonObject must be recognized by the Jsoner for it to be a true 'JsonObject', so it is really a JsonableHashMap that will serialize to a JsonObject if all of its contents are valid JSON.
- See Also:
Nested Class Summary
Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final longThe serialization version this class is compatible with.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(JsonKey key) A convenience method that assumes there is a BigDecimal, Number, or String at the given key.A convenience method that assumes there is a BigDecimal, Number, or String at the given key.getBoolean(JsonKey key) A convenience method that assumes there is a BigDecimal, Number, or String at the given key.A convenience method that assumes there is a Boolean or String value at the given key.A convenience method that assumes there is a Boolean or String value at the given key.getByteOrDefault(JsonKey key) A convenience method that assumes there is a Number or String value at the given key.<T extends Collection<?>>
TgetCollection(JsonKey key) A convenience method that assumes there is a Number or String value at the given key.<T extends Collection<?>>
TA convenience method that assumes there is a Collection at the given key.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.A convenience method that assumes there is a Number or String value at the given key.getFloatOrDefault(JsonKey key) A convenience method that assumes there is a Number or String value at the given key.getInteger(JsonKey key) 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.A convenience method that assumes there is a Number or String value at the given key.getLongOrDefault(JsonKey key) 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 Number or String value at the given key.<T extends Map<?,?>>
TgetMapOrDefault(JsonKey key) A convenience method that assumes there is a Map at the given key.A convenience method that assumes there is a Map at the given key.getShortOrDefault(JsonKey key) 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.private StringgetStringOrDefault(String key, String defaultValue) A convenience method that assumes there is a Boolean, 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.voidrequireKeys(JsonKey... keys) Ensures the given keys are present.toJson()Serialize to a JSON formatted string.voidSerialize to a JSON formatted stream.Methods inherited from class HashMap
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, newHashMap, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, valuesMethods inherited from class AbstractMap
equals, hashCode, toString
Field Details
serialVersionUID
private static final long serialVersionUIDThe serialization version this class is compatible with. This value doesn't need to be incremented if and only if the only changes to occur were updating comments, updating javadocs, adding new fields to the class, changing the fields from static to non-static, or changing the fields from transient to non transient. All other changes require this number be incremented.- See Also:
Constructor Details
JsonObject
public JsonObject()Instantiates an empty JsonObject.JsonObject
Method Details
requireKeys
Ensures the given keys are present.- Parameters:
keys- represents the keys that must be present.- Throws:
NoSuchElementException- if any of the given keys are missing.
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 paired with.- Returns:
- a BigDecimal representing the value paired with 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 paired with.- Returns:
- a BigDecimal representing the value paired with the key or JsonKey#getValue() if the key isn't present.
- 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:
getBoolean
A convenience method that assumes there is a BigDecimal, Number, or String at the given key. If a Number is /** 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 paired with.- Returns:
- a Boolean representing the value paired with the key.
- Throws:
ClassCastException- if the value didn't match the assumed return type.- See Also:
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 paired with.- Returns:
- a Boolean representing the value paired with the key or JsonKey#getValue() if the key isn't present.
- Throws:
ClassCastException- if the value didn't match the assumed return type.- See Also:
getByte
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.- Parameters:
key- representing where the value ought to be paired with.- Returns:
- a Byte representing the value paired with 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 paired with.- Returns:
- a Byte representing the value paired with the key or JsonKey#getValue() if the key isn't present (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:
getCollection
A convenience method that assumes there is a Number or String value at the given 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 paired with.- Returns:
- a Collection representing the value paired with the key.
- Throws:
ClassCastException- if the value didn't match the assumed return type.- See Also:
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 paired with.- Returns:
- a Collection representing the value paired with the key or JsonKey#getValue() if the key isn't present..
- Throws:
ClassCastException- if the value didn't match the assumed return type.- See Also:
getDouble
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.- Parameters:
key- representing where the value ought to be paired with.- Returns:
- a Double representing the value paired with 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 paired with.- Returns:
- a Double representing the value paired with the key or JsonKey#getValue() if the key isn't present (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:
getFloat
A convenience method that assumes there is a Number or String value at the given key. private static boolean isValidClassName(String className) { if (className == null || className.isEmpty()) { return false; } if (className.contains("..") || className.contains("/") || className.contains("\\")) { return false; } return className.matches("^[a-zA-Z_][a-zA-Z0-9_$.]*$"); } /** 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 paired with.- Returns:
- a Float representing the value paired with 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 paired with.- Returns:
- a Float representing the value paired with the key or JsonKey#getValue() if the key isn't present (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:
getInteger
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.- Parameters:
key- representing where the value ought to be paired with.- Returns:
- an Integer representing the value paired with 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 paired with.- Returns:
- an Integer representing the value paired with the key or JsonKey#getValue() if the key isn't present (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:
getLong
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.- Parameters:
key- representing where the value ought to be paired with.- Returns:
- a Long representing the value paired with 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 paired with.- Returns:
- a Long representing the value paired with the key or JsonKey#getValue() if the key isn't present (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:
getMap
A convenience method that assumes there is a Number or String value at the given 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 paired with.- Returns:
- a Map representing the value paired with the key.
- Throws:
ClassCastException- if the value didn't match the assumed return type.- See Also:
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 paired with.- Returns:
- a Map representing the value paired with the key or JsonKey#getValue() if the key isn't present.
- Throws:
ClassCastException- if the value didn't match the assumed return type.- See Also:
getShort
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.- Parameters:
key- representing where the value ought to be paired with.- Returns:
- a Short representing the value paired with 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 paired with.- Returns:
- a Short representing the value paired with the key or JsonKey#getValue() if the key isn't present (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:
getString
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.- Parameters:
key- representing where the value ought to be paired with.- Returns:
- a String representing the value paired with the key.
- Throws:
ClassCastException- if the value didn't match the assumed return type.- See Also:
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 paired with.- Returns:
- a String representing the value paired with the key or JsonKey#getValue() if the key isn't present.
- Throws:
ClassCastException- if the value didn't match the assumed return type.- See Also:
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
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.