Class JsonUtils

java.lang.Object
de.iip_ecosphere.platform.support.json.JsonUtils

public class JsonUtils extends Object
Some JSON utility methods, also reading/writing of specific types.
Author:
Holger Eichelberger, SSE, Lemur Project (BSD License)
  • Constructor Details

    • JsonUtils

      public JsonUtils()
  • Method Details

    • toJson

      public static String toJson(Object obj)
      Turns an object to JSON.
      Parameters:
      obj - the object (may be null), must have getters/setters for all attributes and a no-arg constructor no-arg constructor
      Returns:
      the JSON string or an empty string in case of problems/no address
      See Also:
    • fromJson

      public static <R> R fromJson(Object json, Class<R> cls)
      Reads an Object from a JSON string.
      Type Parameters:
      R - the object type, must have getters/setters for all attributes and a no-arg constructor
      Parameters:
      json - the JSON value (usually a String)
      cls - the class of the type to read
      Returns:
      the server address or null if reading fails
      See Also:
    • listFromJson

      public static <R> List<R> listFromJson(Object json, Class<R> cls)
      Reads a typed List from a JSON string.
      Type Parameters:
      R - the entity type
      Parameters:
      json - the JSON value (usually a String)
      cls - the class of the entity type to read
      Returns:
      the list or null if reading fails
      See Also:
    • mapFromJson

      public static <K, V> Map<K,V> mapFromJson(Object json, Class<K> keyCls, Class<K> valueCls)
      Reads a typed Map from a JSON string.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      json - the JSON value (usually a String)
      keyCls - the class of the key type to read
      valueCls - the class of the value type to read
      Returns:
      the map or null if reading fails
      See Also:
    • escape

      public static String escape(String input)
      Escapes an input string for JSON. Taken over from Stackoverflow and Lemur Project. The respective methods from Apache Commons Lang3 are too slow for our purpose.
      Parameters:
      input - the input string
      Returns:
      the escaped string
    • unescape

      public static String unescape(String input)
      Unescapes an input string from JSON. Taken over from Stackoverflow and Lemur Project. The respective methods from Apache Commons Lang3 are too slow for our purpose.
      Parameters:
      input - the input string
      Returns:
      the unescaped string