Class JsonMap
- All Implemented Interfaces:
Serializable,Cloneable,Map<String,Object>
- Direct Known Subclasses:
DelegateMap
This class is a subclass of MarshalledMap that carries strict-JSON-specific surface, including
strict-JSON toString() (via Json.of(Object)), JsonParser-defaulting
CharSequence / Reader constructors, and convenience methods for serializing to the
various JSON dialects (JSON5, JSON Lines, canonical JSON, HJSON).
v10.0 behavioral break: toString() now produces strict (RFC 8259) JSON (was JSON5),
and the CharSequence / Reader constructors now default to JsonParser.DEFAULT
(was Json5Parser.DEFAULT). For the previous JSON5 behavior use Json5Map.
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
Fields -
Constructor Summary
ConstructorsConstructorDescriptionJsonMap()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(MarshallingSession 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.<T> JsonMapappendFirst(Predicate<T> test, String key, T... values) Adds the first value that matches the specified predicate.Add if flag istrue .<T> JsonMapAdd 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> JsonMapappendIfAbsentIf(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.static JsonMapcreate()Construct an empty map.Returns a copy of this map without the specified keys.filtered()Enables filtering based on default values.Enables filtering based on a predicate test.static JsonMapConstruct an empty filtered map.static JsonMapfilteredMap(Object... keyValuePairs) Construct a filtered map initialized with the specified key/value pairs.Returns the first entry that exists converted to aMarshalledList.Returns the first entry that exists converted to aMarshalledMap.Returns the specified entry value converted to aMarshalledList.Same asMarshalledMap.getList(String)but creates a new emptyMarshalledListif it doesn't already exist.Returns the specified entry value converted to aJsonList.Returns the specified entry value converted to aMap.Same asMarshalledMap.getMap(String)but creates a new emptyMarshalledMapif it doesn't already exist.Returns the specified entry value converted to aJsonMap.Returns a copy of this map with only the specified keys.Set an inner map in this map to allow for chained get calls.The opposite ofMarshalledMap.removeAll(String...).Returns a modifiable copy of this map if it's unmodifiable.protected JsonMapFactory hook used byMarshalledMap.cast(Class)/MarshalledMap.cast(ClassMeta)when the target map type cannot be instantiated directly (e.g. when the caller asks for theMapinterface).static JsonMapConstruct a map initialized with the specified key/value pairs.static JsonMapConstruct a map initialized with the specified map.static JsonMapConstruct a map initialized with the specified reader containing JSON.static JsonMapConstruct a map initialized with the specified string.static JsonMapofString(CharSequence json) Construct a map initialized with the specified JSON string.static JsonMapofString(CharSequence in, Parser p) Construct a map initialized with the specified string.voidConvenience method for inserting JSON directly into an attribute on this object.session(MarshallingSession session) Override the default bean session used for converting POJOs.setBeanSession(MarshallingSession value) Sets theMarshallingSessioncurrently associated with this map.toHjson()Serializes this map to an HJSON string.toJcs()Serializes this map to a canonical JSON string (RFC 8785).toJson()Serializes this map to a standard JSON string.toJson5()Serializes this map to a JSON5 string.toJsonl()Serializes this map to a JSON Lines string.Serializes this map to a readable (indented) JSON5 string usingJson5Serializer.DEFAULT_READABLE.toString()Returns an unmodifiable copy of this map if it's modifiable.Convenience method for serializing this map to the specifiedWriter using theJson.DEFAULTmarshaller.Methods inherited from class org.apache.juneau.marshall.collections.MarshalledMap
at, bs, cast, cast, containsKey, containsKeyNotEmpty, containsOuterKey, deleteAt, entrySet, equals, find, find, findBoolean, findInt, findKeyIgnoreCase, findLong, findString, get, get, get, getAt, getAt, getBoolean, getBoolean, getClassMeta, getFirstKey, getInt, getInt, getList, getList, getLong, getLong, getMap, getMap, getMarshallingSession, getString, getString, getStringArray, getStringArray, getSwapped, getWithDefault, getWithDefault, getWithDefault, hashCode, is, isUnmodifiable, keySet, postAt, put, putAt, removeAll, removeAll, removeBoolean, removeBoolean, removeInt, removeInt, removeString, removeString, removeWithDefault, toStringMethods inherited from class java.util.LinkedHashMap
clear, containsValue, forEach, getOrDefault, removeEldestEntry, replaceAll, valuesMethods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, sizeMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, 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 JSON.- Parameters:
json- The JSON text to parse.
Must be strict (RFC 8259) JSON; for JSON5 input useJson5Mapor passJson5Parser.DEFAULTexplicitly.- Throws:
ParseException- Malformed input encountered.
-
JsonMap
Construct a map initialized with the specified string.- Parameters:
in- The input being parsed.
Can benull or empty (results in an empty map; no parsing occurs).p- The parser to use to parse the input.
Ifnull , usesJsonParser.- Throws:
ParseException- Malformed input encountered.
-
JsonMap
Construct a map initialized with the specified map.- Parameters:
in- The map to copy.
Can benull (results in an empty map).
Keys will be converted to strings usingObject.toString().
-
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.
-
JsonMap
Construct a map initialized with the specified reader containing JSON.- Parameters:
json- The reader containing JSON text to parse.
Must be strict (RFC 8259) JSON; for JSON5 input useJson5Mapor passJson5Parser.DEFAULTexplicitly.- Throws:
ParseException- Malformed input encountered.
-
JsonMap
Construct a map initialized with the specified string.- Parameters:
in- The reader containing the input being parsed.
Must be strict (RFC 8259) JSON.p- The parser to use to parse the input.
Ifnull , usesJsonParser.- Throws:
ParseException- Malformed input encountered.
-
-
Method Details
-
create
Construct an empty map.- Returns:
- An empty map.
-
filteredMap
Construct an empty filtered map.- Returns:
- An empty map.
-
filteredMap
Construct a filtered map initialized with the specified key/value pairs.Same as
of(Object...)but callsfiltered()on the created map.- Parameters:
keyValuePairs- A list of key/value pairs to add to this map.- Returns:
- A new map, never
null .
-
of
Construct a map initialized with the specified map.- Parameters:
values- The map to copy.
Can benull (returnsnull ).
Keys will be converted to strings usingObject.toString().- Returns:
- A new map or
null if the map wasnull .
-
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 .
-
ofString
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 (empty if the input was
null ), nevernull . - Throws:
ParseException- Malformed input encountered.
-
ofString
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 (empty if the input was
null ), nevernull . - Throws:
ParseException- Malformed input encountered.
-
ofString
Construct a map initialized with the specified string.- Parameters:
in- The input being parsed.p- The parser to use to parse the input.
Ifnull , usesJsonParser.- Returns:
- A new map (empty if the input was
null ), nevernull . - Throws:
ParseException- Malformed input encountered.
-
ofString
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 (empty if the input was
null ), nevernull . - Throws:
ParseException- Malformed input encountered.
-
append
Description copied from class:MarshalledMapAppends all the entries in the specified map to this map.- Overrides:
appendin classMarshalledMap- Parameters:
values- The map to copy. Can benull (no-op).- Returns:
- This object.
-
append
Description copied from class:MarshalledMapAdds an entry to this map.- Overrides:
appendin classMarshalledMap- Parameters:
key- The key.value- The value.- Returns:
- This object.
-
appendFirst
Description copied from class:MarshalledMapAdds the first value that matches the specified predicate.- Overrides:
appendFirstin classMarshalledMap- Type Parameters:
T- The value types.- Parameters:
test- The predicate to match against.key- The key.values- The values to test.- Returns:
- This object.
-
appendIf
Description copied from class:MarshalledMapAdd if flag istrue .- Overrides:
appendIfin classMarshalledMap- Parameters:
flag- The flag to check.key- The key.value- The value.- Returns:
- This object.
-
appendIf
Description copied from class:MarshalledMapAdd if predicate matches value.- Overrides:
appendIfin classMarshalledMap- Type Parameters:
T- The value type.- Parameters:
test- The predicate to match against.key- The key.value- The value.- Returns:
- This object.
-
appendIfAbsent
Description copied from class:MarshalledMapAdds a value in this map if the entry does not exist or the current value isnull .- Overrides:
appendIfAbsentin classMarshalledMap- Parameters:
key- The map key.value- The value to set if the current value does not exist or isnull .- Returns:
- This object.
-
appendIfAbsentIf
Description copied from class:MarshalledMapAdds a value in this map if the entry does not exist or the current value isnull and the value matches the specified predicate.- Overrides:
appendIfAbsentIfin classMarshalledMap- 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.
-
toJson
Serializes this map to a standard JSON string.- Returns:
- This object as a standard JSON string.
-
toJson5
Serializes this map to a JSON5 string.A synonym for
toString().- Returns:
- This object as a JSON5 string.
-
toJsonl
Serializes this map to a JSON Lines string.- Returns:
- This object as a JSON Lines string.
-
toJcs
Serializes this map to a canonical JSON string (RFC 8785).- Returns:
- This object as a canonical JSON string.
-
toHjson
Serializes this map to an HJSON string.- Returns:
- This object as an HJSON string.
-
toReadableJson5
Serializes this map to a readable (indented) JSON5 string usingJson5Serializer.DEFAULT_READABLE.- Returns:
- This object serialized as a readable JSON5 string.
-
exclude
Description copied from class:MarshalledMapReturns a copy of this map without the specified keys.- Overrides:
excludein classMarshalledMap- Parameters:
keys- The keys of the entries not to copy.- Returns:
- A new map without the keys and values from this map.
-
filtered
Description copied from class:MarshalledMapEnables 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.
- Overrides:
filteredin classMarshalledMap- Returns:
- This object.
-
filtered
Description copied from class:MarshalledMapEnables filtering based on a predicate test.If the predicate evaluates to
false on values added to this map, the entry will be skipped.- Overrides:
filteredin classMarshalledMap- Parameters:
value- The value tester predicate.- Returns:
- This object.
-
findList
Description copied from class:MarshalledMapReturns the first entry that exists converted to aMarshalledList.- Overrides:
findListin classMarshalledMap- 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.
-
findMap
Description copied from class:MarshalledMapReturns the first entry that exists converted to aMarshalledMap.- Overrides:
findMapin classMarshalledMap- 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.
-
getList
Description copied from class:MarshalledMapReturns the specified entry value converted to aMarshalledList.- Overrides:
getListin classMarshalledMap- Parameters:
key- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key.
-
getList
Description copied from class:MarshalledMapSame asMarshalledMap.getList(String)but creates a new emptyMarshalledListif it doesn't already exist.- Overrides:
getListin classMarshalledMap- Parameters:
key- The key.createIfNotExists- If mapping doesn't already exist, create one with an emptyMarshalledList.- Returns:
- The converted value, or an empty value if the map contains no mapping for this key.
-
getList
Returns the specified entry value converted to aJsonList.- 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
Description copied from class:MarshalledMapReturns the specified entry value converted to aMap.- Overrides:
getMapin classMarshalledMap- Parameters:
key- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key.
-
getMap
Description copied from class:MarshalledMapSame asMarshalledMap.getMap(String)but creates a new emptyMarshalledMapif it doesn't already exist.- Overrides:
getMapin classMarshalledMap- Parameters:
key- The key.createIfNotExists- If mapping doesn't already exist, create one with an emptyMarshalledMap.- Returns:
- The converted value, or an empty value if the map contains no mapping for this key.
-
getMap
Returns the specified entry value converted to aJsonMap.- 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.
-
include
Description copied from class:MarshalledMapReturns a copy of this map with only the specified keys.- Overrides:
includein classMarshalledMap- Parameters:
keys- The keys of the entries to copy.- Returns:
- A new map with just the keys and values from this map.
-
inner
Description copied from class:MarshalledMapSet an inner map in this map to allow for chained get calls.If
MarshalledMap.get(Object)returnsnull , thenMarshalledMap.get(Object)will be called on the inner map.- Overrides:
innerin classMarshalledMap- Parameters:
inner- The inner map. Can benull to remove the inner map from an existing map.- Returns:
- This object.
-
keepAll
Description copied from class:MarshalledMapThe opposite ofMarshalledMap.removeAll(String...).Discards all keys from this map that aren't in the specified list.
- Overrides:
keepAllin classMarshalledMap- Parameters:
keys- The keys to keep.- Returns:
- This map.
-
modifiable
Description copied from class:MarshalledMapReturns a modifiable copy of this map if it's unmodifiable.- Overrides:
modifiablein classMarshalledMap- Returns:
- A modifiable copy of this map if it's unmodifiable, or this map if it is already modifiable.
-
newMarshalledMap
Description copied from class:MarshalledMapFactory hook used byMarshalledMap.cast(Class)/MarshalledMap.cast(ClassMeta)when the target map type cannot be instantiated directly (e.g. when the caller asks for theMapinterface).Subclasses should override to return an instance of the matching
XMapvariant so that callers chaining off ofcast(Map. still see their flavored map type.class )- Overrides:
newMarshalledMapin classMarshalledMap- Parameters:
bs- The marshalling session to associate with the new map.- Returns:
- A new map instance.
-
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."[...]" ).Input must be strict (RFC 8259) JSON; for JSON5 use
Json5Map.putJson5(String,String).- 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.
-
session
Description copied from class:MarshalledMapOverride the default bean session used for converting POJOs.Default is
MarshallingContext.DEFAULT, which is sufficient in most cases.Useful if you're serializing/parsing beans with transforms defined.
- Overrides:
sessionin classMarshalledMap- Parameters:
session- The new bean session.- Returns:
- This object.
-
setBeanSession
Description copied from class:MarshalledMapSets theMarshallingSessioncurrently associated with this map.- Overrides:
setBeanSessionin classMarshalledMap- Parameters:
value- TheMarshallingSessioncurrently associated with this map.- Returns:
- This object.
-
toString
- Overrides:
toStringin classAbstractMap<String,Object>
-
unmodifiable
Description copied from class:MarshalledMapReturns an unmodifiable copy of this map if it's modifiable.- Overrides:
unmodifiablein classMarshalledMap- Returns:
- An unmodifiable copy of this map if it's modifiable, or this map if it is already unmodifiable.
-
writeTo
Convenience method for serializing this map to the specifiedWriter using theJson.DEFAULTmarshaller.- 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.
-