All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.launchdarkly.client.utils.FeatureStoreHelpers Maven / Gradle / Ivy

There is a newer version: 4.61
Show newest version
package com.launchdarkly.client.utils;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.launchdarkly.client.FeatureStore;
import com.launchdarkly.client.VersionedData;
import com.launchdarkly.client.VersionedDataKind;

/**
 * Helper methods that may be useful for implementing a {@link FeatureStore} or {@link FeatureStoreCore}.
 *
 * @since 4.6.0
 */
public abstract class FeatureStoreHelpers {
  private static final Gson gson = new Gson();
  
  /**
   * Unmarshals a feature store item from a JSON string. This is a very simple wrapper around a Gson
   * method, just to allow external feature store implementations to make use of the Gson instance
   * that's inside the SDK rather than having to import Gson themselves.
   * 
   * @param  class of the object that will be returned
   * @param kind specifies the type of item being decoded
   * @param data the JSON string
   * @return the unmarshaled item
   * @throws UnmarshalException if the JSON string was invalid
   */
  public static  T unmarshalJson(VersionedDataKind kind, String data) {
    try {
      return gson.fromJson(data, kind.getItemClass());
    } catch (JsonParseException e) {
      throw new UnmarshalException(e);
    }
  }
  
  /**
   * Marshals a feature store item into a JSON string. This is a very simple wrapper around a Gson
   * method, just to allow external feature store implementations to make use of the Gson instance
   * that's inside the SDK rather than having to import Gson themselves.
   * @param item the item to be marshaled
   * @return the JSON string
   */
  public static String marshalJson(VersionedData item) {
    return gson.toJson(item);
  }
  
  @SuppressWarnings("serial")
  public static class UnmarshalException extends RuntimeException {
    public UnmarshalException(Throwable cause) {
      super(cause);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy