Class JsonHandler

java.lang.Object
ch.ralscha.extdirectspring.util.JsonHandler

public class JsonHandler extends Object
Object contains an ObjectMapper and provides convenient methods.
  • Constructor Details

    • JsonHandler

      public JsonHandler()
    • JsonHandler

      public JsonHandler(com.fasterxml.jackson.databind.ObjectMapper mapper)
  • Method Details

    • setMapper

      public void setMapper(com.fasterxml.jackson.databind.ObjectMapper mapper)
      Sets a new instance of ObjectMapper.
      Parameters:
      mapper - a new object mapper. must not be null
    • getMapper

      public com.fasterxml.jackson.databind.ObjectMapper getMapper()
      Returns:
      the currently assigned ObjectMapper
    • writeValueAsString

      public String writeValueAsString(Object obj)
      Converts an object into a JSON string. In case of an exception returns null and logs the exception.
      Parameters:
      obj - the source object
      Returns:
      obj JSON string, null if an exception occurred
    • writeValueAsString

      public String writeValueAsString(Object obj, boolean indent)
      Converts an object into a JSON string. In case of an exceptions returns null and logs the exception.
      Parameters:
      obj - the source object
      indent - if true JSON is written in a human readable format, if false JSON is written on one line
      Returns:
      obj JSON string, null if an exception occurred
    • readValue

      public <T> T readValue(String json, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
      Converts a JSON string into an object. In case of an exception returns null and logs the exception.
      Type Parameters:
      T - type of the object to create
      Parameters:
      json - string with the JSON
      typeReference - TypeReference instance of the desired result type TypeReference
      Returns:
      the created object, null if there was an exception
    • readValue

      public <T> T readValue(String json, Class<T> clazz)
      Converts a JSON string into an object. In case of an exception returns null and logs the exception.
      Type Parameters:
      T - type of the object to create
      Parameters:
      json - string with the JSON
      clazz - class of object to create
      Returns:
      the converted object, null if there is an exception
    • readValue

      public Object readValue(InputStream is, Class<Object> clazz)
      Converts a JSON string into an object. The input is read from an InputStream. In case of an exception returns null and logs the exception.
      Parameters:
      is - a InputStream
      clazz - class of object to create
      Returns:
      the converted object, null if there is an exception
    • convertValue

      public <T> T convertValue(Object object, Class<T> clazz)
      Converts one object into another.
      Parameters:
      object - the source
      clazz - the type of the target
      Returns:
      the converted object
    • convertValue

      public <T> T convertValue(Object object, com.fasterxml.jackson.databind.JavaType toValueTypeRef)
      Converts one object into another.
      Parameters:
      object - the source
      toValueTypeRef - the type of the target
      Returns:
      the converted object