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

co.easimart.EasimartEncoder Maven / Gradle / Ivy

package co.easimart;

import android.util.Base64;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * A {@code EasimartEncoder} can be used to transform objects such as {@link EasimartObjects} into JSON
 * data structures.
 *
 * @see co.easimart.EasimartDecoder
 */
/** package */ abstract class EasimartEncoder {

  /* package */ static boolean isValidType(Object value) {
    return value instanceof JSONObject || value instanceof JSONArray || value instanceof String
        || value instanceof Number || value instanceof Boolean || value == JSONObject.NULL
        || value instanceof EasimartObject || value instanceof EasimartACL || value instanceof EasimartFile
        || value instanceof EasimartGeoPoint || value instanceof Date || value instanceof byte[]
        || value instanceof List || value instanceof Map || value instanceof EasimartRelation;
  }

  public Object encode(Object object) {
    try {
      if (object instanceof EasimartObject) {
        return encodeRelatedObject((EasimartObject) object);
      }

      // TODO(grantland): Remove once we disallow mutable nested queries t6941155
      if (object instanceof EasimartQuery.State.Builder) {
        EasimartQuery.State.Builder builder = (EasimartQuery.State.Builder) object;
        return encode(builder.build());
      }

      if (object instanceof EasimartQuery.State) {
        EasimartQuery.State state = (EasimartQuery.State) object;
        return state.toJSON(this);
      }

      if (object instanceof Date) {
        return encodeDate((Date) object);
      }

      if (object instanceof byte[]) {
        JSONObject json = new JSONObject();
        json.put("__type", "Bytes");
        json.put("base64", Base64.encodeToString((byte[]) object, Base64.NO_WRAP));
        return json;
      }

      if (object instanceof EasimartFile) {
        return ((EasimartFile) object).encode();
      }

      if (object instanceof EasimartGeoPoint) {
        EasimartGeoPoint point = (EasimartGeoPoint) object;
        JSONObject json = new JSONObject();
        json.put("__type", "GeoPoint");
        json.put("latitude", point.getLatitude());
        json.put("longitude", point.getLongitude());
        return json;
      }

      if (object instanceof EasimartACL) {
        EasimartACL acl = (EasimartACL) object;
        return acl.toJSONObject(this);
      }

      if (object instanceof Map) {
        @SuppressWarnings("unchecked")
        Map map = (Map) object;
        JSONObject json = new JSONObject();
        for (Map.Entry pair : map.entrySet()) {
          json.put(pair.getKey(), encode(pair.getValue()));
        }
        return json;
      }

      if (object instanceof JSONObject) {
        JSONObject map = (JSONObject) object;
        JSONObject json = new JSONObject();
        Iterator keys = map.keys();
        while (keys.hasNext()) {
          String key = (String) keys.next();
          json.put(key, encode(map.opt(key)));
        }
        return json;
      }

      if (object instanceof Collection) {
        JSONArray array = new JSONArray();
        for (Object item : (Collection) object) {
          array.put(encode(item));
        }
        return array;
      }

      if (object instanceof JSONArray) {
        JSONArray array = (JSONArray) object;
        JSONArray json = new JSONArray();
        for (int i = 0; i < array.length(); ++i) {
          json.put(encode(array.opt(i)));
        }
        return json;
      }

      if (object instanceof EasimartRelation) {
        EasimartRelation relation = (EasimartRelation) object;
        return relation.encodeToJSON(this);
      }

      if (object instanceof EasimartFieldOperation) {
        return ((EasimartFieldOperation) object).encode(this);
      }

      if (object instanceof EasimartQuery.RelationConstraint) {
        return ((EasimartQuery.RelationConstraint) object).encode(this);
      }

      if (object == null) {
        return JSONObject.NULL;
      }

    } catch (JSONException e) {
      throw new RuntimeException(e);
    }

    // String, Number, Boolean,
    if (isValidType(object)) {
      return object;
    }

    throw new IllegalArgumentException("invalid type for EasimartObject: "
        + object.getClass().toString());
  }

  protected abstract JSONObject encodeRelatedObject(EasimartObject object);

  protected JSONObject encodeDate(Date date) {
    JSONObject object = new JSONObject();
    String iso = EasimartDateFormat.getInstance().format(date);
    try {
      object.put("__type", "Date");
      object.put("iso", iso);
    } catch (JSONException e) {
      // This should not happen
      throw new RuntimeException(e);
    }
    return object;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy