The MapConverter class allows you to convert arbitrary values into map objects using extended conversion rules.
Description
The MapConverter class allows you to convert arbitrary values into map objects using the following extended conversion rules:
- Objects: property names as keys, property values as values
- Arrays: element indexes as keys, elements as values
Static methods
toMap
Converts a value into a map object or returns an empty map when the conversion is not possible.
public static
Map<String, Object> toMap(Object value)
- value: Object - value to convert.
- returns: Map<String, Object> - map object or empty map when the conversion is not supported.
toMapWithDefault
Converts a value into a map object or returns a given default when the conversion is not possible.
public static
Map<String, Object> toMapWithDefault(Object value, Map<String, Object> defaultValue)
- value: Object - value to convert.
- defaultValue: Map<String, Object> - default value.
- returns: Map<String, Object> - map object or empty map when the conversion is not supported.
toNullableMap
Converts a value into a map object or returns null when the conversion is not possible.
public static
Map<String, Object> toNullableMap(Object value)
- value: Object - value to convert.
- returns: Map<String, Object> - map object or null when the conversion is not supported.
Examples
{
Map<String, Object> value1 = MapConverted.toNullableMap("ABC"); // Result: null
Map<String, Object> value2 = MapConverted.toNullableMap({ key: 123 }); // Result: { key: 123 }
Map<String, Object> value3 = MapConverted.toNullableMap(new int[]{1, 2, 3}); // Result: { "0": 1, "1": 2, "2": 3 }
}