Class GrowthBookJsonUtils

java.lang.Object
growthbook.sdk.java.util.GrowthBookJsonUtils

public class GrowthBookJsonUtils extends Object
INTERNAL: This convenience class was created to help with the serialization and deserialization of custom types. Some types in the JSON source are tuples. This helps with transforming to and from POJOs. The provided methods use a custom Gson instance that has all required type adapters registered.
  • Field Details

    • gson

      public final com.google.gson.Gson gson
      The Gson instance is exposed for convenience.
  • Method Details

    • getInstance

      public static GrowthBookJsonUtils getInstance()
      The JSON utils singleton
      Returns:
      an instance of GrowthBookJsonUtils
    • unwrap

      public static Object unwrap(Object o)
      Unwrap an object. If it's not a JsonElement, you'll get the object right back
      Parameters:
      o - the JSON element to unwrap.
      Returns:
      unwrapped or original object
    • getElementType

      public static DataType getElementType(@Nullable com.google.gson.JsonElement element)
      A convenience method to help work with types of JSON elements
      Parameters:
      element - unknown JsonElement
      Returns:
      DataType
    • getJsonElementForObject

      @Nullable public static com.google.gson.JsonElement getJsonElementForObject(Object object)
      Given a provided object, which can be a primitive or a serializable class, Will return a JSON element. It will first attempt to detect it as a primitive type. If that doesn't work, it will try to serialize it. If serialization fails, null will be returned.
      Parameters:
      object - Unknown typed object
      Returns:
      the JSON element that gets created or null if serialization fails