Class LDGson
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. This class addresses that issue
for applications that prefer to use Gson for everything rather than calling
JsonSerialization
for individual objects.
An application that wishes to use Gson to serialize or deserialize classes from the SDK should
configure its Gson
instance as follows:
import com.launchdarkly.sdk.json.LDGson;
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(LDGson.typeAdapters())
// any other GsonBuilder options go here
.create();
This causes Gson to use the correct JSON representation logic (the same that would be used by
JsonSerialization
) for any types that have the SDK's JsonSerializable
marker
interface, such as LDContext
and LDValue
, regardless of whether they are the
top-level object being serialized or are contained in something else such as a collection. It
does not affect Gson's behavior for any other classes.
Note that some of the LaunchDarkly SDK distributions deliberately do not expose Gson as a
dependency, so if you are using Gson in your application you will need to make sure you have
defined your own dependency on it. Referencing LDGson
will cause a runtime
exception if Gson is not in the caller's classpath.
-
Method Summary
Modifier and TypeMethodDescriptionstatic com.google.gson.TypeAdapterFactory
Returns a GsonTypeAdapterFactory
that defines the correct serialization and deserialization behavior for all LaunchDarkly SDK objects that implementJsonSerializable
.static <T> Map<T,
com.google.gson.JsonElement> valueMapToJsonElementMap
(Map<T, LDValue> valueMap) Convenience method for converting a map ofLDValue
values to a map of GsonJsonElement
s.static com.google.gson.JsonElement
valueToJsonElement
(LDValue value) Returns a GsonJsonElement
that is equivalent to the specifiedLDValue
.
-
Method Details
-
typeAdapters
public static com.google.gson.TypeAdapterFactory typeAdapters()Returns a GsonTypeAdapterFactory
that defines the correct serialization and deserialization behavior for all LaunchDarkly SDK objects that implementJsonSerializable
.import com.launchdarkly.sdk.json.LDGson; Gson gson = new GsonBuilder() .registerTypeAdapterFactory(LDGson.typeAdapters()) // any other GsonBuilder options go here .create();
- Returns:
- a
TypeAdapterFactory
-
valueToJsonElement
Returns a GsonJsonElement
that is equivalent to the specifiedLDValue
.This is slightly more efficient than using
Gson.toJsonTree()
.- Parameters:
value
- anLDValue
(null
is treated as equivalent toLDValue.ofNull()
)- Returns:
- a Gson
JsonElement
(may be aJsonNull
but will never benull
)
-
valueMapToJsonElementMap
public static <T> Map<T,com.google.gson.JsonElement> valueMapToJsonElementMap(Map<T, LDValue> valueMap) Convenience method for converting a map ofLDValue
values to a map of GsonJsonElement
s.- Type Parameters:
T
- type of the map's keys- Parameters:
valueMap
- a map containingLDValue
values- Returns:
- an equivalent map containing Gson
JsonElement
values
-