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

com.senseidb.search.client.res.SenseiHitJsonHandler Maven / Gradle / Ivy

The newest version!
package com.senseidb.search.client.res;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

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

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

public class SenseiHitJsonHandler implements JsonHandler {

  private static final Set PREDEFINED_FIELDS = new HashSet(Arrays.asList("uid",
    "docid", "score", "srcdata", "grouphitscount", "groupHits", "stored", "termvectors",
    "explanation"));

  @Override
  public JSONObject serialize(SenseiHit bean) throws JSONException {
    throw new UnsupportedOperationException();
  }

  @Override
  public SenseiHit deserialize(JSONObject json) throws JSONException {
    if (json == null) {
      return null;
    }

    SenseiHit senseiHit = JsonDeserializer.deserialize(SenseiHit.class, json, false);
    JSONArray storedFieldsArr = json.optJSONArray("_stored");
    if (storedFieldsArr != null) {
      List storedFields = new ArrayList(storedFieldsArr.length());
      for (int i = 0; i < storedFieldsArr.length(); i++) {
        JSONObject storedJson = storedFieldsArr.optJSONObject(i);
        if (storedJson != null) {
          storedFields
              .add(new FieldValue(storedJson.optString("name"), storedJson.optString("val")));
        }
      }
      senseiHit.setStoredFields(storedFields);
    }
    @SuppressWarnings("rawtypes")
    Iterator iterator = json.keys();
    while (iterator.hasNext()) {
      String field = (String) iterator.next();
      if (PREDEFINED_FIELDS.contains(field)) {
        continue;
      }
      JSONArray jsonArr = json.optJSONArray(field);
      if (jsonArr != null) {
        List values = new ArrayList(jsonArr.length());
        for (int i = 0; i < jsonArr.length(); i++) {
          values.add(jsonArr.getString(i));
        }
        senseiHit.getFieldValues().put(field, values);
      }
    }

    return senseiHit;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy