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

redis.clients.jedis.json.JsonBuilderFactory Maven / Gradle / Ivy

The newest version!
package redis.clients.jedis.json;

import static redis.clients.jedis.BuilderFactory.STRING;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import redis.clients.jedis.Builder;
import redis.clients.jedis.BuilderFactory;
import redis.clients.jedis.exceptions.JedisException;

public final class JsonBuilderFactory {

  public static final Builder> JSON_TYPE = new Builder>() {
    @Override
    public Class build(Object data) {
      if (data == null) return null;
      String str = STRING.build(data);
      switch (str) {
        case "null":
          return null;
        case "boolean":
          return boolean.class;
        case "integer":
          return int.class;
        case "number":
          return float.class;
        case "string":
          return String.class;
        case "object":
          return Object.class;
        case "array":
          return List.class;
        default:
          throw new JedisException("Unknown type: " + str);
      }
    }

    @Override
    public String toString() {
      return "Class";
    }
  };

  public static final Builder>> JSON_TYPE_LIST = new Builder>>() {
    @Override
    public List> build(Object data) {
      List list = (List) data;
      List> classes = new ArrayList<>(list.size());
      for (Object elem : list) {
        try {
          classes.add(JSON_TYPE.build(elem));
        } catch (JedisException je) {
          classes.add(null);
        }
      }
      return classes;
    }
  };

  public static final Builder>>> JSON_TYPE_RESPONSE_RESP3 = new Builder>>>() {
    @Override
    public List>> build(Object data) {
      return ((List) data).stream().map(JSON_TYPE_LIST::build).collect(Collectors.toList());
    }
  };

  public static final Builder>> JSON_TYPE_RESPONSE_RESP3_COMPATIBLE = new Builder>>() {
    @Override
    public List> build(Object data) {
      List>> fullReply = JSON_TYPE_RESPONSE_RESP3.build(data);
      return fullReply == null ? null : fullReply.get(0);
    }
  };

  public static final Builder JSON_OBJECT = new Builder() {
    @Override
    public Object build(Object data) {
      if (data == null) {
        return null;
      }

      if (!(data instanceof byte[])) {
        return data;
      }
      String str = STRING.build(data);
      if (str.charAt(0) == '{') {
        try {
          return new JSONObject(str);
        } catch (Exception ex) {
        }
      } else if (str.charAt(0) == '[') {
        try {
          return new JSONArray(str);
        } catch (Exception ex) {
        }
      }
      return str;
    }
  };

  public static final Builder JSON_ARRAY = new Builder() {
    @Override
    public JSONArray build(Object data) {
      if (data == null) {
        return null;
      }
      String str = STRING.build(data);
      try {
        return new JSONArray(str);
      } catch (JSONException ex) { // This is not necessary but we are doing this
        // just to make it safe for com.vaadin.external.google:android-json library
        throw new JedisException(ex);
      }
    }
  };

  public static final Builder JSON_ARRAY_OR_DOUBLE_LIST = new Builder() {
    @Override
    public Object build(Object data) {
      if (data == null) return null;
      if (data instanceof List) return BuilderFactory.DOUBLE_LIST.build(data);
      return JSON_ARRAY.build(data);
    }
  };

  public static final Builder> JSON_ARRAY_LIST = new Builder>() {
    @Override
    public List build(Object data) {
      if (data == null) {
        return null;
      }
      List list = (List) data;
      return list.stream().map(o -> JSON_ARRAY.build(o)).collect(Collectors.toList());
    }
  };

  private JsonBuilderFactory() {
    throw new InstantiationError("Must not instantiate this class");
  }
}