Class JsonSerialization

java.lang.Object
com.launchdarkly.sdk.json.JsonSerialization

public abstract class JsonSerialization extends Object
Helper methods for JSON serialization of SDK classes.

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:

  1. The JsonSerialization methods.
  2. A Gson instance that has been configured with LDGson.
  3. For LDValue, you may also use the convenience methods LDValue.toJsonString() and LDValue.parse(String).
  • Method Details

    • serialize

      public static <T extends JsonSerializable> String serialize(T instance)
      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 any EvaluationDetail<T> instance and it will represent the T value correctly, but when deserializing, you will always get EvaluationDetail<LDValue>.

      Type Parameters:
      T - class of the object being deserialized
      Parameters:
      json - the object's JSON encoding as a string
      objectClass - class of the object being deserialized
      Returns:
      the deserialized instance
      Throws:
      SerializationException - if the JSON encoding was invalid