Class LDGson


  • public abstract class LDGson
    extends java.lang.Object
    A 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() 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

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method 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 JsonElements.
      static com.google.gson.JsonElement valueToJsonElement​(LDValue value)
      Returns a Gson JsonElement that is equivalent to the specified LDValue.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • typeAdapters

        public 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.
        
             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 Gson JsonElement that is equivalent to the specified LDValue.

        This is slightly more efficient than using Gson.toJsonTree().

        Parameters:
        value - an LDValue (null is treated as equivalent to LDValue.ofNull())
        Returns:
        a Gson JsonElement (may be a JsonNull but will never be null)
      • 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 of LDValue values to a map of Gson JsonElements.
        Type Parameters:
        T - type of the map's keys
        Parameters:
        valueMap - a map containing LDValue values
        Returns:
        an equivalent map containing Gson JsonElement values