forked from json-path/JsonPath
/
JsonOrgJsonProvider.java
215 lines (188 loc) · 6.06 KB
/
JsonOrgJsonProvider.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.jayway.jsonpath.spi.json;
import org.json.JSONObject;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.JsonPathException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
public class JsonOrgJsonProvider extends AbstractJsonProvider {
@Override
public Object parse(String json) throws InvalidJsonException {
try {
return new JSONTokener(json).nextValue();
} catch (JSONException e) {
throw new InvalidJsonException(e);
}
}
@Override
public Object parse(InputStream jsonStream, String charset) throws InvalidJsonException {
try {
return new JSONTokener(new InputStreamReader(jsonStream, charset)).nextValue();
} catch (UnsupportedEncodingException e) {
throw new JsonPathException(e);
} catch (JSONException e) {
throw new InvalidJsonException(e);
}
}
@Override
public Object unwrap(Object obj) {
if(obj == JSONObject.NULL){
return null;
}
return obj;
}
@Override
public String toJson(Object obj) {
return obj.toString();
}
@Override
public Object createArray() {
return new JSONArray();
}
@Override
public Object createMap() {
return new JSONObject();
}
@Override
public boolean isArray(Object obj) {
return (obj instanceof JSONArray || obj instanceof List);
}
@Override
public Object getArrayIndex(Object obj, int idx) {
try {
return toJsonArray(obj).get(idx);
} catch (JSONException e) {
throw new JsonPathException(e);
}
}
@Override
public void setArrayIndex(Object array, int index, Object newValue) {
try {
if (!isArray(array)) {
throw new UnsupportedOperationException();
} else {
toJsonArray(array).put(index, createJsonElement(newValue));
}
} catch (JSONException e) {
throw new JsonPathException(e);
}
}
@Override
public Object getMapValue(Object obj, String key) {
try {
JSONObject jsonObject = toJsonObject(obj);
Object o = jsonObject.opt(key);
if (o == null) {
return UNDEFINED;
} else {
return unwrap(o);
}
} catch (JSONException e) {
throw new JsonPathException(e);
}
}
@Override
public void setProperty(Object obj, Object key, Object value) {
try {
if (isMap(obj))
toJsonObject(obj).put(key.toString(), createJsonElement(value));
else {
JSONArray array = toJsonArray(obj);
int index;
if (key != null) {
index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
} else {
index = array.length();
}
if (index == array.length()) {
array.put(createJsonElement(value));
} else {
array.put(index, createJsonElement(value));
}
}
} catch (JSONException e) {
throw new JsonPathException(e);
}
}
@SuppressWarnings("unchecked")
public void removeProperty(Object obj, Object key) {
if (isMap(obj))
toJsonObject(obj).remove(key.toString());
else {
JSONArray array = toJsonArray(obj);
int index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
array.remove(index);
}
}
@Override
public boolean isMap(Object obj) {
return (obj instanceof JSONObject);
}
@SuppressWarnings("unchecked")
@Override
public Collection<String> getPropertyKeys(Object obj) {
JSONObject jsonObject = toJsonObject(obj);
try {
if(Objects.isNull(jsonObject.names()))
return new ArrayList<>();
return jsonObject.keySet();
} catch (JSONException e) {
throw new JsonPathException(e);
}
}
@Override
public int length(Object obj) {
if (isArray(obj)) {
return toJsonArray(obj).length();
} else if (isMap(obj)) {
return toJsonObject(obj).length();
} else {
if (obj instanceof String) {
return ((String) obj).length();
}
}
throw new JsonPathException("length operation can not applied to " + (obj != null ? obj.getClass().getName()
: "null"));
}
@Override
public Iterable<?> toIterable(Object obj) {
try {
if (isArray(obj)) {
JSONArray arr = toJsonArray(obj);
List<Object> values = new ArrayList<Object>(arr.length());
for (int i = 0; i < arr.length(); i++) {
values.add(unwrap(arr.get(i)));
}
return values;
} else {
JSONObject jsonObject = toJsonObject(obj);
List<Object> values = new ArrayList<Object>();
for (int i = 0; i < jsonObject.names().length(); i++) {
String key = (String) jsonObject.names().get(i);
Object val = jsonObject.get(key);
values.add(unwrap(val));
}
return values;
}
} catch (JSONException e) {
throw new JsonPathException(e);
}
}
private Object createJsonElement(Object o) {
return o;
}
private JSONArray toJsonArray(Object o) {
return (JSONArray) o;
}
private JSONObject toJsonObject(Object o) {
return (JSONObject) o;
}
}