Class JsonSerialization
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()
and Gson.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)
.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends JsonSerializable>
Tdeserialize
(String json, Class<T> objectClass) Parses an object from its JSON representation.static <T extends JsonSerializable>
Stringserialize
(T instance) Converts an object to its JSON representation.
-
Method Details
-
serialize
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(String json, 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
-