Package org.pipservices3.commons.convert
Class JsonConverter
java.lang.Object
org.pipservices3.commons.convert.JsonConverter
Converts arbitrary values from and to JSON (JavaScript Object Notation) strings.
### Example ###
T value1 = JsonConverter.fromJson("{\"key\":123}"); // Result: { key: 123 }
T value2 = JsonConverter.toMap({ key: 123}); // Result: "{\"key\":123}"
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TConverts JSON string into a value of type specified by Class type.static StringConverts value into JSON string.Converts JSON string into map object or returns empty map when conversion is not possible.toMapWithDefault(String value, Map<String, Object> defaultValue) Converts JSON string into map object or returns default value when conversion is not possible.toNullableMap(String value) Converts JSON string into map object or returns null when conversion is not possible.
-
Constructor Details
-
JsonConverter
public JsonConverter()
-
-
Method Details
-
fromJson
public static <T> T fromJson(Class<T> type, String value) throws com.fasterxml.jackson.databind.JsonMappingException, com.fasterxml.jackson.core.JsonParseException, IOException Converts JSON string into a value of type specified by Class type.- Parameters:
type- the Class type for the data type into which 'value' is to be converted.value- the JSON string to convert.- Returns:
- converted object value or null when value is null.
- Throws:
com.fasterxml.jackson.databind.JsonMappingException- when conversion fails for mapping reason.com.fasterxml.jackson.core.JsonParseException- when conversion fails for parse reason.IOException- when conversion fails for input/output stream reason.
-
toJson
Converts value into JSON string.- Parameters:
value- the value to convert.- Returns:
- JSON string or null when value is null.
- Throws:
com.fasterxml.jackson.core.JsonProcessingException- when conversion fails for any reason.
-
toNullableMap
Converts JSON string into map object or returns null when conversion is not possible.- Parameters:
value- the JSON string to convert.- Returns:
- Map object value or null when conversion is not supported.
- See Also:
-
toMap
Converts JSON string into map object or returns empty map when conversion is not possible.- Parameters:
value- the JSON string to convert.- Returns:
- Map object value or empty object when conversion is not supported.
- See Also:
-
toMapWithDefault
Converts JSON string into map object or returns default value when conversion is not possible.- Parameters:
value- the JSON string to convert.defaultValue- the default value.- Returns:
- Map object value or default when conversion is not supported.
- See Also:
-