Class JsonConverter

java.lang.Object
org.pipservices3.commons.convert.JsonConverter

public class JsonConverter extends Object
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 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

      public static String toJson(Object value) throws com.fasterxml.jackson.core.JsonProcessingException
      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

      public static Map<String,Object> toNullableMap(String value)
      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

      public static Map<String,Object> toMap(String value)
      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

      public static Map<String,Object> toMapWithDefault(String value, Map<String,Object> defaultValue)
      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: