Class JsonSerialization
- java.lang.Object
-
- com.launchdarkly.sdk.json.JsonSerialization
-
public abstract class JsonSerialization extends java.lang.Object
Helper methods for JSON serialization of SDK classes.While the LaunchDarkly Java-based SDKs have used Gson internally in the past, they may not always do so-- and even if they do, some SDK distributions may embed their own copy of Gson with modified (shaded) class names so that it does not conflict with any Gson instance elsewhere in the classpath. For both of those reasons, applications should not assume that
Gson.toGson()
andGson.fromGson()
-- or any other JSON framework that is based on reflection-- will work correctly for SDK classes, whose correct JSON representations do not necessarily correspond to their internal field layout. Instead, they should always use one of the following:- The
JsonSerialization
methods. - A Gson instance that has been configured with
LDGson
. - For
LDValue
, you may also use the convenience methodsLDValue.toJsonString()
andLDValue.parse(String)
.
- The
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends JsonSerializable>
Tdeserialize(java.lang.String json, java.lang.Class<T> objectClass)
Parses an object from its JSON representation.static <T extends JsonSerializable>
java.lang.Stringserialize(T instance)
Converts an object to its JSON representation.
-
-
-
Method Detail
-
serialize
public static <T extends JsonSerializable> java.lang.String serialize(T instance)
Converts an object to its JSON representation.This is only usable for classes that have the
JsonSerializable
marker interface, indicating that the SDK knows how to serialize them.- Type Parameters:
T
- class of the object being serialized- Parameters:
instance
- the instance to serialize- Returns:
- the object's JSON encoding as a string
-
deserialize
public static <T extends JsonSerializable> T deserialize(java.lang.String json, java.lang.Class<T> objectClass) throws SerializationException
Parses an object from its JSON representation.This is only usable for classes that have the
JsonSerializable
marker interface, indicating that the SDK knows how to serialize them.The current implementation is limited in its ability to handle generic types. Currently, the only such type defined by the SDKs is
EvaluationDetail
. You can serialize anyEvaluationDetail<T>
instance and it will represent theT
value correctly, but when deserializing, you will always getEvaluationDetail<LDValue>
.- Type Parameters:
T
- class of the object being deserialized- Parameters:
json
- the object's JSON encoding as a stringobjectClass
- class of the object being deserialized- Returns:
- the deserialized instance
- Throws:
SerializationException
- if the JSON encoding was invalid
-
-