Class Json5Map

All Implemented Interfaces:
Serializable, Cloneable, Map<String,Object>

public class Json5Map extends MarshalledMap
JSON5-flavored 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:

// Construct an empty Map Json5Map map = Json5Map.of(); // Construct a Map from JSON5 map = Json5Map.ofString("{a:'A',b:{c:'C',d:123}}"); // JSON5 round-trip via toString() String s = map.toString(); // "{a:'A',b:{c:'C',d:123}}"

Notes:
  • This class is not thread safe.
See Also:
  • Field Details

  • Constructor Details

    • Json5Map

      public Json5Map()
      Construct an empty map.
    • Json5Map

      public Json5Map(MarshallingSession session)
      Construct an empty map with the specified bean context.
      Parameters:
      session - The bean session to use for creating beans.
    • Json5Map

      public Json5Map(CharSequence json5) throws ParseException
      Construct a map initialized with the specified JSON5 text.
      Parameters:
      json5 - The JSON5 text to parse.
      Throws:
      ParseException - Malformed input encountered.
    • Json5Map

      public Json5Map(CharSequence in, Parser p) throws ParseException
      Construct a map initialized with the specified string.
      Parameters:
      in - The input being parsed. Can be null or empty (results in an empty map; no parsing occurs).
      p - The parser to use. If null, uses Json5Parser.
      Throws:
      ParseException - Malformed input encountered.
    • Json5Map

      public Json5Map(Map<?,?> in)
      Construct a map initialized with the specified map.
      Parameters:
      in - The map to copy.
      Can be null (results in an empty map).
      Keys will be converted to strings using Object.toString().
    • Json5Map

      public Json5Map(Object... keyValuePairs)
      Construct a map initialized with the specified key/value pairs.
      Parameters:
      keyValuePairs - A list of key/value pairs to add to this map.
    • Json5Map

      public Json5Map(Reader json5) throws ParseException
      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

      public Json5Map(Reader in, Parser p) throws ParseException
      Construct a map initialized with the specified reader and parser.
      Parameters:
      in - The reader containing the input being parsed.
      p - The parser to use. If null, uses Json5Parser.
      Throws:
      ParseException - Malformed input encountered.
  • Method Details

    • create

      public static Json5Map create()
      Construct an empty map.
      Returns:
      An empty map.
    • filteredMap

      public static Json5Map filteredMap()
      Construct an empty filtered map.
      Returns:
      An empty map.
    • filteredMap

      public static Json5Map filteredMap(Object... keyValuePairs)
      Construct a filtered map initialized with the specified key/value pairs.

      Same as of(Object...) but calls filtered() on the created map.

      Parameters:
      keyValuePairs - A list of key/value pairs to add to this map.
      Returns:
      A new map, never null.
    • of

      public static Json5Map of(Map<?,?> values)
      Construct a map initialized with the specified map.
      Parameters:
      values - The map to copy.
      Can be null (returns null).
      Keys will be converted to strings using Object.toString().
      Returns:
      A new map or null if the map was null.
    • of

      public static Json5Map of(Object... keyValuePairs)
      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

      public static Json5Map ofString(CharSequence json5) throws ParseException
      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), never null.
      Throws:
      ParseException - Malformed input encountered.
    • ofString

      public static Json5Map ofString(Reader json5) throws ParseException
      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), never null.
      Throws:
      ParseException - Malformed input encountered.
    • ofString

      public static Json5Map ofString(CharSequence in, Parser p) throws ParseException
      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.
      If null, uses Json5Parser.
      Returns:
      A new map (empty if the input was null), never null.
      Throws:
      ParseException - Malformed input encountered.
    • ofString

      public static Json5Map ofString(Reader in, Parser p) throws ParseException
      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.
      If null, uses Json5Parser.
      Returns:
      A new map (empty if the input was null), never null.
      Throws:
      ParseException - Malformed input encountered.
    • append

      public Json5Map append(Map<String,Object> values)
      Description copied from class: MarshalledMap
      Appends all the entries in the specified map to this map.
      Overrides:
      append in class MarshalledMap
      Parameters:
      values - The map to copy. Can be null (no-op).
      Returns:
      This object.
    • append

      public Json5Map append(String key, Object value)
      Description copied from class: MarshalledMap
      Adds an entry to this map.
      Overrides:
      append in class MarshalledMap
      Parameters:
      key - The key.
      value - The value.
      Returns:
      This object.
    • appendFirst

      public <T> Json5Map appendFirst(Predicate<T> test, String key, T... values)
      Description copied from class: MarshalledMap
      Adds the first value that matches the specified predicate.
      Overrides:
      appendFirst in class MarshalledMap
      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

      public Json5Map appendIf(boolean flag, String key, Object value)
      Description copied from class: MarshalledMap
      Add if flag is true.
      Overrides:
      appendIf in class MarshalledMap
      Parameters:
      flag - The flag to check.
      key - The key.
      value - The value.
      Returns:
      This object.
    • appendIf

      public <T> Json5Map appendIf(Predicate<T> test, String key, T value)
      Description copied from class: MarshalledMap
      Add if predicate matches value.
      Overrides:
      appendIf in class MarshalledMap
      Type Parameters:
      T - The value type.
      Parameters:
      test - The predicate to match against.
      key - The key.
      value - The value.
      Returns:
      This object.
    • appendIfAbsent

      public Json5Map appendIfAbsent(String key, Object value)
      Description copied from class: MarshalledMap
      Adds a value in this map if the entry does not exist or the current value is null.
      Overrides:
      appendIfAbsent in class MarshalledMap
      Parameters:
      key - The map key.
      value - The value to set if the current value does not exist or is null.
      Returns:
      This object.
    • appendIfAbsentIf

      public <T> Json5Map appendIfAbsentIf(Predicate<T> predicate, String key, T value)
      Description copied from class: MarshalledMap
      Adds a value in this map if the entry does not exist or the current value is null and the value matches the specified predicate.
      Overrides:
      appendIfAbsentIf in class MarshalledMap
      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 is null.
      Returns:
      This object.
    • exclude

      public Json5Map exclude(String... keys)
      Description copied from class: MarshalledMap
      Returns a copy of this map without the specified keys.
      Overrides:
      exclude in class MarshalledMap
      Parameters:
      keys - The keys of the entries not to copy.
      Returns:
      A new map without the keys and values from this map.
    • filtered

      public Json5Map filtered()
      Description copied from class: MarshalledMap
      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.
      Overrides:
      filtered in class MarshalledMap
      Returns:
      This object.
    • filtered

      public Json5Map filtered(Predicate<Object> value)
      Description copied from class: MarshalledMap
      Enables filtering based on a predicate test.

      If the predicate evaluates to false on values added to this map, the entry will be skipped.

      Overrides:
      filtered in class MarshalledMap
      Parameters:
      value - The value tester predicate.
      Returns:
      This object.
    • findList

      public Json5List findList(String... keys)
      Description copied from class: MarshalledMap
      Returns the first entry that exists converted to a MarshalledList.
      Overrides:
      findList in class MarshalledMap
      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

      public Json5Map findMap(String... keys)
      Description copied from class: MarshalledMap
      Returns the first entry that exists converted to a MarshalledMap.
      Overrides:
      findMap in class MarshalledMap
      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

      public Json5List getList(String key)
      Description copied from class: MarshalledMap
      Returns the specified entry value converted to a MarshalledList.
      Overrides:
      getList in class MarshalledMap
      Parameters:
      key - The key.
      Returns:
      The converted value, or null if the map contains no mapping for this key.
    • getList

      public Json5List getList(String key, boolean createIfNotExists)
      Description copied from class: MarshalledMap
      Same as MarshalledMap.getList(String) but creates a new empty MarshalledList if it doesn't already exist.
      Overrides:
      getList in class MarshalledMap
      Parameters:
      key - The key.
      createIfNotExists - If mapping doesn't already exist, create one with an empty MarshalledList.
      Returns:
      The converted value, or an empty value if the map contains no mapping for this key.
    • getList

      public Json5List getList(String key, Json5List defVal)
      Returns the specified entry value converted to a Json5List.
      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

      public Json5Map getMap(String key)
      Description copied from class: MarshalledMap
      Returns the specified entry value converted to a Map.
      Overrides:
      getMap in class MarshalledMap
      Parameters:
      key - The key.
      Returns:
      The converted value, or null if the map contains no mapping for this key.
    • getMap

      public Json5Map getMap(String key, boolean createIfNotExists)
      Description copied from class: MarshalledMap
      Same as MarshalledMap.getMap(String) but creates a new empty MarshalledMap if it doesn't already exist.
      Overrides:
      getMap in class MarshalledMap
      Parameters:
      key - The key.
      createIfNotExists - If mapping doesn't already exist, create one with an empty MarshalledMap.
      Returns:
      The converted value, or an empty value if the map contains no mapping for this key.
    • getMap

      public Json5Map getMap(String key, Json5Map defVal)
      Returns the specified entry value converted to a Json5Map.
      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

      public Json5Map include(String... keys)
      Description copied from class: MarshalledMap
      Returns a copy of this map with only the specified keys.
      Overrides:
      include in class MarshalledMap
      Parameters:
      keys - The keys of the entries to copy.
      Returns:
      A new map with just the keys and values from this map.
    • inner

      public Json5Map inner(Map<String,Object> inner)
      Description copied from class: MarshalledMap
      Set an inner map in this map to allow for chained get calls.

      If MarshalledMap.get(Object) returns null, then MarshalledMap.get(Object) will be called on the inner map.

      Overrides:
      inner in class MarshalledMap
      Parameters:
      inner - The inner map. Can be null to remove the inner map from an existing map.
      Returns:
      This object.
    • keepAll

      public Json5Map keepAll(String... keys)
      Description copied from class: MarshalledMap
      The opposite of MarshalledMap.removeAll(String...).

      Discards all keys from this map that aren't in the specified list.

      Overrides:
      keepAll in class MarshalledMap
      Parameters:
      keys - The keys to keep.
      Returns:
      This map.
    • modifiable

      public Json5Map modifiable()
      Description copied from class: MarshalledMap
      Returns a modifiable copy of this map if it's unmodifiable.
      Overrides:
      modifiable in class MarshalledMap
      Returns:
      A modifiable copy of this map if it's unmodifiable, or this map if it is already modifiable.
    • newMarshalledMap

      Description copied from class: MarshalledMap
      Factory hook used by MarshalledMap.cast(Class) / MarshalledMap.cast(ClassMeta) when the target map type cannot be instantiated directly (e.g. when the caller asks for the Map interface).

      Subclasses should override to return an instance of the matching XMap variant so that callers chaining off of cast(Map.class) still see their flavored map type.

      Overrides:
      newMarshalledMap in class MarshalledMap
      Parameters:
      bs - The marshalling session to associate with the new map.
      Returns:
      A new map instance.
    • putJson5

      public void putJson5(String key, String json5) throws ParseException
      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: MarshalledMap
      Override 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:
      session in class MarshalledMap
      Parameters:
      session - The new bean session.
      Returns:
      This object.
    • setBeanSession

      Description copied from class: MarshalledMap
      Sets the MarshallingSession currently associated with this map.
      Overrides:
      setBeanSession in class MarshalledMap
      Parameters:
      value - The MarshallingSession currently associated with this map.
      Returns:
      This object.
    • toJson5

      public String 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 using Json5Serializer.DEFAULT_READABLE.
      Returns:
      This object serialized as a readable JSON5 string.
    • toString

      public String toString()
      Overrides:
      toString in class AbstractMap<String,Object>
    • unmodifiable

      Description copied from class: MarshalledMap
      Returns an unmodifiable copy of this map if it's modifiable.
      Overrides:
      unmodifiable in class MarshalledMap
      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 specified Writer using the Json5.DEFAULT marshaller.
      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.