Class JsonSerialization


  • public abstract class JsonSerialization
    extends java.lang.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 Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends JsonSerializable>
      T
      deserialize​(java.lang.String json, java.lang.Class<T> objectClass)
      Parses an object from its JSON representation.
      static <T extends JsonSerializable>
      java.lang.String
      serialize​(T instance)
      Converts an object to its JSON representation.
      • Methods inherited from class java.lang.Object

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

      • serialize

        public static <T extends JsonSerializable> java.lang.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​(java.lang.String json,
                                                                 java.lang.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