Class LDGson
- java.lang.Object
-
- com.launchdarkly.sdk.json.LDGson
-
public abstract class LDGson extends java.lang.ObjectA helper class for interoperability with application code that uses Gson.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. This class addresses that issue for applications that prefer to use Gson for everything rather than callingJsonSerializationfor individual objects.An application that wishes to use Gson to serialize or deserialize classes from the SDK should configure its
Gsoninstance 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'sJsonSerializablemarker interface, such asLDContextandLDValue, 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
LDGsonwill cause a runtime exception if Gson is not in the caller's classpath.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static com.google.gson.TypeAdapterFactorytypeAdapters()Returns a GsonTypeAdapterFactorythat defines the correct serialization and deserialization behavior for all LaunchDarkly SDK objects that implementJsonSerializable.static <T> java.util.Map<T,com.google.gson.JsonElement>valueMapToJsonElementMap(java.util.Map<T,LDValue> valueMap)Convenience method for converting a map ofLDValuevalues to a map of GsonJsonElements.static com.google.gson.JsonElementvalueToJsonElement(LDValue value)Returns a GsonJsonElementthat is equivalent to the specifiedLDValue.
-
-
-
Method Detail
-
typeAdapters
public static com.google.gson.TypeAdapterFactory typeAdapters()
Returns a GsonTypeAdapterFactorythat 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
public static com.google.gson.JsonElement valueToJsonElement(LDValue value)
Returns a GsonJsonElementthat is equivalent to the specifiedLDValue.This is slightly more efficient than using
Gson.toJsonTree().- Parameters:
value- anLDValue(nullis treated as equivalent toLDValue.ofNull())- Returns:
- a Gson
JsonElement(may be aJsonNullbut will never benull)
-
valueMapToJsonElementMap
public static <T> java.util.Map<T,com.google.gson.JsonElement> valueMapToJsonElementMap(java.util.Map<T,LDValue> valueMap)
Convenience method for converting a map ofLDValuevalues to a map of GsonJsonElements.- Type Parameters:
T- type of the map's keys- Parameters:
valueMap- a map containingLDValuevalues- Returns:
- an equivalent map containing Gson
JsonElementvalues
-
-