Class JsonUtils
java.lang.Object
de.iip_ecosphere.platform.support.json.JsonUtils
Some JSON utility methods, also reading/writing of specific types.
- Author:
- Holger Eichelberger, SSE, Lemur Project (BSD License)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringEscapes an input string for JSON.static <R> RReads an Object from a JSON string.static <R> List<R> listFromJson(Object json, Class<R> cls) Reads a typed List from a JSON string.static <K,V> Map <K, V> mapFromJson(Object json, Class<K> keyCls, Class<K> valueCls) Reads a typed Map from a JSON string.static StringTurns anobjectto JSON.static StringUnescapes an input string from JSON.
-
Constructor Details
-
JsonUtils
public JsonUtils()
-
-
Method Details
-
toJson
Turns anobjectto 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
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
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
Reads a typed Map from a JSON string.- Type Parameters:
K- the key typeV- the value type- Parameters:
json- the JSON value (usually a String)keyCls- the class of the key type to readvalueCls- the class of the value type to read- Returns:
- the map or null if reading fails
- See Also:
-
escape
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
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
-