Class JsonMap

All Implemented Interfaces:
Serializable, Cloneable, Map<String,Object>
Direct Known Subclasses:
Args, DelegateMap, ManifestFile, ResolvingJsonMap

public class JsonMap extends LinkedHashMap<String,Object>
Java implementation of a JSON 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 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:

// Construct an empty Map JsonMap map = JsonMap.of(); // Construct a Map from JSON map = JsonMap.ofJson("{a:'A',b:{c:'C',d:123}}"); // Construct a Map using the append method map = JsonMap.of().a("foo","x").a("bar",123).a("baz",true); // Construct a Map from XML generated by XmlSerializer String xml = "<object><a type='string'>A</a><b type='object'><c type='string'>C</c><d type='number'>123</d></b></object>"; map = JsonMap.of(xml, XmlParser.DEFAULT); // Construct a Map from a URL GET parameter string generated by UrlEncodingParser String urlParams = "?a='A'&b={c:'C',d:123}"; map = JsonMap.of(urlParams, UrlEncodingParser.DEFAULT); // Construct JSON from JsonMap map = JsonMap.ofJson("{foo:'bar'},{baz:[123,true]}"); String json = map.toString(); // Produces "{foo:'bar'},{baz:[123,true]}" json = map.toString(JsonSerializer.DEFAULT); // Equivalent json = JsonSerializer.DEFAULT.serialize(map); // Equivalent // Get a map entry as an Integer map = JsonMap.ofJson("{foo:123}"); Integer integer = map.getInt("foo"); integer = map.get(Integer.class, "foo"); // Equivalent // Get a map entry as a Float map = JsonMap.ofJson("{foo:123}"); Float _float = map.getFloat("foo"); _float = map.get(Float.class, "foo"); // Equivalent // Same as above, except converted to a String map = JsonMap.ofJson("{foo:123}"); String string = map.getString("foo"); // Returns "123" string = map.get(String.class, "foo"); // Equivalent // Get one of the entries in the list as a bean (converted to a bean if it isn't already one) map = JsonMap.ofJson("{person:{name:'John Smith',age:45}}"); Person person = map.get(Person.class, "person"); // Add an inner map JsonMap map1 = JsonMap.ofJson("{a:1}"); JsonMap map2 = JsonMap.ofJson("{b:2}").setInner(map1); int _int = map2.getInt("a"); // a == 1

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

  • Constructor Details

    • JsonMap

      public JsonMap()
      Construct an empty map.
    • JsonMap

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

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

      public JsonMap(CharSequence json) throws ParseException
      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

      public JsonMap(CharSequence in, Parser p) throws ParseException
      Construct a map initialized with the specified string.
      Parameters:
      in - The input being parsed.
      Can be null.
      p - The parser to use to parse the input.
      If null, uses JsonParser.
      Throws:
      ParseException - Malformed input encountered.
    • JsonMap

      public JsonMap(Reader json) throws ParseException
      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

      public JsonMap(Reader in, Parser p) throws ParseException
      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.
      If null, uses JsonParser.
      Throws:
      ParseException - Malformed input encountered.
    • JsonMap

      public JsonMap(Object... keyValuePairs)
      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

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

      public static JsonMap filteredMap()
      Construct an empty map.
      Returns:
      An empty map.
    • of

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

      public static JsonMap ofJson(CharSequence json) throws ParseException
      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

      public static JsonMap ofText(CharSequence in, Parser p) throws ParseException
      Construct a map initialized with the specified string.
      Parameters:
      in - The input being parsed.
      Can be null.
      p - The parser to use to parse the input.
      If null, uses JsonParser.
      Returns:
      A new map or null if the input was null.
      Throws:
      ParseException - Malformed input encountered.
    • ofJson

      public static JsonMap ofJson(Reader json) throws ParseException
      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 was null.
      Throws:
      ParseException - Malformed input encountered.
    • ofText

      public static JsonMap ofText(Reader in, Parser p) throws ParseException
      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.
      If null, uses JsonParser.
      Returns:
      A new map or null if the input was null.
      Throws:
      ParseException - Malformed input encountered.
    • of

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

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

      Same as of(Object...) but calls filtered() 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

      public JsonMap inner(Map<String,Object> inner)
      Set an inner map in this map to allow for chained get calls.

      If get(Object) returns null, then 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.

      JsonMap map1 = JsonMap.ofJson("{foo:1}"); JsonMap map2 = 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 be null to remove the inner map from an existing map.
      Returns:
      This object.
    • session

      public JsonMap session(BeanSession 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

      public JsonMap append(String key, Object value)
      Adds an entry to this map.
      Parameters:
      key - The key.
      value - The value.
      Returns:
      This object.
    • append

      public JsonMap append(Map<String,Object> values)
      Appends all the entries in the specified map to this map.
      Parameters:
      values - The map to copy. Can be null.
      Returns:
      This object.
    • appendIf

      public JsonMap appendIf(boolean flag, String key, Object value)
      Add if flag is true.
      Parameters:
      flag - The flag to check.
      key - The key.
      value - The value.
      Returns:
      This object.
    • appendIf

      public <T> JsonMap appendIf(Predicate<T> test, String key, T value)
      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

      @SafeVarargs public final <T> JsonMap appendFirst(Predicate<T> test, String key, T... values)
      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

      public JsonMap appendIfAbsent(String key, Object value)
      Adds a value in this map if the entry does not exist or the current value is null.
      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> 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 is null 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 is null.
      Returns:
      This object.
    • filtered

      public JsonMap 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

      public JsonMap filtered(Predicate<Object> value)
      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

      public <T> T get(String key, Class<T> type)
      Same as get(), 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. String string = map.get("key1", String.class); // Value converted to a bean. MyBean bean = 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. List list = map.get("key4", LinkedList.class); // Value converted to a map of object keys/values. Map map2 = 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

      public <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.

      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.
    • getWithDefault

      public Object getWithDefault(String key, Object def)
      Same as get(), 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

      public <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.
      Type Parameters:
      T - The class type returned.
      Parameters:
      key - The key.
      def - The default value. Can be null.
      type - The class type returned.
      Returns:
      The value, or null if the entry doesn't exist.
    • getWithDefault

      public <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.
      Type Parameters:
      T - The class type returned.
      Parameters:
      key - The key.
      def - The default value. Can be null.
      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

      public <T> T getSwapped(String key, ObjectSwap<T,?> objectSwap) throws ParseException
      Same as get(), 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

      public Object find(String... keys)
      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

      public <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.

      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

      public String getString(String key)
      Returns the specified entry value converted to a String.

      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

      public String[] getStringArray(String key)
      Returns the specified entry value converted to a String.

      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

      public String[] getStringArray(String key, String[] def)
      Same as getStringArray(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

      public String getString(String key, String defVal)
      Returns the specified entry value converted to a String.

      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

      public Integer getInt(String key)
      Returns the specified entry value converted to an Integer.

      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

      public Integer getInt(String key, Integer defVal)
      Returns the specified entry value converted to an Integer.

      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

      public Long getLong(String key)
      Returns the specified entry value converted to a Long.

      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

      public Long getLong(String key, Long defVal)
      Returns the specified entry value converted to a Long.

      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

      public Boolean getBoolean(String key)
      Returns the specified entry value converted to a Boolean.

      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

      public Boolean getBoolean(String key, Boolean defVal)
      Returns the specified entry value converted to a Boolean.

      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

      public JsonMap getMap(String key)
      Returns the specified entry value converted to a Map.

      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

      public JsonMap getMap(String key, JsonMap defVal)
      Returns the specified entry value converted to a JsonMap.

      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

      public JsonMap getMap(String key, boolean createIfNotExists)
      Same as getMap(String) but creates a new empty JsonMap if it doesn't already exist.
      Parameters:
      key - The key.
      createIfNotExists - If mapping doesn't already exist, create one with an empty JsonMap.
      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

      public <K, V> Map<K,V> getMap(String key, Class<K> keyType, Class<V> valType, Map<K,V> def)
      Same as getMap(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

      public JsonList getList(String key)
      Returns the specified entry value converted to a JsonList.

      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

      public JsonList getList(String key, JsonList defVal)
      Returns the specified entry value converted to a JsonList.

      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

      public JsonList getList(String key, boolean createIfNotExists)
      Same as getList(String) but creates a new empty JsonList if it doesn't already exist.
      Parameters:
      key - The key.
      createIfNotExists - If mapping doesn't already exist, create one with an empty JsonList.
      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

      public <E> List<E> getList(String key, Class<E> elementType, List<E> def)
      Same as getList(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

      public String findString(String... keys)
      Returns the first entry that exists converted to a String.

      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

      public Integer findInt(String... keys)
      Returns the first entry that exists converted to an Integer.

      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

      public Long findLong(String... keys)
      Returns the first entry that exists converted to a Long.

      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

      public Boolean findBoolean(String... keys)
      Returns the first entry that exists converted to a Boolean.

      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

      public JsonMap findMap(String... keys)
      Returns the first entry that exists converted to a JsonMap.

      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

      public JsonList findList(String... keys)
      Returns the first entry that exists converted to a JsonList.

      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

      public String getFirstKey()
      Returns the first key in the map.
      Returns:
      The first key in the map, or null if the map is empty.
    • getClassMeta

      public ClassMeta<?> getClassMeta(String key)
      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

      public <T> T removeWithDefault(String key, T defVal, Class<T> type)
      Equivalent to calling get(class,key,def) followed by remove(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

      public String removeString(String key)
      Equivalent to calling removeWithDefault(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

      public String removeString(String key, String def)
      Equivalent to calling removeWithDefault(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

      public Integer removeInt(String key)
      Equivalent to calling removeWithDefault(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

      public Integer removeInt(String key, Integer def)
      Equivalent to calling removeWithDefault(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 calling removeWithDefault(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

      public Boolean removeBoolean(String key, Boolean def)
      Equivalent to calling removeWithDefault(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

      public void removeAll(Collection<String> keys)
      Convenience method for removing several keys at once.
      Parameters:
      keys - The list of keys to remove.
    • removeAll

      public void removeAll(String... keys)
      Convenience method for removing several keys at once.
      Parameters:
      keys - The list of keys to remove.
    • keepAll

      public JsonMap keepAll(String... keys)
      The opposite of removeAll(String...).

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

      Parameters:
      keys - The keys to keep.
      Returns:
      This map.
    • containsKeyNotEmpty

      public boolean containsKeyNotEmpty(String key)
      Returns true 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 a CharSequence.

      Parameters:
      key - The key.
      Returns:
      true if the map contains the specified entry and the value is not null nor an empty string.
    • containsOuterKey

      public boolean containsOuterKey(Object key)
      Returns true 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

      public JsonMap include(String... keys)
      Returns a copy of this JsonMap 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

      public JsonMap exclude(String... keys)
      Returns a copy of this JsonMap 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

      public <T> T cast(Class<T> type)
      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 the type.

      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 from type
    • cast

      public <T> T cast(ClassMeta<T> cm)
      Same as cast(Class), except allows you to specify a ClassMeta 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 from type
    • getAt

      public <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.

      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 the ObjectRest 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

      public <T> T getAt(String path, Type type, Type... args)
      Same as getAt(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 the ObjectRest 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

      public 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.

      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 the ObjectRest 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

      public Object postAt(String path, Object o)
      Similar to putAt(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 the ObjectRest 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

      public Object deleteAt(String path)
      Similar to remove(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 the ObjectRest 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

      public Object put(String key, Object value)
      Specified by:
      put in interface Map<String,Object>
      Overrides:
      put in class HashMap<String,Object>
    • getBeanSession

      Returns the BeanSession currently associated with this map.
      Returns:
      The BeanSession currently associated with this map.
    • setBeanSession

      Sets the BeanSession currently associated with this map.
      Parameters:
      value - The BeanSession currently associated with this map.
      Returns:
      This object.
    • putJson

      public void putJson(String key, String json) throws ParseException
      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

      public String asString(WriterSerializer serializer)
      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

      public String asString()
      Serialize this object to Simplified JSON using Json5Serializer.DEFAULT.
      Returns:
      This object serialized as a string.
    • asReadableString

      Serialize this object to Simplified JSON using Json5Serializer.DEFAULT_READABLE.
      Returns:
      This object serialized as a string.
    • writeTo

      Convenience method for serializing this map to the specified Writer using the JsonSerializer.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

      public boolean isUnmodifiable()
      Returns true if this map is unmodifiable.
      Returns:
      true if this map is unmodifiable.
    • modifiable

      public JsonMap 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

      public Object get(Object key)
      Specified by:
      get in interface Map<String,Object>
      Overrides:
      get in class LinkedHashMap<String,Object>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<String,Object>
      Overrides:
      containsKey in class HashMap<String,Object>
    • keySet

      public Set<String> keySet()
      Specified by:
      keySet in interface Map<String,Object>
      Overrides:
      keySet in class LinkedHashMap<String,Object>
    • entrySet

      Specified by:
      entrySet in interface Map<String,Object>
      Overrides:
      entrySet in class LinkedHashMap<String,Object>
    • asJson

      public String asJson()
      A synonym for toString()
      Returns:
      This object as a JSON string.
    • toString

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