/
JsonParser.java
962 lines (912 loc) · 38.1 KB
/
JsonParser.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
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
/*
* Copyright (c) 2010 Google Inc.
*
* 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.google.api.client.json;
import com.google.api.client.json.JsonPolymorphicTypeMap.TypeDef;
import com.google.api.client.util.Beta;
import com.google.api.client.util.ClassInfo;
import com.google.api.client.util.Data;
import com.google.api.client.util.FieldInfo;
import com.google.api.client.util.GenericData;
import com.google.api.client.util.Preconditions;
import com.google.api.client.util.Sets;
import com.google.api.client.util.Types;
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Abstract low-level JSON parser. See <a
* href="https://developers.google.com/api-client-library/java/google-http-java-client/json">
* https://developers.google.com/api-client-library/java/google-http-java-client/json</a>
*
* <p>Implementation has no fields and therefore thread-safe, but sub-classes are not necessarily
* thread-safe.
*
* <p>If a JSON map is encountered while using a destination class of type Map, then an {@link
* java.util.ArrayMap} is used by default for the parsed values.
*
* @since 1.3
* @author Yaniv Inbar
*/
public abstract class JsonParser implements Closeable {
/**
* Maps a polymorphic {@link Class} to its {@link Field} with the {@link JsonPolymorphicTypeMap}
* annotation, or {@code null} if there is no field with that annotation.
*/
private static WeakHashMap<Class<?>, Field> cachedTypemapFields =
new WeakHashMap<Class<?>, Field>();
/** Lock on the {@code cachedTypemapFields}. */
private static final Lock lock = new ReentrantLock();
/** Returns the JSON factory from which this generator was created. */
public abstract JsonFactory getFactory();
/**
* Closes the parser and the underlying input stream or reader, and releases any memory associated
* with it.
*/
public abstract void close() throws IOException;
/** Returns the next token from the stream or {@code null} to indicate end of input. */
public abstract JsonToken nextToken() throws IOException;
/**
* Returns the token the parser currently points to or {@code null} for none (at start of input or
* after end of input).
*/
public abstract JsonToken getCurrentToken();
/**
* Returns the most recent field name or {@code null} for array values or for root-level values.
*/
public abstract String getCurrentName() throws IOException;
/**
* Skips to the matching {@link JsonToken#END_ARRAY} if current token is {@link
* JsonToken#START_ARRAY}, the matching {@link JsonToken#END_OBJECT} if the current token is
* {@link JsonToken#START_OBJECT}, else does nothing.
*/
public abstract JsonParser skipChildren() throws IOException;
/**
* Returns a textual representation of the current token or {@code null} if {@link
* #getCurrentToken()} is {@code null}.
*/
public abstract String getText() throws IOException;
// TODO(yanivi): Jackson provides getTextCharacters(), getTextLength(), and getTextOffset()
/** Returns the byte value of the current token. */
public abstract byte getByteValue() throws IOException;
/** Returns the short value of the current token. */
public abstract short getShortValue() throws IOException;
/** Returns the int value of the current token. */
public abstract int getIntValue() throws IOException;
/** Returns the float value of the current token. */
public abstract float getFloatValue() throws IOException;
/** Returns the long value of the current token. */
public abstract long getLongValue() throws IOException;
/** Returns the double value of the current token. */
public abstract double getDoubleValue() throws IOException;
/** Returns the {@link BigInteger} value of the current token. */
public abstract BigInteger getBigIntegerValue() throws IOException;
/** Returns the {@link BigDecimal} value of the current token. */
public abstract BigDecimal getDecimalValue() throws IOException;
/**
* Parse a JSON object, array, or value into a new instance of the given destination class, and
* then closes the parser.
*
* @param <T> destination class
* @param destinationClass destination class that has a public default constructor to use to
* create a new instance
* @return new instance of the parsed destination class
* @since 1.15
*/
public final <T> T parseAndClose(Class<T> destinationClass) throws IOException {
return parseAndClose(destinationClass, null);
}
/**
* {@link Beta} <br>
* Parse a JSON object, array, or value into a new instance of the given destination class using
* {@link JsonParser#parse(Class, CustomizeJsonParser)}, and then closes the parser.
*
* @param <T> destination class
* @param destinationClass destination class that has a public default constructor to use to
* create a new instance
* @param customizeParser optional parser customizer or {@code null} for none
* @return new instance of the parsed destination class
*/
@Beta
public final <T> T parseAndClose(Class<T> destinationClass, CustomizeJsonParser customizeParser)
throws IOException {
try {
return parse(destinationClass, customizeParser);
} finally {
close();
}
}
/**
* Skips the values of all keys in the current object until it finds the given key.
*
* <p>Before this method is called, the parser must either point to the start or end of a JSON
* object or to a field name. After this method ends, the current token will either be the {@link
* JsonToken#END_OBJECT} of the current object if the key is not found, or the value of the key
* that was found.
*
* @param keyToFind key to find
*/
public final void skipToKey(String keyToFind) throws IOException {
skipToKey(Collections.singleton(keyToFind));
}
/**
* Skips the values of all keys in the current object until it finds one of the given keys.
*
* <p>Before this method is called, the parser must either point to the start or end of a JSON
* object or to a field name. After this method ends, the current token will either be the {@link
* JsonToken#END_OBJECT} of the current object if no matching key is found, or the value of the
* key that was found.
*
* @param keysToFind set of keys to look for
* @return name of the first matching key found or {@code null} if no match was found
* @since 1.10
*/
public final String skipToKey(Set<String> keysToFind) throws IOException {
JsonToken curToken = startParsingObjectOrArray();
while (curToken == JsonToken.FIELD_NAME) {
String key = getText();
nextToken();
if (keysToFind.contains(key)) {
return key;
}
skipChildren();
curToken = nextToken();
}
return null;
}
/** Starts parsing that handles start of input by calling {@link #nextToken()}. */
private JsonToken startParsing() throws IOException {
JsonToken currentToken = getCurrentToken();
// token is null at start, so get next token
if (currentToken == null) {
currentToken = nextToken();
}
Preconditions.checkArgument(currentToken != null, "no JSON input found");
return currentToken;
}
/**
* Starts parsing an object or array by making sure the parser points to an object field name,
* first array value or end of object or array.
*
* <p>If the parser is at the start of input, {@link #nextToken()} is called. The current token
* must then be {@link JsonToken#START_OBJECT}, {@link JsonToken#END_OBJECT}, {@link
* JsonToken#START_ARRAY}, {@link JsonToken#END_ARRAY}, or {@link JsonToken#FIELD_NAME}. For an
* object only, after the method is called, the current token must be either {@link
* JsonToken#FIELD_NAME} or {@link JsonToken#END_OBJECT}.
*/
private JsonToken startParsingObjectOrArray() throws IOException {
JsonToken currentToken = startParsing();
switch (currentToken) {
case START_OBJECT:
currentToken = nextToken();
Preconditions.checkArgument(
currentToken == JsonToken.FIELD_NAME || currentToken == JsonToken.END_OBJECT,
currentToken);
break;
case START_ARRAY:
currentToken = nextToken();
break;
default:
break;
}
return currentToken;
}
/**
* Parse a JSON Object from the given JSON parser -- which is closed after parsing completes --
* into the given destination object.
*
* <p>Before this method is called, the parser must either point to the start or end of a JSON
* object or to a field name.
*
* @param destination destination object
* @since 1.15
*/
public final void parseAndClose(Object destination) throws IOException {
parseAndClose(destination, null);
}
/**
* {@link Beta} <br>
* Parse a JSON Object from the given JSON parser -- which is closed after parsing completes --
* into the given destination object, optionally using the given parser customizer.
*
* <p>Before this method is called, the parser must either point to the start or end of a JSON
* object or to a field name.
*
* @param destination destination object
* @param customizeParser optional parser customizer or {@code null} for none
*/
@Beta
public final void parseAndClose(Object destination, CustomizeJsonParser customizeParser)
throws IOException {
try {
parse(destination, customizeParser);
} finally {
close();
}
}
/**
* Parse a JSON object, array, or value into a new instance of the given destination class.
*
* <p>If it parses an object, after this method ends, the current token will be the object's
* ending {@link JsonToken#END_OBJECT}. If it parses an array, after this method ends, the current
* token will be the array's ending {@link JsonToken#END_ARRAY}.
*
* @param <T> destination class
* @param destinationClass destination class that has a public default constructor to use to
* create a new instance
* @return new instance of the parsed destination class
* @since 1.15
*/
public final <T> T parse(Class<T> destinationClass) throws IOException {
return parse(destinationClass, null);
}
/**
* {@link Beta} <br>
* Parse a JSON object, array, or value into a new instance of the given destination class,
* optionally using the given parser customizer.
*
* <p>If it parses an object, after this method ends, the current token will be the object's
* ending {@link JsonToken#END_OBJECT}. If it parses an array, after this method ends, the current
* token will be the array's ending {@link JsonToken#END_ARRAY}.
*
* @param <T> destination class
* @param destinationClass destination class that has a public default constructor to use to
* create a new instance
* @param customizeParser optional parser customizer or {@code null} for none
* @return new instance of the parsed destination class
*/
@Beta
public final <T> T parse(Class<T> destinationClass, CustomizeJsonParser customizeParser)
throws IOException {
@SuppressWarnings("unchecked")
T result = (T) parse(destinationClass, false, customizeParser);
return result;
}
/**
* Parse a JSON object, array, or value into a new instance of the given destination class.
*
* <p>If it parses an object, after this method ends, the current token will be the object's
* ending {@link JsonToken#END_OBJECT}. If it parses an array, after this method ends, the current
* token will be the array's ending {@link JsonToken#END_ARRAY}.
*
* @param dataType Type into which the JSON should be parsed
* @param close {@code true} if {@link #close()} should be called after parsing
* @return new instance of the parsed dataType
* @since 1.15
*/
public Object parse(Type dataType, boolean close) throws IOException {
return parse(dataType, close, null);
}
/**
* {@link Beta} <br>
* Parse a JSON object, array, or value into a new instance of the given destination class,
* optionally using the given parser customizer.
*
* <p>If it parses an object, after this method ends, the current token will be the object's
* ending {@link JsonToken#END_OBJECT}. If it parses an array, after this method ends, the current
* token will be the array's ending {@link JsonToken#END_ARRAY}.
*
* @param dataType Type into which the JSON should be parsed
* @param close {@code true} if {@link #close()} should be called after parsing
* @param customizeParser optional parser customizer or {@code null} for none
* @return new instance of the parsed dataType
* @since 1.10
*/
@Beta
public Object parse(Type dataType, boolean close, CustomizeJsonParser customizeParser)
throws IOException {
try {
if (!Void.class.equals(dataType)) {
startParsing();
}
return parseValue(null, dataType, new ArrayList<Type>(), null, customizeParser, true);
} finally {
if (close) {
close();
}
}
}
/**
* Parse a JSON object from the given JSON parser into the given destination object.
*
* <p>Before this method is called, the parser must either point to the start or end of a JSON
* object or to a field name. After this method ends, the current token will be the {@link
* JsonToken#END_OBJECT} of the current object.
*
* @param destination destination object
* @since 1.15
*/
public final void parse(Object destination) throws IOException {
parse(destination, null);
}
/**
* {@link Beta} <br>
* Parse a JSON object from the given JSON parser into the given destination object, optionally
* using the given parser customizer.
*
* <p>Before this method is called, the parser must either point to the start or end of a JSON
* object or to a field name. After this method ends, the current token will be the {@link
* JsonToken#END_OBJECT} of the current object.
*
* @param destination destination object
* @param customizeParser optional parser customizer or {@code null} for none
*/
@Beta
public final void parse(Object destination, CustomizeJsonParser customizeParser)
throws IOException {
ArrayList<Type> context = new ArrayList<Type>();
context.add(destination.getClass());
parse(context, destination, customizeParser);
}
/**
* Parses the next field from the given JSON parser into the given destination object.
*
* @param context destination context stack (possibly empty)
* @param destination destination object instance or {@code null} for none (for example empty
* context stack)
* @param customizeParser optional parser customizer or {@code null} for none
*/
private void parse(
ArrayList<Type> context, Object destination, CustomizeJsonParser customizeParser)
throws IOException {
if (destination instanceof GenericJson) {
((GenericJson) destination).setFactory(getFactory());
}
JsonToken curToken = startParsingObjectOrArray();
Class<?> destinationClass = destination.getClass();
ClassInfo classInfo = ClassInfo.of(destinationClass);
boolean isGenericData = GenericData.class.isAssignableFrom(destinationClass);
if (!isGenericData && Map.class.isAssignableFrom(destinationClass)) {
// The destination class is not a sub-class of GenericData but is of Map, so parse data
// using parseMap.
@SuppressWarnings("unchecked")
Map<String, Object> destinationMap = (Map<String, Object>) destination;
parseMap(
null,
destinationMap,
Types.getMapValueParameter(destinationClass),
context,
customizeParser);
return;
}
while (curToken == JsonToken.FIELD_NAME) {
String key = getText();
nextToken();
// stop at items for feeds
if (customizeParser != null && customizeParser.stopAt(destination, key)) {
return;
}
// get the field from the type information
FieldInfo fieldInfo = classInfo.getFieldInfo(key);
if (fieldInfo != null) {
// skip final fields
if (fieldInfo.isFinal() && !fieldInfo.isPrimitive()) {
throw new IllegalArgumentException("final array/object fields are not supported");
}
Field field = fieldInfo.getField();
int contextSize = context.size();
context.add(field.getGenericType());
Object fieldValue =
parseValue(
field, fieldInfo.getGenericType(), context, destination, customizeParser, true);
context.remove(contextSize);
fieldInfo.setValue(destination, fieldValue);
} else if (isGenericData) {
// store unknown field in generic JSON
GenericData object = (GenericData) destination;
object.set(key, parseValue(null, null, context, destination, customizeParser, true));
} else {
// unrecognized field, skip value.
if (customizeParser != null) {
customizeParser.handleUnrecognizedKey(destination, key);
}
skipChildren();
}
curToken = nextToken();
}
}
/**
* Parse a JSON Array from the given JSON parser (which is closed after parsing completes) into
* the given destination collection.
*
* @param destinationCollectionClass class of destination collection (must have a public default
* constructor)
* @param destinationItemClass class of destination collection item (must have a public default
* constructor)
* @since 1.15
*/
public final <T> Collection<T> parseArrayAndClose(
Class<?> destinationCollectionClass, Class<T> destinationItemClass) throws IOException {
return parseArrayAndClose(destinationCollectionClass, destinationItemClass, null);
}
/**
* {@link Beta} <br>
* Parse a JSON Array from the given JSON parser (which is closed after parsing completes) into
* the given destination collection, optionally using the given parser customizer.
*
* @param destinationCollectionClass class of destination collection (must have a public default
* constructor)
* @param destinationItemClass class of destination collection item (must have a public default
* constructor)
* @param customizeParser optional parser customizer or {@code null} for none
*/
@Beta
public final <T> Collection<T> parseArrayAndClose(
Class<?> destinationCollectionClass,
Class<T> destinationItemClass,
CustomizeJsonParser customizeParser)
throws IOException {
try {
return parseArray(destinationCollectionClass, destinationItemClass, customizeParser);
} finally {
close();
}
}
/**
* Parse a JSON Array from the given JSON parser (which is closed after parsing completes) into
* the given destination collection.
*
* @param destinationCollection destination collection
* @param destinationItemClass class of destination collection item (must have a public default
* constructor)
* @since 1.15
*/
public final <T> void parseArrayAndClose(
Collection<? super T> destinationCollection, Class<T> destinationItemClass)
throws IOException {
parseArrayAndClose(destinationCollection, destinationItemClass, null);
}
/**
* {@link Beta} <br>
* Parse a JSON Array from the given JSON parser (which is closed after parsing completes) into
* the given destination collection, optionally using the given parser customizer.
*
* @param destinationCollection destination collection
* @param destinationItemClass class of destination collection item (must have a public default
* constructor)
* @param customizeParser optional parser customizer or {@code null} for none
*/
@Beta
public final <T> void parseArrayAndClose(
Collection<? super T> destinationCollection,
Class<T> destinationItemClass,
CustomizeJsonParser customizeParser)
throws IOException {
try {
parseArray(destinationCollection, destinationItemClass, customizeParser);
} finally {
close();
}
}
/**
* Parse a JSON Array from the given JSON parser into the given destination collection.
*
* @param destinationCollectionClass class of destination collection (must have a public default
* constructor)
* @param destinationItemClass class of destination collection item (must have a public default
* constructor)
* @since 1.15
*/
public final <T> Collection<T> parseArray(
Class<?> destinationCollectionClass, Class<T> destinationItemClass) throws IOException {
return parseArray(destinationCollectionClass, destinationItemClass, null);
}
/**
* {@link Beta} <br>
* Parse a JSON Array from the given JSON parser into the given destination collection, optionally
* using the given parser customizer.
*
* @param destinationCollectionClass class of destination collection (must have a public default
* constructor)
* @param destinationItemClass class of destination collection item (must have a public default
* constructor)
* @param customizeParser optional parser customizer or {@code null} for none
*/
@Beta
public final <T> Collection<T> parseArray(
Class<?> destinationCollectionClass,
Class<T> destinationItemClass,
CustomizeJsonParser customizeParser)
throws IOException {
@SuppressWarnings("unchecked")
Collection<T> destinationCollection =
(Collection<T>) Data.newCollectionInstance(destinationCollectionClass);
parseArray(destinationCollection, destinationItemClass, customizeParser);
return destinationCollection;
}
/**
* Parse a JSON Array from the given JSON parser into the given destination collection.
*
* @param destinationCollection destination collection
* @param destinationItemClass class of destination collection item (must have a public default
* constructor)
* @since 1.15
*/
public final <T> void parseArray(
Collection<? super T> destinationCollection, Class<T> destinationItemClass)
throws IOException {
parseArray(destinationCollection, destinationItemClass, null);
}
/**
* {@link Beta} <br>
* Parse a JSON Array from the given JSON parser into the given destination collection, optionally
* using the given parser customizer.
*
* @param destinationCollection destination collection
* @param destinationItemClass class of destination collection item (must have a public default
* constructor)
* @param customizeParser optional parser customizer or {@code null} for none
*/
@Beta
public final <T> void parseArray(
Collection<? super T> destinationCollection,
Class<T> destinationItemClass,
CustomizeJsonParser customizeParser)
throws IOException {
parseArray(
null, destinationCollection, destinationItemClass, new ArrayList<Type>(), customizeParser);
}
/**
* Parse a JSON Array from the given JSON parser into the given destination collection, optionally
* using the given parser customizer.
*
* @param fieldContext field context or {@code null} for none
* @param destinationCollection destination collection
* @param destinationItemType type of destination collection item
* @param context destination context stack (possibly empty)
* @param customizeParser optional parser customizer or {@code null} for none
*/
private <T> void parseArray(
Field fieldContext,
Collection<T> destinationCollection,
Type destinationItemType,
ArrayList<Type> context,
CustomizeJsonParser customizeParser)
throws IOException {
JsonToken curToken = startParsingObjectOrArray();
while (curToken != JsonToken.END_ARRAY) {
@SuppressWarnings("unchecked")
T parsedValue =
(T)
parseValue(
fieldContext,
destinationItemType,
context,
destinationCollection,
customizeParser,
true);
destinationCollection.add(parsedValue);
curToken = nextToken();
}
}
/**
* Parse a JSON Object from the given JSON parser into the given destination map, optionally using
* the given parser customizer.
*
* @param fieldContext field context or {@code null} for none
* @param destinationMap destination map
* @param valueType valueType of the map value type parameter
* @param context destination context stack (possibly empty)
* @param customizeParser optional parser customizer or {@code null} for none
*/
private void parseMap(
Field fieldContext,
Map<String, Object> destinationMap,
Type valueType,
ArrayList<Type> context,
CustomizeJsonParser customizeParser)
throws IOException {
JsonToken curToken = startParsingObjectOrArray();
while (curToken == JsonToken.FIELD_NAME) {
String key = getText();
nextToken();
// stop at items for feeds
if (customizeParser != null && customizeParser.stopAt(destinationMap, key)) {
return;
}
Object value =
parseValue(fieldContext, valueType, context, destinationMap, customizeParser, true);
destinationMap.put(key, value);
curToken = nextToken();
}
}
/**
* Parse a value.
*
* @param fieldContext field context or {@code null} for none (for example into a map)
* @param valueType value type or {@code null} if not known (for example into a map)
* @param context destination context stack (possibly empty)
* @param destination destination object instance or {@code null} for none (for example empty
* context stack)
* @param customizeParser customize parser or {@code null} for none
* @param handlePolymorphic whether or not to check for polymorphic schema
* @return parsed value
*/
private final Object parseValue(
Field fieldContext,
Type valueType,
ArrayList<Type> context,
Object destination,
CustomizeJsonParser customizeParser,
boolean handlePolymorphic)
throws IOException {
valueType = Data.resolveWildcardTypeOrTypeVariable(context, valueType);
// resolve a parameterized type to a class
Class<?> valueClass = valueType instanceof Class<?> ? (Class<?>) valueType : null;
if (valueType instanceof ParameterizedType) {
valueClass = Types.getRawClass((ParameterizedType) valueType);
}
// Void means skip
if (valueClass == Void.class) {
skipChildren();
return null;
}
// value type is now null, class, parameterized type, or generic array type
JsonToken token = getCurrentToken();
try {
switch (token) {
case START_ARRAY:
case END_ARRAY:
boolean isArray = Types.isArray(valueType);
Preconditions.checkArgument(
valueType == null
|| isArray
|| valueClass != null && Types.isAssignableToOrFrom(valueClass, Collection.class),
"expected collection or array type but got %s",
valueType);
Collection<Object> collectionValue = null;
if (customizeParser != null && fieldContext != null) {
collectionValue = customizeParser.newInstanceForArray(destination, fieldContext);
}
if (collectionValue == null) {
collectionValue = Data.newCollectionInstance(valueType);
}
Type subType = null;
if (isArray) {
subType = Types.getArrayComponentType(valueType);
} else if (valueClass != null && Iterable.class.isAssignableFrom(valueClass)) {
subType = Types.getIterableParameter(valueType);
}
subType = Data.resolveWildcardTypeOrTypeVariable(context, subType);
parseArray(fieldContext, collectionValue, subType, context, customizeParser);
if (isArray) {
return Types.toArray(collectionValue, Types.getRawArrayComponentType(context, subType));
}
return collectionValue;
case FIELD_NAME:
case START_OBJECT:
case END_OBJECT:
Preconditions.checkArgument(
!Types.isArray(valueType), "expected object or map type but got %s", valueType);
// Check if we're parsing into a polymorphic datatype.
Field typemapField = handlePolymorphic ? getCachedTypemapFieldFor(valueClass) : null;
Object newInstance = null;
if (valueClass != null && customizeParser != null) {
newInstance = customizeParser.newInstanceForObject(destination, valueClass);
}
boolean isMap = valueClass != null && Types.isAssignableToOrFrom(valueClass, Map.class);
if (typemapField != null) {
newInstance = new GenericJson();
} else if (newInstance == null) {
// check if it is a map to avoid ClassCastException to Map
if (isMap || valueClass == null) {
newInstance = Data.newMapInstance(valueClass);
} else {
newInstance = Types.newInstance(valueClass);
}
}
int contextSize = context.size();
if (valueType != null) {
context.add(valueType);
}
if (isMap && !GenericData.class.isAssignableFrom(valueClass)) {
Type subValueType =
Map.class.isAssignableFrom(valueClass)
? Types.getMapValueParameter(valueType)
: null;
if (subValueType != null) {
@SuppressWarnings("unchecked")
Map<String, Object> destinationMap = (Map<String, Object>) newInstance;
parseMap(fieldContext, destinationMap, subValueType, context, customizeParser);
return newInstance;
}
}
parse(context, newInstance, customizeParser);
if (valueType != null) {
context.remove(contextSize);
}
if (typemapField == null) {
return newInstance;
}
// Get the correct type out of the naively parsed data.
Object typeValueObject = ((GenericJson) newInstance).get(typemapField.getName());
Preconditions.checkArgument(
typeValueObject != null, "No value specified for @JsonPolymorphicTypeMap field");
String typeValue = typeValueObject.toString();
JsonPolymorphicTypeMap typeMap = typemapField.getAnnotation(JsonPolymorphicTypeMap.class);
Class<?> typeClass = null;
for (TypeDef typeDefinition : typeMap.typeDefinitions()) {
if (typeDefinition.key().equals(typeValue)) {
typeClass = typeDefinition.ref();
break;
}
}
Preconditions.checkArgument(
typeClass != null, "No TypeDef annotation found with key: " + typeValue);
JsonFactory factory = getFactory();
// TODO(ngmiceli): Avoid having to parse JSON content twice. Optimize when type is first.
JsonParser parser = factory.createJsonParser(factory.toString(newInstance));
parser.startParsing();
return parser.parseValue(fieldContext, typeClass, context, null, null, false);
case VALUE_TRUE:
case VALUE_FALSE:
Preconditions.checkArgument(
valueType == null
|| valueClass == boolean.class
|| valueClass != null && valueClass.isAssignableFrom(Boolean.class),
"expected type Boolean or boolean but got %s",
valueType);
return token == JsonToken.VALUE_TRUE ? Boolean.TRUE : Boolean.FALSE;
case VALUE_NUMBER_FLOAT:
case VALUE_NUMBER_INT:
Preconditions.checkArgument(
fieldContext == null || fieldContext.getAnnotation(JsonString.class) == null,
"number type formatted as a JSON number cannot use @JsonString annotation");
if (valueClass == null || valueClass.isAssignableFrom(BigDecimal.class)) {
return getDecimalValue();
}
if (valueClass == BigInteger.class) {
return getBigIntegerValue();
}
if (valueClass == Double.class || valueClass == double.class) {
return getDoubleValue();
}
if (valueClass == Long.class || valueClass == long.class) {
return getLongValue();
}
if (valueClass == Float.class || valueClass == float.class) {
return getFloatValue();
}
if (valueClass == Integer.class || valueClass == int.class) {
return getIntValue();
}
if (valueClass == Short.class || valueClass == short.class) {
return getShortValue();
}
if (valueClass == Byte.class || valueClass == byte.class) {
return getByteValue();
}
throw new IllegalArgumentException("expected numeric type but got " + valueType);
case VALUE_STRING:
// TODO(user): Maybe refactor this method in multiple mini-methods for readability?
String text = getText().trim().toLowerCase(Locale.US);
// If we are expecting a Float / Double and the Text is NaN (case insensitive)
// Then: Accept, even if the Annotation is JsonString.
// Otherwise: Check that the Annotation is not JsonString.
if (!(((valueClass == float.class || valueClass == Float.class)
|| (valueClass == double.class || valueClass == Double.class))
&& (text.equals("nan") || text.equals("infinity") || text.equals("-infinity")))) {
Preconditions.checkArgument(
valueClass == null
|| !Number.class.isAssignableFrom(valueClass)
|| fieldContext != null && fieldContext.getAnnotation(JsonString.class) != null,
"number field formatted as a JSON string must use the @JsonString annotation");
}
return Data.parsePrimitiveValue(valueType, getText());
case VALUE_NULL:
Preconditions.checkArgument(
valueClass == null || !valueClass.isPrimitive(),
"primitive number field but found a JSON null");
if (valueClass != null
&& 0 != (valueClass.getModifiers() & (Modifier.ABSTRACT | Modifier.INTERFACE))) {
if (Types.isAssignableToOrFrom(valueClass, Collection.class)) {
return Data.nullOf(Data.newCollectionInstance(valueType).getClass());
}
if (Types.isAssignableToOrFrom(valueClass, Map.class)) {
return Data.nullOf(Data.newMapInstance(valueClass).getClass());
}
}
return Data.nullOf(Types.getRawArrayComponentType(context, valueType));
default:
throw new IllegalArgumentException("unexpected JSON node type: " + token);
}
} catch (IllegalArgumentException e) {
// build context string
StringBuilder contextStringBuilder = new StringBuilder();
String currentName = getCurrentName();
if (currentName != null) {
contextStringBuilder.append("key ").append(currentName);
}
if (fieldContext != null) {
if (currentName != null) {
contextStringBuilder.append(", ");
}
contextStringBuilder.append("field ").append(fieldContext);
}
throw new IllegalArgumentException(contextStringBuilder.toString(), e);
}
}
/**
* Finds the {@link Field} on the given {@link Class} that has the {@link JsonPolymorphicTypeMap}
* annotation, or {@code null} if there is none.
*
* <p>The class must contain exactly zero or one {@link JsonPolymorphicTypeMap} annotation.
*
* @param key The {@link Class} to search in, or {@code null}
* @return The {@link Field} with the {@link JsonPolymorphicTypeMap} annotation, or {@code null}
* either if there is none or if the key is {@code null}
*/
private static Field getCachedTypemapFieldFor(Class<?> key) {
if (key == null) {
return null;
}
lock.lock();
try {
// Must use containsKey because we do store null values for when the class has no
// JsonPolymorphicTypeMap field.
if (cachedTypemapFields.containsKey(key)) {
return cachedTypemapFields.get(key);
}
// Find the field that determines the type and cache it.
Field value = null;
Collection<FieldInfo> fieldInfos = ClassInfo.of(key).getFieldInfos();
for (FieldInfo fieldInfo : fieldInfos) {
Field field = fieldInfo.getField();
JsonPolymorphicTypeMap typemapAnnotation =
field.getAnnotation(JsonPolymorphicTypeMap.class);
if (typemapAnnotation != null) {
Preconditions.checkArgument(
value == null,
"Class contains more than one field with @JsonPolymorphicTypeMap annotation: %s",
key);
Preconditions.checkArgument(
Data.isPrimitive(field.getType()),
"Field which has the @JsonPolymorphicTypeMap, %s, is not a supported type: %s",
key,
field.getType());
value = field;
// Check for duplicate typeDef keys
TypeDef[] typeDefs = typemapAnnotation.typeDefinitions();
HashSet<String> typeDefKeys = Sets.newHashSet();
Preconditions.checkArgument(
typeDefs.length > 0, "@JsonPolymorphicTypeMap must have at least one @TypeDef");
for (TypeDef typeDef : typeDefs) {
Preconditions.checkArgument(
typeDefKeys.add(typeDef.key()),
"Class contains two @TypeDef annotations with identical key: %s",
typeDef.key());
}
}
}
cachedTypemapFields.put(key, value);
return value;
} finally {
lock.unlock();
}
}
}