public class ObjectMap extends LinkedHashMap<String,Object>
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.
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.
This class is not thread safe.
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Modifier and Type | Field and Description |
---|---|
static ObjectMap |
EMPTY_MAP
An empty read-only ObjectMap.
|
Constructor and Description |
---|
ObjectMap()
Construct an empty JSON object (i.e.
|
ObjectMap(BeanSession session)
Construct an empty JSON object (i.e.
|
ObjectMap(CharSequence s)
Shortcut for
|
ObjectMap(CharSequence s,
Parser p)
Construct an ObjectMap directly from a string using the specified parser.
|
ObjectMap(Map<?,?> m)
Construct a JSON object and fill it with the contents from the specified
Map . |
ObjectMap(Reader r)
Shortcut for
. |
ObjectMap(Reader r,
Parser p)
Construct an ObjectMap directly from a reader using the specified parser.
|
Modifier and Type | Method and Description |
---|---|
ObjectMap |
append(String key,
Object value)
Convenience method for adding multiple objects to this map.
|
ObjectMap |
appendAll(Map<String,Object> m)
Convenience method for adding a contents of another map to this map.
|
<T> T |
cast(Class<T> type)
Converts this map into an object of the specified type.
|
<T> T |
cast(ClassMeta<T> cm)
Same as
cast(Class) , except allows you to specify a ClassMeta parameter. |
boolean |
containsKey(Object key) |
boolean |
containsOuterKey(Object key)
Returns
|
Object |
deleteAt(String path)
Similar to
remove(Object) , but the key is a slash-delimited path used to traverse entries
in this POJO. |
Set<Map.Entry<String,Object>> |
entrySet() |
ObjectMap |
exclude(String... keys)
Returns a copy of this
ObjectMap without the specified keys. |
<T> T |
find(Class<T> type,
String... keys)
Returns the value for the first key in the list that has an entry in this map.
|
Object |
find(String... keys)
Returns the value for the first key in the list that has an entry in this map.
|
Boolean |
findBoolean(String... keys)
Returns the first entry that exists converted to a
Boolean . |
Integer |
findInt(String... keys)
Returns the first entry that exists converted to an
Integer . |
String |
findKeyIgnoreCase(String key)
Searches for the specified key in this map ignoring case.
|
List<?> |
findList(String... keys)
Returns the first entry that exists converted to a
List . |
Long |
findLong(String... keys)
Returns the first entry that exists converted to a
Long . |
Map<?,?> |
findMap(String... keys)
Returns the first entry that exists converted to a
Map . |
ObjectList |
findObjectList(String... keys)
Returns the first entry that exists converted to a
ObjectList . |
ObjectMap |
findObjectMap(String... keys)
Returns the first entry that exists converted to a
ObjectMap . |
String |
findString(String... keys)
Returns the first entry that exists converted to a
String . |
Object |
get(Object key) |
<T> T |
get(String key,
Class<T> type)
Same as
get() , but casts or converts the value to the specified class type. |
<T> T |
get(String key,
Type type,
Type... args)
Same as
get(String,Class) , but allows for complex data types consisting of collections or maps. |
<T> T |
getAt(String path,
Class<T> type)
Same as
get(String,Class) , but the key is a slash-delimited path used to traverse
entries in this POJO. |
<T> T |
getAt(String path,
Type type,
Type... args)
Same as
getAt(String,Class) , but allows for conversion to complex maps and collections. |
BeanSession |
getBeanSession()
Returns the
BeanSession currently associated with this map. |
Boolean |
getBoolean(String key)
Returns the specified entry value converted to a
Boolean . |
Boolean |
getBoolean(String key,
Boolean defVal)
Returns the specified entry value converted to a
Boolean . |
ClassMeta<?> |
getClassMeta(String key)
Returns the class type of the object at the specified index.
|
String |
getFirstKey()
Returns the first key in the map.
|
Integer |
getInt(String key)
Returns the specified entry value converted to an
Integer . |
Integer |
getInt(String key,
Integer defVal)
Returns the specified entry value converted to an
Integer . |
List<?> |
getList(String key)
Returns the specified entry value converted to a
List . |
<E> List<E> |
getList(String key,
Class<E> elementType,
List<E> def)
Same as
getList(String, List) except converts the elements to the specified types. |
List<?> |
getList(String key,
List<?> defVal)
Returns the specified entry value converted to a
List . |
Long |
getLong(String key)
Returns the specified entry value converted to a
Long . |
Long |
getLong(String key,
Long defVal)
Returns the specified entry value converted to a
Long . |
Map<?,?> |
getMap(String key)
Returns the specified entry value converted to a
Map . |
<K,V> Map<K,V> |
getMap(String key,
Class<K> keyType,
Class<V> valType,
Map<K,V> def)
Same as
getMap(String, Map) except converts the keys and values to the specified types. |
Map<?,?> |
getMap(String key,
Map<?,?> defVal)
Returns the specified entry value converted to a
Map . |
ObjectList |
getObjectList(String key)
Returns the specified entry value converted to a
ObjectList . |
ObjectList |
getObjectList(String key,
ObjectList defVal)
Returns the specified entry value converted to a
ObjectList . |
ObjectMap |
getObjectMap(String key)
Returns the specified entry value converted to a
Map . |
ObjectMap |
getObjectMap(String key,
ObjectMap defVal)
Returns the specified entry value converted to a
ObjectMap . |
String |
getString(String key)
Returns the specified entry value converted to a
String . |
String |
getString(String key,
String defVal)
Returns the specified entry value converted to a
String . |
String[] |
getStringArray(String key)
Returns the specified entry value converted to a
String . |
String[] |
getStringArray(String key,
String[] def)
Same as
getStringArray(String) but returns a default value if the value cannot be found. |
<T> T |
getSwapped(String key,
PojoSwap<T,?> pojoSwap)
Same as
get() , but converts the raw value to the specified class type using the specified
POJO swap. |
Object |
getWithDefault(String key,
Object def)
Same as
get() , but returns the default value if the key could not be found. |
<T> T |
getWithDefault(String key,
T def,
Class<T> type)
Same as
get(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 as
get(String,Type,Type...) but returns a default value if the value does not exist. |
ObjectMap |
include(String... keys)
Returns a copy of this
ObjectMap with only the specified keys. |
Set<String> |
keySet() |
Object |
postAt(String path,
Object o)
Similar to
putAt(String,Object) , but used to append to collections and arrays. |
Object |
putAt(String path,
Object o)
Same as
put(String,Object) , but the key is a slash-delimited path used to traverse entries in this
POJO. |
ObjectMap |
putIfEmpty(String key,
Object val)
Sets a value in this map if the entry does not exist or the value is
|
ObjectMap |
putIfNull(String key,
Object val)
Sets a value in this map if the entry does not exist or the value is
|
void |
putJson(String key,
String json)
Convenience method for inserting JSON directly into an attribute on this object.
|
void |
removeAll(Collection<String> keys)
Convenience method for removing several keys at once.
|
void |
removeAll(String... keys)
Convenience method for removing several keys at once.
|
<T> T |
removeWithDefault(String key,
T defVal,
Class<T> type)
Equivalent to calling
get(class,key,def) followed by remove(key); |
ObjectMap |
serializeTo(Writer w)
Convenience method for serializing this map to the specified
Writer using the
JsonSerializer.DEFAULT serializer. |
ObjectMap |
setBeanSession(BeanSession session)
Override the default bean session used for converting POJOs.
|
ObjectMap |
setInner(Map<String,Object> inner)
Set an inner map in this map to allow for chained get calls.
|
String |
toString()
Serialize this object into a JSON string using the
JsonSerializer.DEFAULT serializer. |
String |
toString(WriterSerializer serializer)
Serialize this object into a string using the specified serializer.
|
clear, containsValue, forEach, getOrDefault, removeEldestEntry, replaceAll, values
clone, compute, computeIfAbsent, computeIfPresent, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
equals, hashCode
public ObjectMap(CharSequence s, Parser p) throws ParseException
s
- The string being parsed.p
- The parser to use to parse the input.ParseException
- If the input contains a syntax error or is malformed.public ObjectMap(CharSequence s) throws ParseException
new ObjectMap(string,JsonParser.DEFAULT );
s
- The JSON text to parse.ParseException
- If the input contains a syntax error or is malformed.public ObjectMap(Reader r, Parser p) throws ParseException, IOException
r
- The reader to read from. The reader will be wrapped in a BufferedReader
if it isn't already.p
- The parser to use to parse the input.ParseException
- If the input contains a syntax error or is malformed.IOException
- If a problem occurred trying to read from the reader.public ObjectMap(Reader r) throws ParseException, IOException
new ObjectMap(reader, JsonParser.DEFAULT )
.r
- The reader to read from. The reader will be wrapped in a BufferedReader
if it isn't already.ParseException
- If the input contains a syntax error or is malformed.IOException
- If a problem occurred trying to read from the reader.public ObjectMap()
LinkedHashMap
).public ObjectMap(BeanSession session)
LinkedHashMap
) with the specified bean context.session
- The bean session to use for creating beans.public ObjectMap setInner(Map<String,Object> inner)
If get(Object)
returns get(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.
ObjectMap m1 =
inner
- The inner map.
Can be public String findKeyIgnoreCase(String key)
key
- The key to search for.
For performance reasons, it's preferable that the key be all lowercase.public ObjectMap setBeanSession(BeanSession session)
Default is BeanContext.DEFAULT
, which is sufficient in most cases.
Useful if you're serializing/parsing beans with transforms defined.
session
- The new bean session.public BeanSession getBeanSession()
BeanSession
currently associated with this map.BeanSession
currently associated with this map.public ObjectMap append(String key, Object value)
Equivalent to calling put(key, value)
, but returns this map so that the method can be chained.
key
- The key.value
- The value.public ObjectMap appendAll(Map<String,Object> m)
Equivalent to calling putAll(m)
, but returns this map so that the method can be chained.
m
- The map whose contents should be added to this map.public <T> T get(String key, Class<T> type)
get()
, but casts or converts the value to the specified class type.
This is the preferred get method for simple types.
ObjectMap m =
See BeanSession.convertToType(Object, ClassMeta)
for the list of valid data conversions.
T
- The class type returned.key
- The key.type
- The class type returned.public <T> T get(String key, Type type, Type... args)
get(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).
ObjectMap m =
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.
get(String, Class)
method instead if you don't need a parameterized map/collection.
T
- The class type returned.key
- The key.type
- The class type returned.args
- The class type parameters.public Object getWithDefault(String key, Object def)
get()
, but returns the default value if the key could not be found.key
- The key.def
- The default value if the entry doesn't exist.public <T> T getWithDefault(String key, T def, Class<T> type)
get(String,Class)
but returns a default value if the value does not exist.T
- The class type returned.key
- The key.def
- The default value. Can be type
- The class type returned.public <T> T getWithDefault(String key, T def, Type type, Type... args)
get(String,Type,Type...)
but returns a default value if the value does not exist.T
- The class type returned.key
- The key.def
- The default value. Can be type
- The class type returned.args
- The class type parameters.public <T> T getSwapped(String key, PojoSwap<T,?> pojoSwap) throws ParseException
get()
, but converts the raw value to the specified class type using the specified
POJO swap.T
- The transformed class type.key
- The key.pojoSwap
- The swap class used to convert the raw type to a transformed type.ParseException
- Thrown by the POJO swap if a problem occurred trying to parse the value.public Object find(String... keys)
keys
- The keys to look up in order.public <T> T find(Class<T> type, String... keys)
Casts or converts the value to the specified class type.
See BeanSession.convertToType(Object, ClassMeta)
for the list of valid data conversions.
T
- The class type to convert the value to.type
- The class type to convert the value to.keys
- The keys to look up in order.public <T> T getAt(String path, Class<T> type)
get(String,Class)
, but the key is a slash-delimited path used to traverse
entries in this POJO.
For example, the following code is equivalent:
ObjectMap m = getObjectMap();
This method uses the PojoRest
class to perform the lookup, so the map can contain any of the various
class types that the PojoRest
class supports (e.g. beans, collections, arrays).
T
- The class type.path
- The path to the entry.type
- The class type.public <T> T getAt(String path, Type type, Type... args)
getAt(String,Class)
, but allows for conversion to complex maps and collections.
This method uses the PojoRest
class to perform the lookup, so the map can contain any of the various
class types that the PojoRest
class supports (e.g. beans, collections, arrays).
T
- The class type.path
- The path to the entry.type
- The class type.args
- The class parameter types.public Object putAt(String path, Object o)
put(String,Object)
, but the key is a slash-delimited path used to traverse entries in this
POJO.
For example, the following code is equivalent:
ObjectMap m = getObjectMap();
This method uses the PojoRest
class to perform the lookup, so the map can contain any of the various
class types that the PojoRest
class supports (e.g. beans, collections, arrays).
path
- The path to the entry.o
- The new value.public Object postAt(String path, Object o)
putAt(String,Object)
, but used to append to collections and arrays.
For example, the following code is equivalent:
ObjectMap m = getObjectMap();
This method uses the PojoRest
class to perform the lookup, so the map can contain any of the various
class types that the PojoRest
class supports (e.g. beans, collections, arrays).
path
- The path to the entry.o
- The new value.public Object deleteAt(String path)
remove(Object)
, but the key is a slash-delimited path used to traverse entries
in this POJO.
For example, the following code is equivalent:
ObjectMap m = getObjectMap();
This method uses the PojoRest
class to perform the lookup, so the map can contain any of the various
class types that the PojoRest
class supports (e.g. beans, collections, arrays).
path
- The path to the entry.public void putJson(String key, String json) throws ParseException
The JSON text can be an object (i.e.
key
- The key.json
- The JSON text that will be parsed into an Object and then inserted into this map.ParseException
- If the input contains a syntax error or is malformed.public String getString(String key)
String
.
Shortcut for get(key, String.
.
key
- The key.public String[] getStringArray(String key)
String
.
Shortcut for get(key, String[].
.
key
- The key.public String[] getStringArray(String key, String[] def)
getStringArray(String)
but returns a default value if the value cannot be found.key
- The map key.def
- The default value if value is not found.public String getString(String key, String defVal)
String
.
Shortcut for getWithDefault(key, defVal, String.
.
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.public Integer getInt(String key)
Integer
.
Shortcut for get(key, Integer.
.
key
- The key.InvalidDataConversionException
- If value cannot be converted.public Integer getInt(String key, Integer defVal)
Integer
.
Shortcut for getWithDefault(key, defVal, Integer.
.
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public Long getLong(String key)
Long
.
Shortcut for get(key, Long.
.
key
- The key.InvalidDataConversionException
- If value cannot be converted.public Long getLong(String key, Long defVal)
Long
.
Shortcut for getWithDefault(key, defVal, Long.
.
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public Boolean getBoolean(String key)
Boolean
.
Shortcut for get(key, Boolean.
.
key
- The key.InvalidDataConversionException
- If value cannot be converted.public Boolean getBoolean(String key, Boolean defVal)
Boolean
.
Shortcut for getWithDefault(key, defVal, Boolean.
.
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public Map<?,?> getMap(String key)
Map
.
Shortcut for get(key, Map.
.
key
- The key.InvalidDataConversionException
- If value cannot be converted.public Map<?,?> getMap(String key, Map<?,?> defVal)
Map
.
Shortcut for getWithDefault(key, defVal, Map.
.
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public <K,V> Map<K,V> getMap(String key, Class<K> keyType, Class<V> valType, Map<K,V> def)
getMap(String, Map)
except converts the keys and values to the specified types.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.InvalidDataConversionException
- If value cannot be converted.public List<?> getList(String key)
List
.
Shortcut for get(key, List.
.
key
- The key.InvalidDataConversionException
- If value cannot be converted.public List<?> getList(String key, List<?> defVal)
List
.
Shortcut for getWithDefault(key, defVal, List.
.
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public <E> List<E> getList(String key, Class<E> elementType, List<E> def)
getList(String, List)
except converts the elements to the specified types.key
- The key.elementType
- The element type class.def
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public ObjectMap getObjectMap(String key)
Map
.
Shortcut for get(key, ObjectMap.
.
key
- The key.InvalidDataConversionException
- If value cannot be converted.public ObjectMap getObjectMap(String key, ObjectMap defVal)
ObjectMap
.
Shortcut for getWithDefault(key, defVal, ObjectMap.
.
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public ObjectList getObjectList(String key)
ObjectList
.
Shortcut for get(key, ObjectList.
.
key
- The key.InvalidDataConversionException
- If value cannot be converted.public ObjectList getObjectList(String key, ObjectList defVal)
ObjectList
.
Shortcut for getWithDefault(key, defVal, ObjectList.
.
key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public String findString(String... keys)
String
.
Shortcut for find(String.
.
keys
- The list of keys to look for.public Integer findInt(String... keys)
Integer
.
Shortcut for find(Integer.
.
keys
- The list of keys to look for.InvalidDataConversionException
- If value cannot be converted.public Long findLong(String... keys)
Long
.
Shortcut for find(Long.
.
keys
- The list of keys to look for.InvalidDataConversionException
- If value cannot be converted.public Boolean findBoolean(String... keys)
Boolean
.
Shortcut for find(Boolean.
.
keys
- The list of keys to look for.InvalidDataConversionException
- If value cannot be converted.public Map<?,?> findMap(String... keys)
Map
.
Shortcut for find(Map.
.
keys
- The list of keys to look for.InvalidDataConversionException
- If value cannot be converted.public List<?> findList(String... keys)
List
.
Shortcut for find(List.
.
keys
- The list of keys to look for.InvalidDataConversionException
- If value cannot be converted.public ObjectMap findObjectMap(String... keys)
ObjectMap
.
Shortcut for find(ObjectMap.
.
keys
- The list of keys to look for.InvalidDataConversionException
- If value cannot be converted.public ObjectList findObjectList(String... keys)
ObjectList
.
Shortcut for find(ObjectList.
.
keys
- The list of keys to look for.InvalidDataConversionException
- If value cannot be converted.public String getFirstKey()
public ClassMeta<?> getClassMeta(String key)
key
- The key into this map.public <T> T removeWithDefault(String key, T defVal, Class<T> type)
get(class,key,def)
followed by remove(key);
T
- The class type.key
- The key.defVal
- The default value if the map doesn't contain the specified mapping.type
- The class type.InvalidDataConversionException
- If value cannot be converted.public void removeAll(Collection<String> keys)
keys
- The list of keys to remove.public void removeAll(String... keys)
keys
- The list of keys to remove.public boolean containsKey(Object key)
containsKey
in interface Map<String,Object>
containsKey
in class HashMap<String,Object>
public boolean containsOuterKey(Object key)
key
- The key to look up.public ObjectMap include(String... keys)
ObjectMap
with only the specified keys.keys
- The keys of the entries to copy.public ObjectMap exclude(String... keys)
ObjectMap
without the specified keys.keys
- The keys of the entries not to copy.public ObjectMap putIfNull(String key, Object val)
key
- The map key.val
- The value to set if the current value does not exist or is public ObjectMap putIfEmpty(String key, Object val)
key
- The map key.val
- The value to set if the current value does not exist or is public <T> T cast(Class<T> type)
If this map contains a type
.
T
- The class type to convert this map object to.type
- The class type to convert this map object to.ClassCastException
- If the type
public <T> T cast(ClassMeta<T> cm)
cast(Class)
, except allows you to specify a ClassMeta
parameter.T
- The class type to convert this map object to.cm
- The class type to convert this map object to.ClassCastException
- If the type
public String toString(WriterSerializer serializer) throws SerializeException
serializer
- The serializer to use to convert this object to a string.SerializeException
- If a problem occurred trying to convert the output.public String toString()
JsonSerializer.DEFAULT
serializer.toString
in class AbstractMap<String,Object>
public ObjectMap serializeTo(Writer w) throws IOException, SerializeException
Writer
using the
JsonSerializer.DEFAULT
serializer.w
- The writer to serialize this object to.IOException
- If a problem occurred trying to write to the writer.SerializeException
- If a problem occurred trying to convert the output.Copyright © 2018 Apache. All rights reserved.