Class JsonMap
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<String,
Object>
- Direct Known Subclasses:
Args
,DelegateMap
,ManifestFile
,ResolvingJsonMap
An extension of LinkedHashMap
, so all methods available in that class are also available to this class.
Note that the use of this class is optional for generating JSON. The serializers will accept any objects that implement the
Map
interface. But this class provides some useful additional functionality when working with
JSON models constructed from Java Collections Framework objects. For example, a constructor is provided for
converting a JSON object string directly into a Map
. It also contains accessor methods for to avoid common
typecasting when accessing elements in a list.
Example:
Notes:
- This class is not thread safe.
- See Also:
-
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> -
Field Summary
-
Constructor Summary
ConstructorDescriptionJsonMap()
Construct an empty map.Construct a map initialized with the specified reader containing JSON.Construct a map initialized with the specified string.JsonMap
(CharSequence json) Construct a map initialized with the specified JSON.JsonMap
(CharSequence in, Parser p) Construct a map initialized with the specified string.Construct a map initialized with the specified key/value pairs.Construct a map initialized with the specified map.JsonMap
(BeanSession session) Construct an empty map with the specified bean context. -
Method Summary
Modifier and TypeMethodDescriptionAdds an entry to this map.Appends all the entries in the specified map to this map.final <T> JsonMap
appendFirst
(Predicate<T> test, String key, T... values) Adds the first value that matches the specified predicate.Add if flag istrue .<T> JsonMap
Add if predicate matches value.appendIfAbsent
(String key, Object value) Adds a value in this map if the entry does not exist or the current value isnull .<T> JsonMap
appendIfAbsentIf
(Predicate<T> predicate, String key, T value) Adds a value in this map if the entry does not exist or the current value isnull and the value matches the specified predicate.asJson()
A synonym fortoString()
Serialize this object to Simplified JSON usingJson5Serializer.DEFAULT_READABLE
.asString()
Serialize this object to Simplified JSON usingJson5Serializer.DEFAULT
.asString
(WriterSerializer serializer) Serialize this object into a string using the specified serializer.<T> T
Converts this map into an object of the specified type.<T> T
Same ascast(Class)
, except allows you to specify aClassMeta
parameter.boolean
containsKey
(Object key) boolean
Returnstrue if the map contains the specified entry and the value is not null nor an empty string.boolean
containsOuterKey
(Object key) Returnstrue if this map contains the specified key, ignoring the inner map if it exists.static JsonMap
create()
Construct an empty map.Similar toremove(Object)
, but the key is a slash-delimited path used to traverse entries in this POJO.entrySet()
Returns a copy of thisJsonMap without the specified keys.filtered()
Enables filtering based on default values.Enables filtering based on a predicate test.static JsonMap
Construct an empty map.static JsonMap
filteredMap
(Object... keyValuePairs) Construct a map initialized with the specified key/value pairs.<T> T
Returns the value for the first key in the list that has an entry in this map.Returns the value for the first key in the list that has an entry in this map.findBoolean
(String... keys) Returns the first entry that exists converted to aBoolean
.Returns the first entry that exists converted to anInteger
.findKeyIgnoreCase
(String key) Searches for the specified key in this map ignoring case.Returns the first entry that exists converted to aJsonList
.Returns the first entry that exists converted to aLong
.Returns the first entry that exists converted to aJsonMap
.findString
(String... keys) Returns the first entry that exists converted to aString
.<T> T
Same asget()
, but casts or converts the value to the specified class type.<T> T
Same asget(String,Class)
, but allows for complex data types consisting of collections or maps.<T> T
Same asget(String,Class)
, but the key is a slash-delimited path used to traverse entries in this POJO.<T> T
Same asgetAt(String,Class)
, but allows for conversion to complex maps and collections.Returns theBeanSession
currently associated with this map.getBoolean
(String key) Returns the specified entry value converted to aBoolean
.getBoolean
(String key, Boolean defVal) Returns the specified entry value converted to aBoolean
.ClassMeta<?>
getClassMeta
(String key) Returns the class type of the object at the specified index.Returns the first key in the map.Returns the specified entry value converted to anInteger
.Returns the specified entry value converted to anInteger
.Returns the specified entry value converted to aJsonList
.Same asgetList(String)
but creates a new emptyJsonList
if it doesn't already exist.<E> List<E>
Same asgetList(String, JsonList)
except converts the elements to the specified types.Returns the specified entry value converted to aJsonList
.Returns the specified entry value converted to aLong
.Returns the specified entry value converted to aLong
.Returns the specified entry value converted to aMap
.Same asgetMap(String)
but creates a new emptyJsonMap
if it doesn't already exist.<K,
V> Map<K, V> Same asgetMap(String, JsonMap)
except converts the keys and values to the specified types.Returns the specified entry value converted to aJsonMap
.Returns the specified entry value converted to aString
.Returns the specified entry value converted to aString
.String[]
getStringArray
(String key) Returns the specified entry value converted to aString
.String[]
getStringArray
(String key, String[] def) Same asgetStringArray(String)
but returns a default value if the value cannot be found.<T> T
getSwapped
(String key, ObjectSwap<T, ?> objectSwap) Same asget()
, but converts the raw value to the specified class type using the specified POJO swap.getWithDefault
(String key, Object def) Same asget()
, but returns the default value if the key could not be found.<T> T
getWithDefault
(String key, T def, Class<T> type) Same asget(String,Class)
but returns a default value if the value does not exist.<T> T
getWithDefault
(String key, T def, Type type, Type... args) Same asget(String,Type,Type...)
but returns a default value if the value does not exist.Returns a copy of thisJsonMap with only the specified keys.Set an inner map in this map to allow for chained get calls.boolean
Returnstrue if this map is unmodifiable.The opposite ofremoveAll(String...)
.keySet()
Returns a modifiable copy of this map if it's unmodifiable.static JsonMap
Construct a map initialized with the specified key/value pairs.static JsonMap
Construct a map initialized with the specified map.static JsonMap
Construct a map initialized with the specified reader containing JSON.static JsonMap
ofJson
(CharSequence json) Construct a map initialized with the specified JSON string.static JsonMap
Construct a map initialized with the specified string.static JsonMap
ofText
(CharSequence in, Parser p) Construct a map initialized with the specified string.Similar toputAt(String,Object)
, but used to append to collections and arrays.Same asput(String,Object) , but the key is a slash-delimited path used to traverse entries in this POJO.void
Convenience method for inserting JSON directly into an attribute on this object.void
Convenience method for removing several keys at once.void
removeAll
(Collection<String> keys) Convenience method for removing several keys at once.removeBoolean
(String key) Equivalent to callingremoveWithDefault(key,
.null ,Boolean.class )removeBoolean
(String key, Boolean def) Equivalent to callingremoveWithDefault(key,def,Boolean.
.class )Equivalent to callingremoveWithDefault(key,
.null ,Integer.class )Equivalent to callingremoveWithDefault(key,def,Integer.
.class )removeString
(String key) Equivalent to callingremoveWithDefault(key,
.null ,String.class )removeString
(String key, String def) Equivalent to callingremoveWithDefault(key,def,String.
.class )<T> T
removeWithDefault
(String key, T defVal, Class<T> type) Equivalent to callingget(class,key,def) followed byremove(key); session
(BeanSession session) Override the default bean session used for converting POJOs.setBeanSession
(BeanSession value) Sets theBeanSession
currently associated with this map.toString()
Returns an unmodifiable copy of this map if it's modifiable.Convenience method for serializing this map to the specifiedWriter using theJsonSerializer.DEFAULT
serializer.Methods inherited from class java.util.LinkedHashMap
clear, containsValue, forEach, getOrDefault, removeEldestEntry, replaceAll, values
Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, size
Methods inherited from class java.util.AbstractMap
equals, hashCode
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, hashCode, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, size
-
Field Details
-
EMPTY_MAP
An empty read-only JsonMap.
-
-
Constructor Details
-
JsonMap
public JsonMap()Construct an empty map. -
JsonMap
Construct an empty map with the specified bean context.- Parameters:
session
- The bean session to use for creating beans.
-
JsonMap
Construct a map initialized with the specified map.- Parameters:
in
- The map to copy.
Can benull .
Keys will be converted to strings usingObject.toString()
.
-
JsonMap
Construct a map initialized with the specified JSON.- Parameters:
json
- The JSON text to parse.
Can be normal or simplified JSON.- Throws:
ParseException
- Malformed input encountered.
-
JsonMap
Construct a map initialized with the specified string.- Parameters:
in
- The input being parsed.
Can benull .p
- The parser to use to parse the input.
Ifnull , usesJsonParser
.- Throws:
ParseException
- Malformed input encountered.
-
JsonMap
Construct a map initialized with the specified reader containing JSON.- Parameters:
json
- The reader containing JSON text to parse.
Can contain normal or simplified JSON.- Throws:
ParseException
- Malformed input encountered.
-
JsonMap
Construct a map initialized with the specified string.- Parameters:
in
- The reader containing the input being parsed.
Can contain normal or simplified JSON.p
- The parser to use to parse the input.
Ifnull , usesJsonParser
.- Throws:
ParseException
- Malformed input encountered.
-
JsonMap
Construct a map initialized with the specified key/value pairs.Examples:
JsonMap
map =new JsonMap("key1" ,"val1" ,"key2" ,"val2" );- Parameters:
keyValuePairs
- A list of key/value pairs to add to this map.
-
-
Method Details
-
create
Construct an empty map.- Returns:
- An empty map.
-
filteredMap
Construct an empty map.- Returns:
- An empty map.
-
of
Construct a map initialized with the specified map.- Parameters:
values
- The map to copy.
Can benull .
Keys will be converted to strings usingObject.toString()
.- Returns:
- A new map or
null if the map wasnull .
-
ofJson
Construct a map initialized with the specified JSON string.- Parameters:
json
- The JSON text to parse.
Can be normal or simplified JSON.- Returns:
- A new map or
null if the string was null. - Throws:
ParseException
- Malformed input encountered.
-
ofText
Construct a map initialized with the specified string.- Parameters:
in
- The input being parsed.
Can benull .p
- The parser to use to parse the input.
Ifnull , usesJsonParser
.- Returns:
- A new map or
null if the input wasnull . - Throws:
ParseException
- Malformed input encountered.
-
ofJson
Construct a map initialized with the specified reader containing JSON.- Parameters:
json
- The reader containing JSON text to parse.
Can contain normal or simplified JSON.- Returns:
- A new map or
null if the input wasnull . - Throws:
ParseException
- Malformed input encountered.
-
ofText
Construct a map initialized with the specified string.- Parameters:
in
- The reader containing the input being parsed.
Can contain normal or simplified JSON.p
- The parser to use to parse the input.
Ifnull , usesJsonParser
.- Returns:
- A new map or
null if the input wasnull . - Throws:
ParseException
- Malformed input encountered.
-
of
Construct a map initialized with the specified key/value pairs.Examples:
JsonMap
map =new JsonMap("key1" ,"val1" ,"key2" ,"val2" );- Parameters:
keyValuePairs
- A list of key/value pairs to add to this map.- Returns:
- A new map, never
null .
-
filteredMap
Construct a map initialized with the specified key/value pairs.Same as
of(Object...)
but callsfiltered()
on the created map.Examples:
JsonMap
map =new JsonMap("key1" ,"val1" ,"key2" ,"val2" );- Parameters:
keyValuePairs
- A list of key/value pairs to add to this map.- Returns:
- A new map, never
null .
-
inner
Set an inner map in this map to allow for chained get calls.If
get(Object)
returnsnull , thenget(Object)
will be called on the inner map.In addition to providing the ability to chain maps, this method also provides the ability to wrap an existing map inside another map so that you can add entries to the outer map without affecting the values on the inner map.
JsonMap
map1 = JsonMap.ofJson ("{foo:1}" ); JsonMapmap2 = JsonMap.of ().setInner(map1 );map2 .put("foo" , 2);// Overwrite the entry int foo1 =map1 .getInt("foo" );// foo1 == 1 int foo2 =map2 .getInt("foo" );// foo2 == 2 - Parameters:
inner
- The inner map. Can benull to remove the inner map from an existing map.- Returns:
- This object.
-
session
Override the default bean session used for converting POJOs.Default is
BeanContext.DEFAULT
, which is sufficient in most cases.Useful if you're serializing/parsing beans with transforms defined.
- Parameters:
session
- The new bean session.- Returns:
- This object.
-
append
Adds an entry to this map.- Parameters:
key
- The key.value
- The value.- Returns:
- This object.
-
append
Appends all the entries in the specified map to this map.- Parameters:
values
- The map to copy. Can benull .- Returns:
- This object.
-
appendIf
Add if flag istrue .- Parameters:
flag
- The flag to check.key
- The key.value
- The value.- Returns:
- This object.
-
appendIf
Add if predicate matches value.- Type Parameters:
T
- The value type.- Parameters:
test
- The predicate to match against.key
- The key.value
- The value.- Returns:
- This object.
-
appendFirst
Adds the first value that matches the specified predicate.- Type Parameters:
T
- The value types.- Parameters:
test
- The predicate to match against.key
- The key.values
- The values to test.- Returns:
- This object.
-
appendIfAbsent
Adds a value in this map if the entry does not exist or the current value isnull .- Parameters:
key
- The map key.value
- The value to set if the current value does not exist or isnull .- Returns:
- This object.
-
appendIfAbsentIf
Adds a value in this map if the entry does not exist or the current value isnull and the value matches the specified predicate.- Type Parameters:
T
- The value type.- Parameters:
predicate
- The predicate to test the value with.key
- The map key.value
- The value to set if the current value does not exist or isnull .- Returns:
- This object.
-
filtered
Enables filtering based on default values.Any of the following types will be ignored when set as values in this map:
null false -1 (any Number type)- Empty arrays/collections/maps.
- Returns:
- This object.
-
filtered
Enables filtering based on a predicate test.If the predicate evaluates to
false on values added to this map, the entry will be skipped.- Parameters:
value
- The value tester predicate.- Returns:
- This object.
-
get
Same asget()
, but casts or converts the value to the specified class type.This is the preferred get method for simple types.
Examples:
JsonMap
map = JsonMap.ofJson ("..." );// Value converted to a string. Stringstring =map .get("key1" , String.class );// Value converted to a bean. MyBeanbean =map .get("key2" , MyBean.class );// Value converted to a bean array. MyBean[]beanArray =map .get("key3" , MyBean[].class );// Value converted to a linked-list of objects. Listlist =map .get("key4" , LinkedList.class );// Value converted to a map of object keys/values. Mapmap2 =map .get("key5" , TreeMap.class );See
BeanSession.convertToType(Object, ClassMeta)
for the list of valid data conversions.- Type Parameters:
T
- The class type returned.- Parameters:
key
- The key.type
- The class type returned.- Returns:
- The value, or
null if the entry doesn't exist.
-
get
Same asget(String,Class)
, but allows for complex data types consisting of collections or maps.The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).
Examples:
JsonMap
map = JsonMap.ofJson ("..." );// Value converted to a linked-list of strings. List<String>list1 =map .get("key1" , LinkedList.class , String.class );// Value converted to a linked-list of beans. List<MyBean>list2 =map .get("key2" , LinkedList.class , MyBean.class );// Value converted to a linked-list of linked-lists of strings. List<List<String>>list3 =map .get("key3" , LinkedList.class , LinkedList.class , String.class );// Value converted to a map of string keys/values. Map<String,String>map1 =map .get("key4" , TreeMap.class , String.class , String.class );// Value converted to a map containing string keys and values of lists containing beans. Map<String,List<MyBean>>map2 =map .get("key5" , TreeMap.class , String.class , List.class , MyBean.class );Collection classes are assumed to be followed by zero or one objects indicating the element type.Map classes are assumed to be followed by zero or two meta objects indicating the key and value types.The array can be arbitrarily long to indicate arbitrarily complex data structures.
See
BeanSession.convertToType(Object, ClassMeta)
for the list of valid data conversions.Notes:
-
Use the
get(String, Class)
method instead if you don't need a parameterized map/collection.
- Type Parameters:
T
- The class type returned.- Parameters:
key
- The key.type
- The class type returned.args
- The class type parameters.- Returns:
- The value, or
null if the entry doesn't exist.
-
Use the
-
getWithDefault
Same asget()
, but returns the default value if the key could not be found.- Parameters:
key
- The key.def
- The default value if the entry doesn't exist.- Returns:
- The value, or the default value if the entry doesn't exist.
-
getWithDefault
Same asget(String,Class)
but returns a default value if the value does not exist.- Type Parameters:
T
- The class type returned.- Parameters:
key
- The key.def
- The default value. Can benull .type
- The class type returned.- Returns:
- The value, or
null if the entry doesn't exist.
-
getWithDefault
Same asget(String,Type,Type...)
but returns a default value if the value does not exist.- Type Parameters:
T
- The class type returned.- Parameters:
key
- The key.def
- The default value. Can benull .type
- The class type returned.args
- The class type parameters.- Returns:
- The value, or
null if the entry doesn't exist.
-
findKeyIgnoreCase
Searches for the specified key in this map ignoring case.- Parameters:
key
- The key to search for. For performance reasons, it's preferable that the key be all lowercase.- Returns:
- The key, or
null if map does not contain this key.
-
getSwapped
Same asget()
, but converts the raw value to the specified class type using the specified POJO swap.- Type Parameters:
T
- The transformed class type.- Parameters:
key
- The key.objectSwap
- The swap class used to convert the raw type to a transformed type.- Returns:
- The value, or
null if the entry doesn't exist. - Throws:
ParseException
- Malformed input encountered.
-
find
Returns the value for the first key in the list that has an entry in this map.- Parameters:
keys
- The keys to look up in order.- Returns:
- The value of the first entry whose key exists, or
null if none of the keys exist in this map.
-
find
Returns the value for the first key in the list that has an entry in this map.Casts or converts the value to the specified class type.
See
BeanSession.convertToType(Object, ClassMeta)
for the list of valid data conversions.- Type Parameters:
T
- The class type to convert the value to.- Parameters:
type
- The class type to convert the value to.keys
- The keys to look up in order.- Returns:
- The value of the first entry whose key exists, or
null if none of the keys exist in this map.
-
getString
Returns the specified entry value converted to aString
.Shortcut for
get(key, String.
.class )- Parameters:
key
- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key.
-
getStringArray
Returns the specified entry value converted to aString
.Shortcut for
get(key, String[].
.class )- Parameters:
key
- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key.
-
getStringArray
Same asgetStringArray(String)
but returns a default value if the value cannot be found.- Parameters:
key
- The map key.def
- The default value if value is not found.- Returns:
- The value converted to a string array.
-
getString
Returns the specified entry value converted to aString
.Shortcut for
getWithDefault(key, defVal, String.
.class )- Parameters:
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
-
getInt
Returns the specified entry value converted to anInteger
.Shortcut for
get(key, Integer.
.class )- Parameters:
key
- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getInt
Returns the specified entry value converted to anInteger
.Shortcut for
getWithDefault(key, defVal, Integer.
.class )- Parameters:
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getLong
Returns the specified entry value converted to aLong
.Shortcut for
get(key, Long.
.class )- Parameters:
key
- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getLong
Returns the specified entry value converted to aLong
.Shortcut for
getWithDefault(key, defVal, Long.
.class )- Parameters:
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getBoolean
Returns the specified entry value converted to aBoolean
.Shortcut for
get(key, Boolean.
.class )- Parameters:
key
- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getBoolean
Returns the specified entry value converted to aBoolean
.Shortcut for
getWithDefault(key, defVal, Boolean.
.class )- Parameters:
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getMap
Returns the specified entry value converted to aMap
.Shortcut for
get(key, JsonMap.
.class )- Parameters:
key
- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getMap
Returns the specified entry value converted to aJsonMap
.Shortcut for
getWithDefault(key, defVal, JsonMap.
.class )- Parameters:
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getMap
Same asgetMap(String)
but creates a new emptyJsonMap
if it doesn't already exist.- Parameters:
key
- The key.createIfNotExists
- If mapping doesn't already exist, create one with an emptyJsonMap
.- Returns:
- The converted value, or an empty value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getMap
Same asgetMap(String, JsonMap)
except converts the keys and values to the specified types.- Type Parameters:
K
- The key type.V
- The value type.- Parameters:
key
- The key.keyType
- The key type class.valType
- The value type class.def
- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getList
Returns the specified entry value converted to aJsonList
.Shortcut for
get(key, JsonList.
.class )- Parameters:
key
- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getList
Returns the specified entry value converted to aJsonList
.Shortcut for
getWithDefault(key, defVal, JsonList.
.class )- Parameters:
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getList
Same asgetList(String)
but creates a new emptyJsonList
if it doesn't already exist.- Parameters:
key
- The key.createIfNotExists
- If mapping doesn't already exist, create one with an emptyJsonList
.- Returns:
- The converted value, or an empty value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getList
Same asgetList(String, JsonList)
except converts the elements to the specified types.- Type Parameters:
E
- The element type.- Parameters:
key
- The key.elementType
- The element type class.def
- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
findString
Returns the first entry that exists converted to aString
.Shortcut for
find(String.
.class , keys)- Parameters:
keys
- The list of keys to look for.- Returns:
- The converted value of the first key in the list that has an entry in this map, or
null if the map contains no mapping for any of the keys.
-
findInt
Returns the first entry that exists converted to anInteger
.Shortcut for
find(Integer.
.class , keys)- Parameters:
keys
- The list of keys to look for.- Returns:
- The converted value of the first key in the list that has an entry in this map, or
null if the map contains no mapping for any of the keys. - Throws:
InvalidDataConversionException
- If value cannot be converted.
-
findLong
Returns the first entry that exists converted to aLong
.Shortcut for
find(Long.
.class , keys)- Parameters:
keys
- The list of keys to look for.- Returns:
- The converted value of the first key in the list that has an entry in this map, or
null if the map contains no mapping for any of the keys. - Throws:
InvalidDataConversionException
- If value cannot be converted.
-
findBoolean
Returns the first entry that exists converted to aBoolean
.Shortcut for
find(Boolean.
.class , keys)- Parameters:
keys
- The list of keys to look for.- Returns:
- The converted value of the first key in the list that has an entry in this map, or
null if the map contains no mapping for any of the keys. - Throws:
InvalidDataConversionException
- If value cannot be converted.
-
findMap
Returns the first entry that exists converted to aJsonMap
.Shortcut for
find(JsonMap.
.class , keys)- Parameters:
keys
- The list of keys to look for.- Returns:
- The converted value of the first key in the list that has an entry in this map, or
null if the map contains no mapping for any of the keys. - Throws:
InvalidDataConversionException
- If value cannot be converted.
-
findList
Returns the first entry that exists converted to aJsonList
.Shortcut for
find(JsonList.
.class , keys)- Parameters:
keys
- The list of keys to look for.- Returns:
- The converted value of the first key in the list that has an entry in this map, or
null if the map contains no mapping for any of the keys. - Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getFirstKey
Returns the first key in the map.- Returns:
- The first key in the map, or
null if the map is empty.
-
getClassMeta
Returns the class type of the object at the specified index.- Parameters:
key
- The key into this map.- Returns:
- The data type of the object at the specified key, or
null if the value is null or does not exist.
-
removeWithDefault
Equivalent to callingget(class,key,def) followed byremove(key); - Type Parameters:
T
- The class type.- Parameters:
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.type
- The class type.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
removeString
Equivalent to callingremoveWithDefault(key,
.null ,String.class )- Parameters:
key
- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException
- If value cannot be converted.
-
removeString
Equivalent to callingremoveWithDefault(key,def,String.
.class )- Parameters:
key
- The key.def
- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
removeInt
Equivalent to callingremoveWithDefault(key,
.null ,Integer.class )- Parameters:
key
- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException
- If value cannot be converted.
-
removeInt
Equivalent to callingremoveWithDefault(key,def,Integer.
.class )- Parameters:
key
- The key.def
- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
removeBoolean
Equivalent to callingremoveWithDefault(key,
.null ,Boolean.class )- Parameters:
key
- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException
- If value cannot be converted.
-
removeBoolean
Equivalent to callingremoveWithDefault(key,def,Boolean.
.class )- Parameters:
key
- The key.def
- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
removeAll
Convenience method for removing several keys at once.- Parameters:
keys
- The list of keys to remove.
-
removeAll
Convenience method for removing several keys at once.- Parameters:
keys
- The list of keys to remove.
-
keepAll
The opposite ofremoveAll(String...)
.Discards all keys from this map that aren't in the specified list.
- Parameters:
keys
- The keys to keep.- Returns:
- This map.
-
containsKeyNotEmpty
Returnstrue if the map contains the specified entry and the value is not null nor an empty string.Always returns
false if the value is not aCharSequence
.- Parameters:
key
- The key.- Returns:
true if the map contains the specified entry and the value is not null nor an empty string.
-
containsOuterKey
Returnstrue if this map contains the specified key, ignoring the inner map if it exists.- Parameters:
key
- The key to look up.- Returns:
true if this map contains the specified key.
-
include
Returns a copy of thisJsonMap with only the specified keys.- Parameters:
keys
- The keys of the entries to copy.- Returns:
- A new map with just the keys and values from this map.
-
exclude
Returns a copy of thisJsonMap without the specified keys.- Parameters:
keys
- The keys of the entries not to copy.- Returns:
- A new map without the keys and values from this map.
-
cast
Converts this map into an object of the specified type.If this map contains a
"_type" entry, it must be the same as or a subclass of thetype .- Type Parameters:
T
- The class type to convert this map object to.- Parameters:
type
- The class type to convert this map object to.- Returns:
- The new object.
- Throws:
ClassCastException
- If the"_type" entry is present and not assignable fromtype
-
cast
Same ascast(Class)
, except allows you to specify aClassMeta
parameter.- Type Parameters:
T
- The class type to convert this map object to.- Parameters:
cm
- The class type to convert this map object to.- Returns:
- The new object.
- Throws:
ClassCastException
- If the"_type" entry is present and not assignable fromtype
-
getAt
Same asget(String,Class)
, but the key is a slash-delimited path used to traverse entries in this POJO.For example, the following code is equivalent:
JsonMap
map = JsonMap.ofJson ("..." );// Long way long _long =map .getMap("foo" ).getList("bar" ).getMap("0" ).getLong("baz" );// Using this method long _long =map .getAt("foo/bar/0/baz" ,long .class );This method uses the
ObjectRest
class to perform the lookup, so the map can contain any of the various class types that theObjectRest
class supports (e.g. beans, collections, arrays).- Type Parameters:
T
- The class type.- Parameters:
path
- The path to the entry.type
- The class type.- Returns:
- The value, or
null if the entry doesn't exist.
-
getAt
Same asgetAt(String,Class)
, but allows for conversion to complex maps and collections.This method uses the
ObjectRest
class to perform the lookup, so the map can contain any of the various class types that theObjectRest
class supports (e.g. beans, collections, arrays).- Type Parameters:
T
- The class type.- Parameters:
path
- The path to the entry.type
- The class type.args
- The class parameter types.- Returns:
- The value, or
null if the entry doesn't exist.
-
putAt
Same asput(String,Object) , but the key is a slash-delimited path used to traverse entries in this POJO.For example, the following code is equivalent:
JsonMap
map = JsonMap.ofJson ("..." );// Long way map .getMap("foo" ).getList("bar" ).getMap("0" ).put("baz" , 123);// Using this method map .putAt("foo/bar/0/baz" , 123);This method uses the
ObjectRest
class to perform the lookup, so the map can contain any of the various class types that theObjectRest
class supports (e.g. beans, collections, arrays).- Parameters:
path
- The path to the entry.o
- The new value.- Returns:
- The previous value, or
null if the entry doesn't exist.
-
postAt
Similar toputAt(String,Object)
, but used to append to collections and arrays.For example, the following code is equivalent:
JsonMap
map = JsonMap.ofJson ("..." );// Long way map .getMap("foo" ).getList("bar" ).append(123);// Using this method map .postAt("foo/bar" , 123);This method uses the
ObjectRest
class to perform the lookup, so the map can contain any of the various class types that theObjectRest
class supports (e.g. beans, collections, arrays).- Parameters:
path
- The path to the entry.o
- The new value.- Returns:
- The previous value, or
null if the entry doesn't exist.
-
deleteAt
Similar toremove(Object)
, but the key is a slash-delimited path used to traverse entries in this POJO.For example, the following code is equivalent:
JsonMap
map = JsonMap.ofJson ("..." );// Long way map .getMap("foo" ).getList("bar" ).getMap(0).remove("baz" );// Using this method map .deleteAt("foo/bar/0/baz" );This method uses the
ObjectRest
class to perform the lookup, so the map can contain any of the various class types that theObjectRest
class supports (e.g. beans, collections, arrays).- Parameters:
path
- The path to the entry.- Returns:
- The previous value, or
null if the entry doesn't exist.
-
put
-
getBeanSession
Returns theBeanSession
currently associated with this map.- Returns:
- The
BeanSession
currently associated with this map.
-
setBeanSession
Sets theBeanSession
currently associated with this map.- Parameters:
value
- TheBeanSession
currently associated with this map.- Returns:
- This object.
-
putJson
Convenience method for inserting JSON directly into an attribute on this object.The JSON text can be an object (i.e.
"{...}" ) or an array (i.e."[...]" ).- Parameters:
key
- The key.json
- The JSON text that will be parsed into an Object and then inserted into this map.- Throws:
ParseException
- Malformed input encountered.
-
asString
Serialize this object into a string using the specified serializer.- Parameters:
serializer
- The serializer to use to convert this object to a string.- Returns:
- This object serialized as a string.
-
asString
Serialize this object to Simplified JSON usingJson5Serializer.DEFAULT
.- Returns:
- This object serialized as a string.
-
asReadableString
Serialize this object to Simplified JSON usingJson5Serializer.DEFAULT_READABLE
.- Returns:
- This object serialized as a string.
-
writeTo
Convenience method for serializing this map to the specifiedWriter using theJsonSerializer.DEFAULT
serializer.- Parameters:
w
- The writer to serialize this object to.- Returns:
- This object.
- Throws:
IOException
- If a problem occurred trying to write to the writer.SerializeException
- If a problem occurred trying to convert the output.
-
isUnmodifiable
Returnstrue if this map is unmodifiable.- Returns:
true if this map is unmodifiable.
-
modifiable
Returns a modifiable copy of this map if it's unmodifiable.- Returns:
- A modifiable copy of this map if it's unmodifiable, or this map if it is already modifiable.
-
unmodifiable
Returns an unmodifiable copy of this map if it's modifiable.- Returns:
- An unmodifiable copy of this map if it's modifiable, or this map if it is already unmodifiable.
-
get
-
containsKey
- Specified by:
containsKey
in interfaceMap<String,
Object> - Overrides:
containsKey
in classHashMap<String,
Object>
-
keySet
-
entrySet
-
asJson
A synonym fortoString()
- Returns:
- This object as a JSON string.
-
toString
- Overrides:
toString
in classAbstractMap<String,
Object>
-