-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path184.java
More file actions
1068 lines (945 loc) · 38.7 KB
/
Copy path184.java
File metadata and controls
1068 lines (945 loc) · 38.7 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.esotericsoftware.jsonbeans;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import com.esotericsoftware.jsonbeans.JsonValue.PrettyPrintSettings;
import com.esotericsoftware.jsonbeans.ObjectMap.Entry;
import com.esotericsoftware.jsonbeans.OrderedMap.OrderedMapValues;
/** Reads/writes Java objects to/from JSON, automatically. See the wiki for usage:
* https://github.com/libgdx/libgdx/wiki/Reading-%26-writing-JSON
* @author Nathan Sweet */
public class Json {
static private final boolean debug = false;
private JsonWriter writer;
private String typeName = "class";
private boolean usePrototypes = true;
private OutputType outputType;
private boolean quoteLongValues;
private boolean ignoreUnknownFields;
private boolean enumNames = true;
private JsonSerializer defaultSerializer;
private final ObjectMap<Class, OrderedMap<String, FieldMetadata>> typeToFields = new ObjectMap();
private final ObjectMap<String, Class> tagToClass = new ObjectMap();
private final ObjectMap<Class, String> classToTag = new ObjectMap();
private final ObjectMap<Class, JsonSerializer> classToSerializer = new ObjectMap();
private final ObjectMap<Class, Object[]> classToDefaultValues = new ObjectMap();
private final Object[] equals1 = {null}, equals2 = {null};
public Json () {
outputType = OutputType.minimal;
}
public Json (OutputType outputType) {
this.outputType = outputType;
}
/** When true, fields in the JSON that are not found on the class will not throw a {@link JsonException}. Default is false. */
public void setIgnoreUnknownFields (boolean ignoreUnknownFields) {
this.ignoreUnknownFields = ignoreUnknownFields;
}
/** @see JsonWriter#setOutputType(OutputType) */
public void setOutputType (OutputType outputType) {
this.outputType = outputType;
}
/** @see JsonWriter#setQuoteLongValues(boolean) */
public void setQuoteLongValues (boolean quoteLongValues) {
this.quoteLongValues = quoteLongValues;
}
/** When true, {@link Enum#name()} is used to write enum values. When false, {@link Enum#toString()} is used which may not be
* unique. Default is true. */
public void setEnumNames (boolean enumNames) {
this.enumNames = enumNames;
}
/** Sets a tag to use instead of the fully qualifier class name. This can make the JSON easier to read. */
public void addClassTag (String tag, Class type) {
tagToClass.put(tag, type);
classToTag.put(type, tag);
}
/** Returns the class for the specified tag, or null. */
public Class getClass (String tag) {
return tagToClass.get(tag);
}
/** Returns the tag for the specified class, or null. */
public String getTag (Class type) {
return classToTag.get(type);
}
/** Sets the name of the JSON field to store the Java class name or class tag when required to avoid ambiguity during
* deserialization. Set to null to never output this information, but be warned that deserialization may fail. Default is
* "class". */
public void setTypeName (String typeName) {
this.typeName = typeName;
}
/** Sets the serializer to use when the type being deserialized is not known (null).
* @param defaultSerializer May be null. */
public void setDefaultSerializer (JsonSerializer defaultSerializer) {
this.defaultSerializer = defaultSerializer;
}
/** Registers a serializer to use for the specified type instead of the default behavior of serializing all of an objects
* fields. */
public <T> void setSerializer (Class<T> type, JsonSerializer<T> serializer) {
classToSerializer.put(type, serializer);
}
public <T> JsonSerializer<T> getSerializer (Class<T> type) {
return classToSerializer.get(type);
}
/** When true, field values that are identical to a newly constructed instance are not written. Default is true. */
public void setUsePrototypes (boolean usePrototypes) {
this.usePrototypes = usePrototypes;
}
/** Sets the type of elements in a collection. When the element type is known, the class for each element in the collection
* does not need to be written unless different from the element type. */
public void setElementType (Class type, String fieldName, Class elementType) {
ObjectMap<String, FieldMetadata> fields = getFields(type);
FieldMetadata metadata = fields.get(fieldName);
if (metadata == null) throw new JsonException("Field not found: " + fieldName + " (" + type.getName() + ")");
metadata.elementType = elementType;
}
private OrderedMap<String, FieldMetadata> getFields (Class type) {
OrderedMap<String, FieldMetadata> fields = typeToFields.get(type);
if (fields != null) return fields;
ArrayList<Field> allFields = new ArrayList();
Class nextClass = type;
while (nextClass != Object.class) {
Collections.addAll(allFields, nextClass.getDeclaredFields());
nextClass = nextClass.getSuperclass();
}
OrderedMap<String, FieldMetadata> nameToField = new OrderedMap();
for (int i = 0, n = allFields.size(); i < n; i++) {
Field field = allFields.get(i);
int modifiers = field.getModifiers();
if (Modifier.isTransient(modifiers)) continue;
if (Modifier.isStatic(modifiers)) continue;
if (field.isSynthetic()) continue;
if (!field.isAccessible()) {
try {
field.setAccessible(true);
} catch (AccessControlException ex) {
continue;
}
}
nameToField.put(field.getName(), new FieldMetadata(field));
}
typeToFields.put(type, nameToField);
return nameToField;
}
public String toJson (Object object) {
return toJson(object, object == null ? null : object.getClass(), (Class)null);
}
public String toJson (Object object, Class knownType) {
return toJson(object, knownType, (Class)null);
}
/** @param knownType May be null if the type is unknown.
* @param elementType May be null if the type is unknown. */
public String toJson (Object object, Class knownType, Class elementType) {
StringWriter buffer = new StringWriter();
toJson(object, knownType, elementType, buffer);
return buffer.toString();
}
public void toJson (Object object, File file) {
toJson(object, object == null ? null : object.getClass(), null, file);
}
/** @param knownType May be null if the type is unknown. */
public void toJson (Object object, Class knownType, File file) {
toJson(object, knownType, null, file);
}
/** @param knownType May be null if the type is unknown.
* @param elementType May be null if the type is unknown. */
public void toJson (Object object, Class knownType, Class elementType, File file) {
Writer writer = null;
try {
writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
toJson(object, knownType, elementType, writer);
} catch (Exception ex) {
throw new JsonException("Error writing file: " + file, ex);
} finally {
try {
if (writer != null) writer.close();
} catch (IOException ignored) {
}
}
}
public void toJson (Object object, Writer writer) {
toJson(object, object == null ? null : object.getClass(), null, writer);
}
/** @param knownType May be null if the type is unknown. */
public void toJson (Object object, Class knownType, Writer writer) {
toJson(object, knownType, null, writer);
}
/** @param knownType May be null if the type is unknown.
* @param elementType May be null if the type is unknown. */
public void toJson (Object object, Class knownType, Class elementType, Writer writer) {
setWriter(writer);
try {
writeValue(object, knownType, elementType);
} finally {
if (this.writer != null) {
try {
this.writer.close();
} catch (IOException ignored) {
}
}
this.writer = null;
}
}
/** Sets the writer where JSON output will be written. This is only necessary when not using the toJson methods. */
public void setWriter (Writer writer) {
if (!(writer instanceof JsonWriter)) writer = new JsonWriter(writer);
this.writer = (JsonWriter)writer;
this.writer.setOutputType(outputType);
this.writer.setQuoteLongValues(quoteLongValues);
}
public JsonWriter getWriter () {
return writer;
}
/** Writes all fields of the specified object to the current JSON object. */
public void writeFields (Object object) {
Class type = object.getClass();
Object[] defaultValues = getDefaultValues(type);
OrderedMap<String, FieldMetadata> fields = getFields(type);
int i = 0;
for (FieldMetadata metadata : new OrderedMapValues<FieldMetadata>(fields)) {
Field field = metadata.field;
try {
Object value = field.get(object);
if (defaultValues != null) {
Object defaultValue = defaultValues[i++];
if (value == null && defaultValue == null) continue;
if (value != null && defaultValue != null) {
if (value.equals(defaultValue)) continue;
if (value.getClass().isArray() && defaultValue.getClass().isArray()) {
equals1[0] = value;
equals2[0] = defaultValue;
if (Arrays.deepEquals(equals1, equals2)) continue;
}
}
}
if (debug) System.out.println("Writing field: " + field.getName() + " (" + type.getName() + ")");
writer.name(field.getName());
writeValue(value, field.getType(), metadata.elementType);
} catch (IllegalAccessException ex) {
throw new JsonException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
} catch (JsonException ex) {
ex.addTrace(field + " (" + type.getName() + ")");
throw ex;
} catch (Exception runtimeEx) {
JsonException ex = new JsonException(runtimeEx);
ex.addTrace(field + " (" + type.getName() + ")");
throw ex;
}
}
}
private Object[] getDefaultValues (Class type) {
if (!usePrototypes) return null;
if (classToDefaultValues.containsKey(type)) return classToDefaultValues.get(type);
Object object;
try {
object = newInstance(type);
} catch (Exception ex) {
classToDefaultValues.put(type, null);
return null;
}
ObjectMap<String, FieldMetadata> fields = getFields(type);
Object[] values = new Object[fields.size];
classToDefaultValues.put(type, values);
int i = 0;
for (FieldMetadata metadata : fields.values()) {
Field field = metadata.field;
try {
values[i++] = field.get(object);
} catch (IllegalAccessException ex) {
throw new JsonException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
} catch (JsonException ex) {
ex.addTrace(field + " (" + type.getName() + ")");
throw ex;
} catch (RuntimeException runtimeEx) {
JsonException ex = new JsonException(runtimeEx);
ex.addTrace(field + " (" + type.getName() + ")");
throw ex;
}
}
return values;
}
/** @see #writeField(Object, String, String, Class) */
public void writeField (Object object, String name) {
writeField(object, name, name, null);
}
/** @param elementType May be null if the type is unknown.
* @see #writeField(Object, String, String, Class) */
public void writeField (Object object, String name, Class elementType) {
writeField(object, name, name, elementType);
}
/** @see #writeField(Object, String, String, Class) */
public void writeField (Object object, String fieldName, String jsonName) {
writeField(object, fieldName, jsonName, null);
}
/** Writes the specified field to the current JSON object.
* @param elementType May be null if the type is unknown. */
public void writeField (Object object, String fieldName, String jsonName, Class elementType) {
Class type = object.getClass();
ObjectMap<String, FieldMetadata> fields = getFields(type);
FieldMetadata metadata = fields.get(fieldName);
if (metadata == null) throw new JsonException("Field not found: " + fieldName + " (" + type.getName() + ")");
Field field = metadata.field;
if (elementType == null) elementType = metadata.elementType;
try {
if (debug) System.out.println("Writing field: " + field.getName() + " (" + type.getName() + ")");
writer.name(jsonName);
writeValue(field.get(object), field.getType(), elementType);
} catch (IllegalAccessException ex) {
throw new JsonException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
} catch (JsonException ex) {
ex.addTrace(field + " (" + type.getName() + ")");
throw ex;
} catch (Exception runtimeEx) {
JsonException ex = new JsonException(runtimeEx);
ex.addTrace(field + " (" + type.getName() + ")");
throw ex;
}
}
/** Writes the value as a field on the current JSON object, without writing the actual class.
* @param value May be null.
* @see #writeValue(String, Object, Class, Class) */
public void writeValue (String name, Object value) {
try {
writer.name(name);
} catch (IOException ex) {
throw new JsonException(ex);
}
if (value == null)
writeValue(value, null, null);
else
writeValue(value, value.getClass(), null);
}
/** Writes the value as a field on the current JSON object, writing the class of the object if it differs from the specified
* known type.
* @param value May be null.
* @param knownType May be null if the type is unknown.
* @see #writeValue(String, Object, Class, Class) */
public void writeValue (String name, Object value, Class knownType) {
try {
writer.name(name);
} catch (IOException ex) {
throw new JsonException(ex);
}
writeValue(value, knownType, null);
}
/** Writes the value as a field on the current JSON object, writing the class of the object if it differs from the specified
* known type. The specified element type is used as the default type for collections.
* @param value May be null.
* @param knownType May be null if the type is unknown.
* @param elementType May be null if the type is unknown. */
public void writeValue (String name, Object value, Class knownType, Class elementType) {
try {
writer.name(name);
} catch (IOException ex) {
throw new JsonException(ex);
}
writeValue(value, knownType, elementType);
}
/** Writes the value, without writing the class of the object.
* @param value May be null. */
public void writeValue (Object value) {
if (value == null)
writeValue(value, null, null);
else
writeValue(value, value.getClass(), null);
}
/** Writes the value, writing the class of the object if it differs from the specified known type.
* @param value May be null.
* @param knownType May be null if the type is unknown. */
public void writeValue (Object value, Class knownType) {
writeValue(value, knownType, null);
}
/** Writes the value, writing the class of the object if it differs from the specified known type. The specified element type
* is used as the default type for collections.
* @param value May be null.
* @param knownType May be null if the type is unknown.
* @param elementType May be null if the type is unknown. */
public void writeValue (Object value, Class knownType, Class elementType) {
try {
if (value == null) {
writer.value(null);
return;
}
if ((knownType != null && knownType.isPrimitive()) || knownType == String.class || knownType == Integer.class
|| knownType == Boolean.class || knownType == Float.class || knownType == Long.class || knownType == Double.class
|| knownType == Short.class || knownType == Byte.class || knownType == Character.class) {
writer.value(value);
return;
}
Class actualType = value.getClass();
if (actualType.isPrimitive() || actualType == String.class || actualType == Integer.class || actualType == Boolean.class
|| actualType == Float.class || actualType == Long.class || actualType == Double.class || actualType == Short.class
|| actualType == Byte.class || actualType == Character.class) {
writeObjectStart(actualType, null);
writeValue("value", value);
writeObjectEnd();
return;
}
if (value instanceof JsonSerializable) {
writeObjectStart(actualType, knownType);
((JsonSerializable)value).write(this);
writeObjectEnd();
return;
}
JsonSerializer serializer = classToSerializer.get(actualType);
if (serializer != null) {
serializer.write(this, value, knownType);
return;
}
// JSON array special cases.
if (value instanceof ArrayList) {
if (knownType != null && actualType != knownType && actualType != ArrayList.class)
throw new JsonException("Serialization of an Array other than the known type is not supported.\n" + "Known type: "
+ knownType + "\nActual type: " + actualType);
writeArrayStart();
ArrayList array = (ArrayList)value;
for (int i = 0, n = array.size(); i < n; i++)
writeValue(array.get(i), elementType, null);
writeArrayEnd();
return;
}
if (value instanceof Collection) {
if (typeName != null && actualType != ArrayList.class && (knownType == null || knownType != actualType)) {
writeObjectStart(actualType, knownType);
writeArrayStart("items");
for (Object item : (Collection)value)
writeValue(item, elementType, null);
writeArrayEnd();
writeObjectEnd();
} else {
writeArrayStart();
for (Object item : (Collection)value)
writeValue(item, elementType, null);
writeArrayEnd();
}
return;
}
if (actualType.isArray()) {
if (elementType == null) elementType = actualType.getComponentType();
int length = Array.getLength(value);
writeArrayStart();
for (int i = 0; i < length; i++)
writeValue(Array.get(value, i), elementType, null);
writeArrayEnd();
return;
}
// JSON object special cases.
if (value instanceof ObjectMap) {
if (knownType == null) knownType = ObjectMap.class;
writeObjectStart(actualType, knownType);
for (Entry entry : ((ObjectMap<?, ?>)value).entries()) {
writer.name(convertToString(entry.key));
writeValue(entry.value, elementType, null);
}
writeObjectEnd();
return;
}
if (value instanceof Map) {
if (knownType == null) knownType = HashMap.class;
writeObjectStart(actualType, knownType);
for (Map.Entry entry : ((Map<?, ?>)value).entrySet()) {
writer.name(convertToString(entry.getKey()));
writeValue(entry.getValue(), elementType, null);
}
writeObjectEnd();
return;
}
// Enum special case.
if (Enum.class.isAssignableFrom(actualType)) {
if (typeName != null && (knownType == null || knownType != actualType)) {
// Ensures that enums with specific implementations (abstract logic) serialize correctly.
if (actualType.getEnumConstants() == null) actualType = actualType.getSuperclass();
writeObjectStart(actualType, null);
writer.name("value");
writer.value(convertToString((Enum)value));
writeObjectEnd();
} else {
writer.value(convertToString((Enum)value));
}
return;
}
writeObjectStart(actualType, knownType);
writeFields(value);
writeObjectEnd();
} catch (IOException ex) {
throw new JsonException(ex);
}
}
public void writeObjectStart (String name) {
try {
writer.name(name);
} catch (IOException ex) {
throw new JsonException(ex);
}
writeObjectStart();
}
/** @param knownType May be null if the type is unknown. */
public void writeObjectStart (String name, Class actualType, Class knownType) {
try {
writer.name(name);
} catch (IOException ex) {
throw new JsonException(ex);
}
writeObjectStart(actualType, knownType);
}
public void writeObjectStart () {
try {
writer.object();
} catch (IOException ex) {
throw new JsonException(ex);
}
}
/** Starts writing an object, writing the actualType to a field if needed.
* @param knownType May be null if the type is unknown. */
public void writeObjectStart (Class actualType, Class knownType) {
try {
writer.object();
} catch (IOException ex) {
throw new JsonException(ex);
}
if (knownType == null || knownType != actualType) writeType(actualType);
}
public void writeObjectEnd () {
try {
writer.pop();
} catch (IOException ex) {
throw new JsonException(ex);
}
}
public void writeArrayStart (String name) {
try {
writer.name(name);
writer.array();
} catch (IOException ex) {
throw new JsonException(ex);
}
}
public void writeArrayStart () {
try {
writer.array();
} catch (IOException ex) {
throw new JsonException(ex);
}
}
public void writeArrayEnd () {
try {
writer.pop();
} catch (IOException ex) {
throw new JsonException(ex);
}
}
public void writeType (Class type) {
if (typeName == null) return;
String className = getTag(type);
if (className == null) className = type.getName();
try {
writer.set(typeName, className);
} catch (IOException ex) {
throw new JsonException(ex);
}
if (debug) System.out.println("Writing type: " + type.getName());
}
/** @param type May be null if the type is unknown.
* @return May be null. */
public <T> T fromJson (Class<T> type, Reader reader) {
return (T)readValue(type, null, new JsonReader().parse(reader));
}
/** @param type May be null if the type is unknown.
* @param elementType May be null if the type is unknown.
* @return May be null. */
public <T> T fromJson (Class<T> type, Class elementType, Reader reader) {
return (T)readValue(type, elementType, new JsonReader().parse(reader));
}
/** @param type May be null if the type is unknown.
* @return May be null. */
public <T> T fromJson (Class<T> type, InputStream input) {
return (T)readValue(type, null, new JsonReader().parse(input));
}
/** @param type May be null if the type is unknown.
* @param elementType May be null if the type is unknown.
* @return May be null. */
public <T> T fromJson (Class<T> type, Class elementType, InputStream input) {
return (T)readValue(type, elementType, new JsonReader().parse(input));
}
/** @param type May be null if the type is unknown.
* @return May be null. */
public <T> T fromJson (Class<T> type, File file) {
try {
return (T)readValue(type, null, new JsonReader().parse(file));
} catch (Exception ex) {
throw new JsonException("Error reading file: " + file, ex);
}
}
/** @param type May be null if the type is unknown.
* @param elementType May be null if the type is unknown.
* @return May be null. */
public <T> T fromJson (Class<T> type, Class elementType, File file) {
try {
return (T)readValue(type, elementType, new JsonReader().parse(file));
} catch (Exception ex) {
throw new JsonException("Error reading file: " + file, ex);
}
}
/** @param type May be null if the type is unknown.
* @return May be null. */
public <T> T fromJson (Class<T> type, char[] data, int offset, int length) {
return (T)readValue(type, null, new JsonReader().parse(data, offset, length));
}
/** @param type May be null if the type is unknown.
* @param elementType May be null if the type is unknown.
* @return May be null. */
public <T> T fromJson (Class<T> type, Class elementType, char[] data, int offset, int length) {
return (T)readValue(type, elementType, new JsonReader().parse(data, offset, length));
}
/** @param type May be null if the type is unknown.
* @return May be null. */
public <T> T fromJson (Class<T> type, String json) {
return (T)readValue(type, null, new JsonReader().parse(json));
}
/** @param type May be null if the type is unknown.
* @return May be null. */
public <T> T fromJson (Class<T> type, Class elementType, String json) {
return (T)readValue(type, elementType, new JsonReader().parse(json));
}
public void readField (Object object, String name, JsonValue jsonData) {
readField(object, name, name, null, jsonData);
}
public void readField (Object object, String name, Class elementType, JsonValue jsonData) {
readField(object, name, name, elementType, jsonData);
}
public void readField (Object object, String fieldName, String jsonName, JsonValue jsonData) {
readField(object, fieldName, jsonName, null, jsonData);
}
/** @param elementType May be null if the type is unknown. */
public void readField (Object object, String fieldName, String jsonName, Class elementType, JsonValue jsonMap) {
Class type = object.getClass();
ObjectMap<String, FieldMetadata> fields = getFields(type);
FieldMetadata metadata = fields.get(fieldName);
if (metadata == null) throw new JsonException("Field not found: " + fieldName + " (" + type.getName() + ")");
Field field = metadata.field;
if (elementType == null) elementType = metadata.elementType;
readField(object, field, jsonName, elementType, jsonMap);
}
/** @param object May be null if the field is static.
* @param elementType May be null if the type is unknown. */
public void readField (Object object, Field field, String jsonName, Class elementType, JsonValue jsonMap) {
JsonValue jsonValue = jsonMap.get(jsonName);
if (jsonValue == null) return;
try {
field.set(object, readValue(field.getType(), elementType, jsonValue));
} catch (IllegalAccessException ex) {
throw new JsonException("Error accessing field: " + field.getName() + " (" + field.getDeclaringClass().getName() + ")",
ex);
} catch (JsonException ex) {
ex.addTrace(field.getName() + " (" + field.getDeclaringClass().getName() + ")");
throw ex;
} catch (RuntimeException runtimeEx) {
JsonException ex = new JsonException(runtimeEx);
ex.addTrace(field.getName() + " (" + field.getDeclaringClass().getName() + ")");
throw ex;
}
}
public void readFields (Object object, JsonValue jsonMap) {
Class type = object.getClass();
ObjectMap<String, FieldMetadata> fields = getFields(type);
for (JsonValue child = jsonMap.child; child != null; child = child.next) {
FieldMetadata metadata = fields.get(child.name().replace(" ", "_"));
if (metadata == null) {
if (ignoreUnknownFields) {
if (debug) System.out.println("Ignoring unknown field: " + child.name() + " (" + type.getName() + ")");
continue;
} else
throw new JsonException("Field not found: " + child.name() + " (" + type.getName() + ")");
}
Field field = metadata.field;
try {
field.set(object, readValue(field.getType(), metadata.elementType, child));
} catch (IllegalAccessException ex) {
throw new JsonException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
} catch (JsonException ex) {
ex.addTrace(field.getName() + " (" + type.getName() + ")");
throw ex;
} catch (RuntimeException runtimeEx) {
JsonException ex = new JsonException(runtimeEx);
ex.addTrace(field.getName() + " (" + type.getName() + ")");
throw ex;
}
}
}
/** @param type May be null if the type is unknown.
* @return May be null. */
public <T> T readValue (String name, Class<T> type, JsonValue jsonMap) {
return (T)readValue(type, null, jsonMap.get(name));
}
/** @param type May be null if the type is unknown.
* @return May be null. */
public <T> T readValue (String name, Class<T> type, T defaultValue, JsonValue jsonMap) {
JsonValue jsonValue = jsonMap.get(name);
if (jsonValue == null) return defaultValue;
return (T)readValue(type, null, jsonValue);
}
/** @param type May be null if the type is unknown.
* @param elementType May be null if the type is unknown.
* @return May be null. */
public <T> T readValue (String name, Class<T> type, Class elementType, JsonValue jsonMap) {
return (T)readValue(type, elementType, jsonMap.get(name));
}
/** @param type May be null if the type is unknown.
* @param elementType May be null if the type is unknown.
* @return May be null. */
public <T> T readValue (String name, Class<T> type, Class elementType, T defaultValue, JsonValue jsonMap) {
JsonValue jsonValue = jsonMap.get(name);
if (jsonValue == null) return defaultValue;
return (T)readValue(type, elementType, jsonValue);
}
/** @param type May be null if the type is unknown.
* @param elementType May be null if the type is unknown.
* @return May be null. */
public <T> T readValue (Class<T> type, Class elementType, T defaultValue, JsonValue jsonData) {
return (T)readValue(type, elementType, jsonData);
}
/** @param type May be null if the type is unknown.
* @return May be null. */
public <T> T readValue (Class<T> type, JsonValue jsonData) {
return (T)readValue(type, null, jsonData);
}
/** @param type May be null if the type is unknown.
* @param elementType May be null if the type is unknown.
* @return May be null. */
public <T> T readValue (Class<T> type, Class elementType, JsonValue jsonData) {
if (jsonData == null) return null;
if (jsonData.isObject()) {
String className = typeName == null ? null : jsonData.getString(typeName, null);
if (className != null) {
jsonData.remove(typeName);
type = getClass(className);
if (type == null) {
try {
type = (Class<T>)Class.forName(className);
} catch (ClassNotFoundException ex) {
throw new JsonException(ex);
}
}
}
if (type == null) {
if (defaultSerializer != null) return (T)defaultSerializer.read(this, jsonData, type);
return (T)jsonData;
}
if (type == String.class || type == Integer.class || type == Boolean.class || type == Float.class || type == Long.class
|| type == Double.class || type == Short.class || type == Byte.class || type == Character.class
|| Enum.class.isAssignableFrom(type)) {
return readValue("value", type, jsonData);
}
if (typeName != null && Collection.class.isAssignableFrom(type)) {
// JSON object wrapper to specify type.
jsonData = jsonData.get("items");
if (jsonData == null)
throw new JsonException("Unable to convert object to collection: " + jsonData + " (" + type.getName() + ")");
} else {
JsonSerializer serializer = classToSerializer.get(type);
if (serializer != null) return (T)serializer.read(this, jsonData, type);
Object object = newInstance(type);
if (object instanceof JsonSerializable) {
((JsonSerializable)object).read(this, jsonData);
return (T)object;
}
// JSON object special cases.
if (object instanceof ObjectMap) {
ObjectMap result = (ObjectMap)object;
for (JsonValue child = jsonData.child; child != null; child = child.next)
result.put(child.name(), readValue(elementType, null, child));
return (T)result;
}
if (object instanceof Map) {
Map result = (Map)object;
for (JsonValue child = jsonData.child; child != null; child = child.next)
result.put(child.name(), readValue(elementType, null, child));
return (T)result;
}
readFields(object, jsonData);
return (T)object;
}
}
if (type != null) {
JsonSerializer serializer = classToSerializer.get(type);
if (serializer != null) return (T)serializer.read(this, jsonData, type);
if (JsonSerializable.class.isAssignableFrom(type)) {
// A Serializable may be read as an array, string, etc, even though it will be written as an object.
Object object = newInstance(type);
((JsonSerializable)object).read(this, jsonData);
return (T)object;
}
}
if (jsonData.isArray()) {
// JSON array special cases.
if (type == null || type == Object.class) type = (Class<T>)ArrayList.class;
if (Collection.class.isAssignableFrom(type)) {
Collection result = type.isInterface() ? new ArrayList() : (Collection)newInstance(type);
for (JsonValue child = jsonData.child; child != null; child = child.next)
result.add(readValue(elementType, null, child));
return (T)result;
}
if (type.isArray()) {
Class componentType = type.getComponentType();
if (elementType == null) elementType = componentType;
Object result = Array.newInstance(componentType, jsonData.size);
int i = 0;
for (JsonValue child = jsonData.child; child != null; child = child.next)
Array.set(result, i++, readValue(elementType, null, child));
return (T)result;
}
throw new JsonException("Unable to convert value to required type: " + jsonData + " (" + type.getName() + ")");
}
if (jsonData.isNumber()) {
try {
if (type == null || type == float.class || type == Float.class) return (T)(Float)jsonData.asFloat();
if (type == int.class || type == Integer.class) return (T)(Integer)jsonData.asInt();
if (type == long.class || type == Long.class) return (T)(Long)jsonData.asLong();
if (type == double.class || type == Double.class) return (T)(Double)jsonData.asDouble();
if (type == String.class) return (T)jsonData.asString();
if (type == short.class || type == Short.class) return (T)(Short)jsonData.asShort();
if (type == byte.class || type == Byte.class) return (T)(Byte)jsonData.asByte();
} catch (NumberFormatException ignored) {
}
jsonData = new JsonValue(jsonData.asString());
}
if (jsonData.isBoolean()) {
try {
if (type == null || type == boolean.class || type == Boolean.class) return (T)(Boolean)jsonData.asBoolean();
} catch (NumberFormatException ignored) {
}
jsonData = new JsonValue(jsonData.asString());
}
if (jsonData.isString()) {
String string = jsonData.asString();
if (type == null || type == String.class) return (T)string;
try {
if (type == int.class || type == Integer.class) return (T)Integer.valueOf(string);
if (type == float.class || type == Float.class) return (T)Float.valueOf(string);
if (type == long.class || type == Long.class) return (T)Long.valueOf(string);
if (type == double.class || type == Double.class) return (T)Double.valueOf(string);
if (type == short.class || type == Short.class) return (T)Short.valueOf(string);
if (type == byte.class || type == Byte.class) return (T)Byte.valueOf(string);
} catch (NumberFormatException ignored) {
}
if (type == boolean.class || type == Boolean.class) return (T)Boolean.valueOf(string);
if (type == char.class || type == Character.class) return (T)(Character)string.charAt(0);
if (Enum.class.isAssignableFrom(type)) {
Enum[] constants = (Enum[])type.getEnumConstants();
for (int i = 0, n = constants.length; i < n; i++) {
Enum e = constants[i];
if (string.equals(convertToString(e))) return (T)e;
}
}
if (type == CharSequence.class) return (T)string;
throw new JsonException("Unable to convert value to required type: " + jsonData + " (" + type.getName() + ")");
}
return null;
}
private String convertToString (Enum e) {
return enumNames ? e.name() : e.toString();
}
private String convertToString (Object object) {
if (object instanceof Enum) return convertToString((Enum)object);
if (object instanceof Class) return ((Class)object).getName();
return String.valueOf(object);
}
protected Object newInstance (Class type) {
try {
return type.newInstance();
} catch (Exception ex) {
try {
// Try a private constructor.
Constructor constructor = type.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();