Class Json5Map
- All Implemented Interfaces:
Serializable,Cloneable,Map<String,Object>
MarshalledMap.
Carries the JSON5-specific surface that historically lived on JsonMap: a JSON5
toString(), JSON5 default-parser constructors, and convenience methods for working with
JSON5 text. Use this class when you want a Map-shaped value whose textual form (and
default parsing) is JSON5 ("simplified JSON" with unquoted keys, single-quoted strings, etc.).
For strict (RFC 8259) JSON behavior, use JsonMap. For a neutral, marshaller-agnostic
base with no language coupling, use MarshalledMap.
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
ConstructorsConstructorDescriptionJson5Map()Construct an empty map.Construct a map initialized with the specified reader containing JSON5.Construct a map initialized with the specified reader and parser.Json5Map(CharSequence json5) Construct a map initialized with the specified JSON5 text.Json5Map(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.Json5Map(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> Json5MapappendFirst(Predicate<T> test, String key, T... values) Adds the first value that matches the specified predicate.Add if flag istrue .<T> Json5MapAdd 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> Json5MapappendIfAbsentIf(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 Json5Mapcreate()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 Json5MapConstruct an empty filtered map.static Json5MapfilteredMap(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 aJson5List.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 aJson5Map.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 Json5MapFactory 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 Json5MapConstruct a map initialized with the specified key/value pairs.static Json5MapConstruct a map initialized with the specified map.static Json5MapConstruct a map initialized with the specified reader containing JSON5.static Json5MapConstruct a map initialized with the specified reader and parser.static Json5MapofString(CharSequence json5) Construct a map initialized with the specified JSON5 string.static Json5MapofString(CharSequence in, Parser p) Construct a map initialized with the specified string and parser.voidConvenience method for inserting JSON5 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.toJson5()Serializes this map to a JSON5 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 theJson5.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 Json5Map.
-
-
Constructor Details
-
Json5Map
public Json5Map()Construct an empty map. -
Json5Map
Construct an empty map with the specified bean context.- Parameters:
session- The bean session to use for creating beans.
-
Json5Map
Construct a map initialized with the specified JSON5 text.- Parameters:
json5- The JSON5 text to parse.- Throws:
ParseException- Malformed input encountered.
-
Json5Map
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. Ifnull , usesJson5Parser.- Throws:
ParseException- Malformed input encountered.
-
Json5Map
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().
-
Json5Map
Construct a map initialized with the specified key/value pairs.- Parameters:
keyValuePairs- A list of key/value pairs to add to this map.
-
Json5Map
Construct a map initialized with the specified reader containing JSON5.- Parameters:
json5- The reader containing JSON5 text to parse.- Throws:
ParseException- Malformed input encountered.
-
Json5Map
Construct a map initialized with the specified reader and parser.- Parameters:
in- The reader containing the input being parsed.p- The parser to use. Ifnull , usesJson5Parser.- 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:
Json5Map
map =new Json5Map("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 JSON5 string.- Parameters:
json5- The JSON5 text to parse.- 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 JSON5.- Parameters:
json5- The reader containing JSON5 text to parse.- 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 and parser.- Parameters:
in- The input being parsed.p- The parser to use to parse the input.
Ifnull , usesJson5Parser.- 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 and parser.- Parameters:
in- The reader containing the input being parsed.p- The parser to use to parse the input.
Ifnull , usesJson5Parser.- 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.
-
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 aJson5List.- 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 aJson5Map.- 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.
-
putJson5
Convenience method for inserting JSON5 directly into an attribute on this object.The JSON5 text can be an object (i.e.
"{...}" ) or an array (i.e."[...]" ).- Parameters:
key- The key.json5- The JSON5 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.
-
toJson5
Serializes this map to a JSON5 string.A synonym for
toString().- Returns:
- This object as a JSON5 string.
-
toReadableJson5
Serializes this map to a readable (indented) JSON5 string usingJson5Serializer.DEFAULT_READABLE.- Returns:
- This object serialized as a readable JSON5 string.
-
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 theJson5.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.
-