Package org.forgerock.am.cts.utils
Interface JSONSerialisation
public interface JSONSerialisation
Responsible for serialising and deserialising objects to and from JSON.
Note: This serialisation mechanism uses Jackson's ability to detect fields in an object to serialise.
It does not use getters and setters (JavaBean based) which is the default. See the configuration that
is defined in the constructor for more details.
Note: Important caveat, JSON does not handle Maps with keys that are not strings (Enums, Objects etc). This
is covered here http://stackoverflow.com/questions/11246748/deserializing-non-string-map-keys-with-jackson?lq=1
the recommendation is to switch the map to use strings as keys and convert accordingly. If necessary it is
possible to implement custom KeySerializer/KeyDeserializer implementations to ensure the map keys are correctly
handled during serialisation and deserialisation. The default KeySerializer implementation calls toString on the
key object, which may be suitable in certain cases.
Note: Another Important caveat, It appears that Jackson basically handles poorly any Object that is not a
String, Integer, Map or a List. Based on this I cannot recommend this class as a general purpose Object to JSON
serialisation class, unless the caller makes extra effort to ensure that their objects are being
serialised following the above guidelines.
-
Method Summary
Modifier and TypeMethodDescription<T> T
deserialise
(String text, Class<T> clazz) Deserialise JSON to an object of type T.static String
jsonAttributeName
(String name) Wrap the attribute name in quotes and a colon to make it look like a JSON attribute.<T> String
serialise
(T object) Serialise an object to JSON.
-
Method Details
-
serialise
Serialise an object to JSON.- Type Parameters:
T
- The generic type of the passed in object.- Parameters:
object
- Non null object to serialise.- Returns:
- Non null JSON text.
-
deserialise
Deserialise JSON to an object of type T.- Type Parameters:
T
- Type to cast the created object to when deserialising.- Parameters:
text
- Non null JSON text to parse and deserialise.clazz
- Class which contains the type of the value stored in JSON, required for deserialsiation.- Returns:
- Non null object of type T.
-
jsonAttributeName
Wrap the attribute name in quotes and a colon to make it look like a JSON attribute.- Parameters:
name
- Non null text to wrap.- Returns:
- A string that represents a JSON Attribute Name
-