This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 173
/
SenseiHitJsonHandler.java
98 lines (87 loc) · 3.7 KB
/
SenseiHitJsonHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* This software is licensed to you under the Apache License, Version 2.0 (the
* "Apache License").
*
* LinkedIn's contributions are made under the Apache License. If you contribute
* to the Software, the contributions will be deemed to have been made under the
* Apache License, unless you expressly indicate otherwise. Please do not make any
* contributions that would be inconsistent with the Apache License.
*
* You may obtain a copy of the Apache License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, this software
* distributed under the Apache License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Apache
* License for the specific language governing permissions and limitations for the
* software governed under the Apache License.
*
* © 2012 LinkedIn Corp. All Rights Reserved.
*/
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<SenseiHit> {
private static final Set<String> PREDEFINED_FIELDS= new HashSet<String>(Arrays.asList("uid", "docid", "score", "srcdata", "grouphitscount", "groupHits", "stored", "termvectors", "explanation"));
@Override
public JSONObject serialize(SenseiHit bean) throws JSONException {
throw new UnsupportedOperationException();
// return (JSONObject) JsonSerializer.serialize(bean);
}
@Override
public SenseiHit deserialize(JSONObject json) throws JSONException {
if (json == null) {
return null;
}
/*SenseiHit senseiHit = new SenseiHit();
senseiHit.setDocid(json.optInt("docid"));
senseiHit.setGrouphitscount(json.optInt("grouphitscount"));
senseiHit.setSrcdata(json.optString("srcdata"));
senseiHit.setUid(json.optInt("uid"));
senseiHit.setScore(json.optInt("score"));
JSONArray groupHitsArr = json.optJSONArray("groupHits");
if (groupHitsArr != null){
List<SenseiHit> groupHits = new ArrayList<SenseiHit>(groupHitsArr.length());
for (int i = 0; i< groupHitsArr.length(); i++) {
groupHits.add(deserialize(groupHitsArr.optJSONObject(i)));
}
senseiHit.setGroupHits(groupHits);
}*/
SenseiHit senseiHit = JsonDeserializer.deserialize(SenseiHit.class, json, false);
JSONArray storedFieldsArr = json.optJSONArray("stored");
if (storedFieldsArr != null) {
List<FieldValue> storedFields = new ArrayList<FieldValue>(storedFieldsArr.length());
for (int i = 0; i< storedFieldsArr.length(); i++) {
JSONObject storedJson = storedFieldsArr.optJSONObject(i);
if (storedJson != null) {
String fieldName = (String) storedJson.keys().next();
storedFields.add(new FieldValue(fieldName, storedJson.optString(fieldName)));
}
}
senseiHit.setStoredFields(storedFields);
}
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<String> values = new ArrayList<String>(jsonArr.length());
for (int i = 0; i< jsonArr.length(); i++) {
values.add(jsonArr.getString(i));
}
senseiHit.getFieldValues().put(field, values);
}
}
return senseiHit;
}
}