public abstract class LDGson
extends java.lang.Object
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.
Modifier and Type | Method and Description |
---|---|
static com.google.gson.TypeAdapterFactory |
typeAdapters()
Returns a Gson
TypeAdapterFactory that defines the correct serialization and
deserialization behavior for all LaunchDarkly SDK objects that implement JsonSerializable . |
static <T> java.util.Map<T,com.google.gson.JsonElement> |
valueMapToJsonElementMap(java.util.Map<T,LDValue> valueMap)
Convenience method for converting a map of
LDValue values to a map of Gson JsonElement s. |
static com.google.gson.JsonElement |
valueToJsonElement(LDValue value)
Returns a Gson
JsonElement that is equivalent to the specified LDValue . |
public static com.google.gson.TypeAdapterFactory typeAdapters()
TypeAdapterFactory
that defines the correct serialization and
deserialization behavior for all LaunchDarkly SDK objects that implement JsonSerializable
.
import com.launchdarkly.sdk.json.LDGson;
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(LDGson.typeAdapters())
// any other GsonBuilder options go here
.create();
TypeAdapterFactory
public static com.google.gson.JsonElement valueToJsonElement(LDValue value)
JsonElement
that is equivalent to the specified LDValue
.
This is slightly more efficient than using Gson.toJsonTree()
.
value
- an LDValue
(null
is treated as equivalent to LDValue.ofNull()
)JsonElement
(may be a JsonNull
but will never be null
)public static <T> java.util.Map<T,com.google.gson.JsonElement> valueMapToJsonElementMap(java.util.Map<T,LDValue> valueMap)
LDValue
values to a map of Gson JsonElement
s.T
- type of the map's keysvalueMap
- a map containing LDValue
valuesJsonElement
values