gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.util / org.gvsig.tools.util.impl / src / main / java / org / gvsig / json / JsonManagerImpl.java @ 2401
History | View | Annotate | Download (13.1 KB)
1 |
package org.gvsig.json; |
---|---|
2 |
|
3 |
import com.jayway.jsonpath.JsonPath; |
4 |
import java.io.ByteArrayInputStream; |
5 |
import java.io.StringWriter; |
6 |
import java.math.BigDecimal; |
7 |
import java.util.Arrays; |
8 |
import java.util.Collections; |
9 |
import java.util.HashMap; |
10 |
import java.util.Iterator; |
11 |
import java.util.Map; |
12 |
import java.util.Objects; |
13 |
import javax.json.JsonArray; |
14 |
import javax.json.JsonNumber; |
15 |
import javax.json.JsonObject; |
16 |
import javax.json.JsonReader; |
17 |
import javax.json.JsonString; |
18 |
import javax.json.JsonStructure; |
19 |
import javax.json.JsonValue; |
20 |
import javax.json.JsonWriter; |
21 |
import javax.json.JsonWriterFactory; |
22 |
import javax.json.stream.JsonGenerator; |
23 |
import static org.gvsig.json.JsonManager.ATTRIBUTE_NAME_CLASS; |
24 |
import org.gvsig.json.serializers.DataTypeSerializer; |
25 |
import org.gvsig.json.serializers.DefaultObjectSerializer; |
26 |
import org.gvsig.json.serializers.DynObjectSerializer; |
27 |
import org.gvsig.json.serializers.DynObjectValueItemSerializer; |
28 |
import org.gvsig.json.serializers.FileSerializer; |
29 |
import org.gvsig.json.serializers.LocaleSerializer; |
30 |
import org.gvsig.json.serializers.TagsSerializer; |
31 |
import org.gvsig.json.serializers.UriSerializer; |
32 |
import org.gvsig.json.serializers.UrlSerializer; |
33 |
import org.gvsig.tools.dynobject.DynClass; |
34 |
import org.gvsig.tools.dynobject.DynField; |
35 |
import org.gvsig.tools.dynobject.DynObject; |
36 |
import org.gvsig.tools.util.GetItemWithSizeAndIterator; |
37 |
import org.gvsig.tools.util.IsApplicable; |
38 |
|
39 |
/**
|
40 |
*
|
41 |
* @author gvSIG Team
|
42 |
*/
|
43 |
@SuppressWarnings("UseSpecificCatch") |
44 |
public class JsonManagerImpl implements JsonManager { |
45 |
|
46 |
private final Map<String,JsonSerializer> serializers1; |
47 |
private final Map<String,IsApplicable> serializers2; |
48 |
|
49 |
private final DynObjectSerializer dynObjectSerializer; |
50 |
|
51 |
public JsonManagerImpl() {
|
52 |
this.dynObjectSerializer = new DynObjectSerializer(); |
53 |
this.serializers1 = new HashMap<>(); |
54 |
this.serializers2 = new HashMap<>(); |
55 |
this.registerSerializer(new FileSerializer()); |
56 |
this.registerSerializer(new UrlSerializer()); |
57 |
this.registerSerializer(new UriSerializer()); |
58 |
this.registerSerializer(new LocaleSerializer()); |
59 |
this.registerSerializer(new TagsSerializer()); |
60 |
this.registerSerializer(new DynObjectValueItemSerializer()); |
61 |
this.registerSerializer(new DataTypeSerializer()); |
62 |
// this.registerSerializer(this.dynObjectSerializer);
|
63 |
} |
64 |
|
65 |
@Override
|
66 |
public void registerSerializer(Class<?extends SupportFromJson> theClass) { |
67 |
if (theClass == null) { |
68 |
return;
|
69 |
} |
70 |
if (!SupportFromJson.class.isAssignableFrom(theClass)) {
|
71 |
throw new IllegalArgumentException("the class '" + theClass.getName() + "'must implement SupportFromJson"); |
72 |
} |
73 |
this.serializers1.put(theClass.getName(), new DefaultObjectSerializer(theClass)); |
74 |
} |
75 |
|
76 |
@Override
|
77 |
public void registerSerializer(JsonSerializer serializer) { |
78 |
this.serializers1.put(serializer.getObjectClass().getName(), serializer);
|
79 |
if( serializer instanceof IsApplicable ) { |
80 |
this.serializers2.put(serializer.getObjectClass().getName(), (IsApplicable) serializer);
|
81 |
} |
82 |
} |
83 |
|
84 |
@Override
|
85 |
public JsonSerializer getSerializer(Object value) { |
86 |
JsonSerializer serializer1 = this.serializers1.get(value.getClass().getName());
|
87 |
if( serializer1!=null ) { |
88 |
return serializer1;
|
89 |
} |
90 |
for (IsApplicable serializer2 : this.serializers2.values()) { |
91 |
if( serializer2.isApplicable(value) ) {
|
92 |
return (JsonSerializer) serializer2;
|
93 |
} |
94 |
} |
95 |
if( value instanceof DynObject ) { |
96 |
return this.dynObjectSerializer; |
97 |
} |
98 |
return null; |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public JsonSerializer getSerializer(JsonObject json) {
|
103 |
String className = json.getString(ATTRIBUTE_NAME_CLASS, null); |
104 |
JsonSerializer serializer1 = this.serializers1.get(className);
|
105 |
if( serializer1!=null ) { |
106 |
return serializer1;
|
107 |
} |
108 |
for (IsApplicable serializer2 : this.serializers2.values()) { |
109 |
if( serializer2.isApplicable(json) ) {
|
110 |
return (JsonSerializer) serializer2;
|
111 |
} |
112 |
} |
113 |
if( this.dynObjectSerializer.isApplicable(json) ) { |
114 |
return this.dynObjectSerializer; |
115 |
} |
116 |
return null; |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public JsonObjectBuilder createObjectBuilder() {
|
121 |
return new JsonObjectBuilderImpl(); |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public JsonArrayBuilder createArrayBuilder() {
|
126 |
return new JsonArrayBuilderImpl(); |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public JsonObject createObject(String json) { |
131 |
try {
|
132 |
ByteArrayInputStream data = new ByteArrayInputStream(json.getBytes()); |
133 |
JsonReader reader = javax.json.Json.createReader(data); |
134 |
JsonObject x = reader.readObject(); |
135 |
return new org.gvsig.json.JsonObjectImpl(x); |
136 |
} catch (Exception ex) { |
137 |
return null; |
138 |
} |
139 |
} |
140 |
|
141 |
@Override
|
142 |
public JsonArray createArray(String json) { |
143 |
try {
|
144 |
ByteArrayInputStream data = new ByteArrayInputStream(json.getBytes()); |
145 |
JsonReader reader = javax.json.Json.createReader(data); |
146 |
JsonArray x = reader.readArray(); |
147 |
return new org.gvsig.json.JsonArrayImpl(x); |
148 |
} catch (Exception ex) { |
149 |
return null; |
150 |
} |
151 |
} |
152 |
|
153 |
private String toString(JsonStructure obj) { |
154 |
StringWriter sw = new StringWriter(); |
155 |
JsonWriterFactory writerFactory = javax.json.Json.createWriterFactory( |
156 |
Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true) |
157 |
); |
158 |
try (JsonWriter jsonWriter = writerFactory.createWriter(sw)) {
|
159 |
jsonWriter.write(obj); |
160 |
} |
161 |
String s = sw.toString();
|
162 |
return s;
|
163 |
} |
164 |
|
165 |
@Override
|
166 |
public String toString(JsonValue value) { |
167 |
if( value == null ) { |
168 |
return Objects.toString(null); |
169 |
} |
170 |
switch (value.getValueType()) {
|
171 |
case ARRAY:
|
172 |
case OBJECT:
|
173 |
return this.toString((JsonStructure)value); |
174 |
case FALSE:
|
175 |
case NUMBER:
|
176 |
case STRING:
|
177 |
case TRUE:
|
178 |
case NULL:
|
179 |
default:
|
180 |
return value.toString();
|
181 |
} |
182 |
} |
183 |
|
184 |
|
185 |
@Override
|
186 |
public Object toObject(JsonObject json, String name) { |
187 |
JsonValue value = json.get(name); |
188 |
return toObject(value);
|
189 |
} |
190 |
|
191 |
@Override
|
192 |
public Object toObject(JsonArray json, int index) { |
193 |
JsonValue value = json.get(index); |
194 |
return toObject(value);
|
195 |
} |
196 |
|
197 |
private Object createObject(JsonObject json, Object defaultValue) { |
198 |
if (json == null) { |
199 |
return defaultValue;
|
200 |
} |
201 |
JsonSerializer serializer = this.getSerializer(json);
|
202 |
if (serializer == null) { |
203 |
return defaultValue;
|
204 |
} |
205 |
Object obj = serializer.toObject(json);
|
206 |
if (obj == null) { |
207 |
return defaultValue;
|
208 |
} |
209 |
return obj;
|
210 |
} |
211 |
|
212 |
@Override
|
213 |
public Iterable<Object> toIterable(final JsonArray jsonArray) { |
214 |
@SuppressWarnings("Convert2Lambda") |
215 |
Iterable<Object> iterable = new Iterable<Object>() { |
216 |
@Override
|
217 |
public Iterator<Object> iterator() { |
218 |
Iterator<Object> it = new Iterator<Object>() { |
219 |
int current= 0; |
220 |
@Override
|
221 |
public boolean hasNext() { |
222 |
return current<jsonArray.size();
|
223 |
} |
224 |
|
225 |
@Override
|
226 |
public Object next() { |
227 |
return toObject(jsonArray.get(current++));
|
228 |
} |
229 |
}; |
230 |
return it;
|
231 |
} |
232 |
}; |
233 |
return iterable;
|
234 |
} |
235 |
|
236 |
@Override
|
237 |
public GetItemWithSizeAndIterator<Object> toItems(final JsonArray jsonArray) { |
238 |
GetItemWithSizeAndIterator<Object> items = new GetItemWithSizeAndIterator<Object>() { |
239 |
|
240 |
@Override
|
241 |
public int size() { |
242 |
return jsonArray.size();
|
243 |
} |
244 |
|
245 |
@Override
|
246 |
public Object get(int position) { |
247 |
return toObject(jsonArray.get(position));
|
248 |
} |
249 |
|
250 |
@Override
|
251 |
public Iterator<Object> iterator() { |
252 |
Iterator<Object> it = new Iterator<Object>() { |
253 |
int current= 0; |
254 |
@Override
|
255 |
public boolean hasNext() { |
256 |
return current<jsonArray.size();
|
257 |
} |
258 |
|
259 |
@Override
|
260 |
public Object next() { |
261 |
return toObject(jsonArray.get(current++));
|
262 |
} |
263 |
}; |
264 |
return it;
|
265 |
}; |
266 |
}; |
267 |
return items;
|
268 |
} |
269 |
|
270 |
@Override
|
271 |
public Object[] toArray(JsonArray jsonArray, Object[] a) { |
272 |
if( jsonArray==null ) { |
273 |
return null; |
274 |
} |
275 |
int sz = jsonArray.size();
|
276 |
if( a==null ) { |
277 |
a = new Object[sz]; |
278 |
} else if (a.length != sz) { |
279 |
a = Arrays.copyOf(a, sz);
|
280 |
} |
281 |
for (int i = 0; i < sz; i++) { |
282 |
JsonValue value = jsonArray.get(i); |
283 |
a[i] = this.toObject(value);
|
284 |
} |
285 |
return a;
|
286 |
} |
287 |
|
288 |
@Override
|
289 |
public Object toObject(JsonValue value) { |
290 |
return this.toObjectOrDefault(value, null); |
291 |
} |
292 |
|
293 |
@Override
|
294 |
public Object toObjectOrDefault(JsonValue value, Object defaultValue) { |
295 |
if( value == null ) { |
296 |
return defaultValue;
|
297 |
} |
298 |
switch (value.getValueType()) {
|
299 |
case ARRAY:
|
300 |
return this.toArray((JsonArray) value, new Object[0]); |
301 |
case FALSE:
|
302 |
return false; |
303 |
case NUMBER:
|
304 |
JsonNumber n = (JsonNumber) value; |
305 |
if (n.isIntegral()) {
|
306 |
int nint = n.intValue();
|
307 |
long nlong = n.longValue();
|
308 |
if (nint == nlong) {
|
309 |
return nint;
|
310 |
} |
311 |
if (n.bigDecimalValue().compareTo(new BigDecimal(nlong)) == 0) { |
312 |
return nlong;
|
313 |
} |
314 |
return n.bigIntegerValue();
|
315 |
} else {
|
316 |
double ndouble = n.doubleValue();
|
317 |
if (n.bigDecimalValue().compareTo(new BigDecimal(ndouble)) == 0) { |
318 |
return ndouble;
|
319 |
} |
320 |
return n.bigDecimalValue();
|
321 |
} |
322 |
|
323 |
case OBJECT:
|
324 |
return this.createObject((JsonObject) value, defaultValue); |
325 |
|
326 |
case STRING:
|
327 |
return ((JsonString) value).getString();
|
328 |
case TRUE:
|
329 |
return true; |
330 |
case NULL:
|
331 |
return null; |
332 |
default:
|
333 |
return defaultValue;
|
334 |
} |
335 |
} |
336 |
|
337 |
@Override
|
338 |
public DynObject addAll(DynObject target, JsonObject json) {
|
339 |
if( json == null || target==null ) { |
340 |
return target;
|
341 |
} |
342 |
DynClass dynClass = target.getDynClass(); |
343 |
String name = null; |
344 |
try {
|
345 |
for (DynField dynField : dynClass.getDynFields()) {
|
346 |
name = dynField.getName(); |
347 |
if( json.containsKey(name) ) {
|
348 |
target.setDynValue(name, this.toObject(json.get(name)));
|
349 |
} |
350 |
} |
351 |
} catch (Exception ex) { |
352 |
throw new RuntimeException("Can't copy values from json to DynObject (class "+dynClass.getFullName()+", field "+name+").", ex); |
353 |
} |
354 |
return target;
|
355 |
} |
356 |
|
357 |
@Override
|
358 |
public Map toMap(JsonObject json) { |
359 |
if( json == null ) { |
360 |
return null; |
361 |
} |
362 |
Map map = new HashMap(); |
363 |
for (String name : json.keySet()) { |
364 |
map.put(name, this.toObject(json.get(name)));
|
365 |
} |
366 |
return map;
|
367 |
} |
368 |
|
369 |
public static JsonValue wrap(JsonValue value) { |
370 |
if( value == null ) { |
371 |
return null; |
372 |
} else if( value == JsonValue.NULL ) { |
373 |
return null; |
374 |
} else if( value instanceof JsonObject && |
375 |
!(value instanceof org.gvsig.json.JsonObjectImpl)) {
|
376 |
value = new org.gvsig.json.JsonObjectImpl((JsonObject) value);
|
377 |
} else if( value instanceof JsonArray && |
378 |
!(value instanceof org.gvsig.json.JsonArrayImpl)) {
|
379 |
value = new org.gvsig.json.JsonArrayImpl((JsonArray) value);
|
380 |
} |
381 |
return value;
|
382 |
} |
383 |
|
384 |
@Override
|
385 |
public JsonObjectBuilder createObjectBuilder(String json) { |
386 |
JsonObject obj = this.createObject(json);
|
387 |
return this.createObjectBuilder(obj); |
388 |
} |
389 |
|
390 |
@Override
|
391 |
public JsonObjectBuilder createObjectBuilder(JsonObject obj) {
|
392 |
JsonObjectBuilder builder = this.createObjectBuilder();
|
393 |
for (Map.Entry<String, JsonValue> entry : obj.entrySet()) { |
394 |
builder.add(entry.getKey(), entry.getValue()); |
395 |
} |
396 |
return builder;
|
397 |
} |
398 |
|
399 |
@Override
|
400 |
public JsonPathContext createJSonPathContext(JsonStructure jsonObject) {
|
401 |
return new JsonPathContextImpl(this, jsonObject); |
402 |
} |
403 |
|
404 |
@Override
|
405 |
public JsonPathContext createJSonPathContext(String jsonObject) { |
406 |
return new JsonPathContextImpl(this, jsonObject); |
407 |
} |
408 |
} |