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

com.senseidb.search.client.json.req.SelectionJsonHandler Maven / Gradle / Ivy

There is a newer version: 2.0.1
Show newest version
package com.senseidb.search.client.json.req;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

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

import com.senseidb.search.client.json.JsonDeserializer;
import com.senseidb.search.client.json.JsonHandler;
import com.senseidb.search.client.json.JsonSerializer;

public class SelectionJsonHandler implements JsonHandler {
    public static Map> selectionClasses = new HashMap>();
    static {
        for (Class cls : Arrays.asList(Term.class, Terms.class, Selection.Path.class, Selection.Range.class, Selection.Custom.class)) {
            selectionClasses.put(cls.getSimpleName().toLowerCase(), cls);
        }
    }
    @Override
    public JSONObject serialize(Selection bean)  throws JSONException {
        if (bean == null) {
            return null;
        }
        if (bean instanceof Selection.Custom) {
            JSONObject ret = new JSONObject();
            ret.put("custom", ((Selection.Custom)bean).getCustom());
            return ret;
        }
        
        JSONObject innerObject = (JSONObject) JsonSerializer.serialize(bean, false);
        JSONObject paramContainer = new JSONObject();
        paramContainer.put(bean.getField(), innerObject);
        JSONObject ret = new JSONObject();
        ret.put(bean.getClass().getSimpleName().toLowerCase(), paramContainer);
        return ret;
    }

    @Override
    public Selection deserialize(JSONObject json)  throws JSONException{
        String[] names = JSONObject.getNames(json);
        if (names.length == 0 || !selectionClasses.keySet().contains(names[0])) {
            throw new IllegalStateException("The json object doesn't contain the value from " + Arrays.toString(names));
        }
        String name = names[0];
        JSONObject innerPart = json.getJSONObject(name);
        if ("custom".equals(name)) {
            return Selection.custom(innerPart);
        }
        String fieldName = JSONObject.getNames(innerPart)[0];
        Selection selection = JsonDeserializer.deserialize(selectionClasses.get(name), innerPart.getJSONObject(fieldName), false);
        selection.setField(fieldName);
        return selection;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy