-
Notifications
You must be signed in to change notification settings - Fork 273
/
Gsons.generator
994 lines (937 loc) · 31.4 KB
/
Gsons.generator
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
[--
Copyright 2014-2016 Immutables Authors and Contributors
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.
--]
[template public generate]
[for adapters in typeAdapters,
packageName = adapters.packageGenerated,
simpleName = adapters.definedBy.simpleName,
String capitalizedSimpleName = toUpper simpleName,
String typeAdaptersName = ('GsonAdapters' capitalizedSimpleName),
String serviceInterface = (gson '.TypeAdapterFactory')]
[output.java packageName typeAdaptersName]
[for d = adapters.definedBy,
s in d.style if s.headerComments,
t in d.asType]
[t.associatedTopLevel.headerComments]
[/for]
[generateTypeAdapters packageName simpleName typeAdaptersName adapters.types]
[/output.java]
[if adapters.mirror.metainfService]
[output.service serviceInterface]
[if packageName][packageName].[/if][typeAdaptersName]
[/output.service]
[/if]
[/for]
[/template]
[template generateTypeAdapters String packageName String simpleName String typeAdaptersName Type... allValues]
[if packageName]
package [packageName];
[/if]
[-- replace with fully qualified names where needed --]
import [gson].*;
import [gson].stream.*;
import [gson].reflect.*;
import java.io.IOException;
import java.lang.String;
import java.lang.Object;
[for t in allValues]
[for starImport in t.requiredSourceStarImports]
import [starImport];
[/for]
[/for]
/**
* A {@code TypeAdapterFactory} that handles all of the immutable types generated under {@code [simpleName]}.
[for v in allValues]
* @see [v.typeValue.relativeRaw]
[/for]
*/
@SuppressWarnings({"all", "MethodCanBeStatic"})[-- reliably check for generateSuppressAllWarnings --]
[for v in allValues][if for.first][-- this is only needed to get to first available value's style --]
[if v allowsClasspathAnnotation 'javax.annotation.ParametersAreNonnullByDefault']
@javax.annotation.ParametersAreNonnullByDefault
[/if]
[if v allowsClasspathAnnotation 'javax.annotation.processing.Generated']
@javax.annotation.processing.Generated({"Gsons.generator", "[packageName]"})
[else if v allowsClasspathAnnotation 'javax.annotation.Generated']
@javax.annotation.Generated({"Gsons.generator", "[packageName]"})
[/if]
[/if][/for]
public final class [typeAdaptersName] implements TypeAdapterFactory {
@SuppressWarnings({"unchecked", "raw"}) // safe unchecked, types are verified in runtime
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
[for v in allValues]
if ([v.name]TypeAdapter.adapts(type)) {
[if v.generics]
return (TypeAdapter<T>) new [v.name]TypeAdapter(type, gson);
[else]
return (TypeAdapter<T>) new [v.name]TypeAdapter(gson);
[/if]
}
[/for]
return null;
}
@Override
public String toString() {
return "[typeAdaptersName]([for v in allValues][if not for.first], [/if][v.name][/for])";
}
[for v in allValues]
[generateTypeAdapter v]
[/for]
[for v in allValues if v.generics]
[if for.first]
private static java.lang.reflect.Type['[]'] getTypeArguments(TypeToken<?> type) {
if (type.getType() instanceof java.lang.reflect.ParameterizedType) {
return ((java.lang.reflect.ParameterizedType) type.getType()).getActualTypeArguments();
}
throw new IllegalStateException("Please supply Type with actual type parameters to serialize "
+ type.getType() + " instance using method overloads like toJson(instance, type)."
+ " Runtime raw type alone is not enough."
+ " You can use TypeToken class or reflection to construct Type with type arguments");
}
[/if]
[/for]
[for v in allValues if v.gsonTypeAdapters.fieldNamingStrategy andnot v.useConstructorOnly]
[if for.first]
private static String translateName(Gson gson, Class<?> sampleClass, String fieldName) {
try {
return gson.fieldNamingStrategy().translateName(sampleClass.getField(fieldName));
} catch (NoSuchFieldException noSuchField) {
throw new AssertionError(noSuchField);
}
}
[/if]
[/for]
}
[/template]
[template generateTypeAdapter Type type]
[checkAttributes type]
[for allAttributes = type.allMarshalingAttributes, t = type.typeAbstract, im = type.typeImmutable]
@SuppressWarnings({"unchecked", "raw"}) // safe unchecked, types are verified in runtime
private static class [type.name]TypeAdapter[type.generics] extends TypeAdapter<[t]> {
[for a in allAttributes]
[if a.requiresMarshalingAdapter and a.primitiveArrayType][-- no sample --]
[else if a.requiresMarshalingAdapter and a.wholeTypeVariable.not]
public final [a.elementType] [a.name]TypeSample = null;
[/if]
[if a.requiresMarshalingSecondaryAdapter and a.secondaryWholeTypeVariable.not]
public final [a.secondaryElementType] [a.name]SecondaryTypeSample = null;
[/if]
[/for]
[for other = type.gsonOther][if other]
private final TypeAdapter<JsonElement> [other.name]TypeAdapter;
[/if][/for]
[for a in allAttributes]
[if a.requiresMarshalingAdapter and a.primitiveArrayType]
private final TypeAdapter<[a.type]> [a.name]TypeAdapter;
[else if a.requiresMarshalingAdapter]
private final TypeAdapter<[a.elementType]> [a.name]TypeAdapter;
[/if]
[if a.requiresMarshalingSecondaryAdapter]
private final TypeAdapter<[a.secondaryElementType]> [a.name]SecondaryTypeAdapter;
[/if]
[/for]
[if type.gsonTypeAdapters.fieldNamingStrategy andnot type.useConstructorOnly]
[for a in allAttributes]
final String [a.name]Name;
[/for]
[/if]
[if type.gsonTypeAdapters.fieldNamingStrategy andnot type.useConstructorOnly]
[if allAttributes]
static class [type.name]NamingFields[type.generics] {
[for a in allAttributes]
public [a.type] [a.name];
[/for]
}
[/if]
[/if]
[type.name]TypeAdapter([if type.generics]TypeToken<?> type, [/if]Gson gson) {[if not allAttributes]}[/if]
[if type.generics]
java.lang.reflect.Type['[]'] typeArguments = getTypeArguments(type);
[/if]
[for other = type.gsonOther][if other]
this.[other.name]TypeAdapter = gson.getAdapter(JsonElement.class);
[/if][/for]
[for a in allAttributes]
[if a.requiresMarshalingAdapter]
[getAdapterFromGson a false]
[/if]
[if a.requiresMarshalingSecondaryAdapter]
[getAdapterFromGson a true]
[/if]
[/for]
[if type.gsonTypeAdapters.fieldNamingStrategy andnot type.useConstructorOnly]
[for a in allAttributes]
[if a.serializedName]
this.[a.name]Name = "[a.serializedName]";
[else]
this.[a.name]Name = translateName(gson, [type.name]NamingFields.class, "[a.name]");
[/if]
[/for]
[/if]
[if allAttributes]} [/if]
static boolean adapts(TypeToken<?> type) {
return [t.relativeRaw].class == type.getRawType()
|| [im.relativeRaw].class == type.getRawType();
}
@Override
public void write(JsonWriter out, [t] value) throws IOException {
if (value == null) {
out.nullValue();
} else {
write[type.name](out, value);
}
}
@Override
public [t] read(JsonReader in) throws IOException {
return read[type.name](in);
}
[generateMarshalingMethods type]
[generateUnmarshalingMethods type]
}
[/for]
[/template]
[template getAdapterFromGson Attribute a Boolean secondary]
[for subs = a.expectedSubtypes,
Boolean supportedSubs = ((a.typeKind.mapKind and secondary) ornot a.typeKind.mapKind),
Boolean reallySupportsSubs = supportedSubs andnot a.hasTypeVariables]
[if subs and reallySupportsSubs]
this.[a.name][if secondary]Secondary[/if]TypeAdapter = org.immutables.gson.adapter.ExpectedSubtypesAdapter.create(gson,
[if secondary][a.typeTokenOfSecondaryElement][else][a.typeTokenOfElement][/if][for s in subs],
TypeToken.get([s].class)[/for]);
[else]
[if secondary]
this.[a.name]SecondaryTypeAdapter = [output.linesShortable]gson.getAdapter(
[a.typeTokenOfSecondaryElement]);[/output.linesShortable]
[else if a.primitiveArrayType]
this.[a.name]TypeAdapter = gson.getAdapter([a.type].class);
[else]
this.[a.name]TypeAdapter = [output.linesShortable]gson.getAdapter(
[a.typeTokenOfElement]);[/output.linesShortable]
[/if]
[/if]
[/for]
[/template]
[template generateMarshalingMethods Type type]
private void write[type.name](JsonWriter out, [type.typeAbstract] instance)
throws IOException {
[if type.useConstructorOnly]
[for ca = type.constructorArguments]
[if singular ca]
[for a in ca]
[generateMarshalConstructorValue type a]
[/for]
[else]
out.beginArray();
[for a in ca]
[generateMarshalConstructorValue type a]
[/for]
out.endArray();
[/if]
[/for]
[else]
out.beginObject();
[for a in type.marshaledAttributes]
[generateMarshalAttributeValue type a]
[/for]
[generateMarshalOther type]
out.endObject();
[/if]
}
[/template]
[template generateMarshalOther Type type]
[for other = type.gsonOther][if other]
for (java.util.Map.Entry<String, JsonElement> other : instance.[other.names.get]().entrySet()) {
out.name(other.getKey());
[other.name]TypeAdapter.write(out, other.getValue());
}
[/if][/for]
[/template]
[template generateMarshalConstructorValue Type type Attribute a]
[if a.optionalType]
[a.type] [a.name]Optional = instance.[a.names.get]();
if ([a.name]Optional.[optionalPresent a]) {
[a.unwrappedElementType] [a.name]Value = [a.name]Optional.[optionalGet a];
[generateWriteAttributeValue type a (a.name 'Value') false]
} else {
out.nullValue();
}
[else if a.mapType]
[a.atNullabilityLocal][a.type] [a.name]Mapping = instance.[a.names.get]();
[if a.nullable]
if ([a.name]Mapping != null) {
[generateMarshalMapping type a]
} else {
out.nullValue();
}
[else]
[generateMarshalMapping type a]
[/if]
[else if a.collectionType or a.referenceArrayType]
[if a.nullable]
if (instance.[a.names.get]() == null) {
out.nullValue();
} else {
out.beginArray();
for ([a.unwrappedElementType] e : instance.[a.names.get]()) {
[generateWriteAttributeValue type a 'e' false]
}
out.endArray();
}
[else]
out.beginArray();
for ([a.unwrappedElementType] e : instance.[a.names.get]()) {
[generateWriteAttributeValue type a 'e' false]
}
out.endArray();
[/if]
[else]
[if a.nullable]
[a.atNullabilityLocal][a.type] [a.name]Value = instance.[a.names.get]();
if ([a.name]Value != null) {
[generateWriteAttributeValue type a (a.name 'Value') false]
} else {
out.nullValue();
}
[else]
[generateWriteAttributeValue type a ('instance.' a.names.get '()') false]
[/if]
[/if]
[/template]
[template generateMarshalMapping Type type Attribute a]
out.beginObject();
[if a.typeKind.multimapKind]
for (java.util.Map.Entry<[a.wrappedElementType], java.util.Collection<[a.wrappedSecondaryElementType]>> e : [a.name]Mapping.asMap().entrySet()) {
[generateMarshalKey a]e.getKey()[/generateMarshalKey]
out.name(key);
out.beginArray();
for ([a.unwrappedSecondaryElementType] value : e.getValue()) {
[generateWriteAttributeValue type a 'value' true]
}
out.endArray();
}
[else]
for (java.util.Map.Entry<[a.wrappedElementType], [a.wrappedSecondaryElementType]> e : [a.name]Mapping.entrySet()) {
[-- only use toString with objects and boxed primitives]
[generateMarshalKey a]e.getKey()[/generateMarshalKey]
out.name(key);
[-- here we assign to a unwrapped variable so overload resolution will precise]
[a.unwrappedSecondaryElementType] value = e.getValue();
[generateWriteAttributeValue type a 'value' true]
}
[/if]
out.endObject();
[/template]
[template generateMarshalKey Attribute a String expression]
[if a.unwrappedElementPrimitiveType]
String key = String.valueOf([expression]);
[else if a.elementType eq 'java.lang.String']
String key = [expression];
[else][if not a.requiresMarshalingAdapter][output.error]assertion: key write to string with type adapter for attribute: [a.type] [a.name][/output.error][/if]
String key = [a.name]TypeAdapter.toJsonTree([expression]).getAsString();
[/if]
[/template]
[template generateMarshalAttributeValue Type type Attribute a]
[if a.optionalType]
[a.type] [a.name]Optional = instance.[a.names.get]();
if ([a.name]Optional.[optionalPresent a]) {
out.name([serializedName a]);
[a.unwrappedElementType] [a.name]Value = [a.name]Optional.[optionalGet a];
[generateWriteAttributeValue type a (a.name 'Value') false]
} else if (out.getSerializeNulls()) {
out.name([serializedName a]);
out.nullValue();
}
[else if a.mapType]
[a.atNullabilityLocal][a.type] [a.name]Mapping = instance.[a.names.get]();
[if type.gsonTypeAdapters.emptyAsNulls]
if ([if a.nullable][a.name]Mapping != null && [/if]![a.name]Mapping.isEmpty()) {
out.name([serializedName a]);
[generateMarshalMapping type a]
} else if (out.getSerializeNulls()) {
out.name([serializedName a]);
[if a.nullable]
if ([a.name]Mapping == null) {
out.nullValue();
} else {
out.beginObject();
out.endObject();
}
[else]
out.beginObject();
out.endObject();
[/if]
}
[else if a.nullable]
if ([a.name]Mapping != null) {
out.name([serializedName a]);
[generateMarshalMapping type a]
} else if (out.getSerializeNulls()) {
out.name([serializedName a]);
out.nullValue();
}
[else]
out.name([serializedName a]);
[generateMarshalMapping type a]
[/if]
[else if a.collectionType or a.referenceArrayType]
[a.atNullabilityLocal][a.type] [a.name]Elements = instance.[a.names.get]();
[if type.gsonTypeAdapters.emptyAsNulls]
if ([if a.nullable][a.name]Elements != null && [/if][if a.arrayType][a.name]Elements.length != 0[else]![a.name]Elements.isEmpty()[/if]) {
out.name([serializedName a]);
out.beginArray();
for ([a.unwrappedElementType] e : [a.name]Elements) {
[generateWriteAttributeValue type a 'e' false]
}
out.endArray();
} else if (out.getSerializeNulls()) {
out.name([serializedName a]);
[if a.nullable]
if ([a.name]Elements == null) {
out.nullValue();
} else {
out.beginArray();
out.endArray();
}
[else]
out.beginArray();
out.endArray();
[/if]
}
[else if a.nullable]
if ([a.name]Elements != null) {
out.name([serializedName a]);
out.beginArray();
for ([a.unwrappedElementType] e : [a.name]Elements) {
[generateWriteAttributeValue type a 'e' false]
}
out.endArray();
} else if (out.getSerializeNulls()) {
out.name([serializedName a]);
out.nullValue();
}
[else]
out.name([serializedName a]);
out.beginArray();
for ([a.unwrappedElementType] e : [a.name]Elements) {
[generateWriteAttributeValue type a 'e' false]
}
out.endArray();
[/if]
[else if a.nullable]
[a.atNullabilityLocal][a.type] [a.name]Value = instance.[a.names.get]();
if ([a.name]Value != null) {
out.name([serializedName a]);
[generateWriteAttributeValue type a (a.name 'Value') false]
} else if (out.getSerializeNulls()) {
out.name([serializedName a]);
out.nullValue();
}
[else]
out.name([serializedName a]);
[generateWriteAttributeValue type a ('instance.' a.names.get '()') false]
[/if]
[/template]
[template generateUnmarshalingMethods Type type]
[for charAttributes = a for
a in type.unmarshaledAttributes
if a.unwrappedElementType eq 'char']
[if charAttributes]
private static char charValueOf(String string) {
if (string.length() == 1) {
return string.charAt(0);
}
throw new IllegalArgumentException(
String.format("Expected string with a single character but was: '%s'", string));
}
[/if]
[/for]
[if type.useSingletonOnly]
private [type.typeAbstract] read[type.name](JsonReader in)
throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
in.skipValue();
return [type.factoryInstance]();
}
[else if type.useConstructorOnly]
[for ca = type.constructorArguments]
private [if singular ca][for a in ca][if a.primitive]static[/if][/for][/if] [type.typeAbstract] read[type.name](JsonReader in)
throws IOException {
[if singular ca]
[for a in ca]
return [type.factoryOf](readParameter[toUpper a.name](in));
[/for]
[else]
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
in.beginArray();
[type.typeAbstract] instance = [type.factoryOf]([for a in ca][if not for.first],[/if]
readParameter[toUpper a.name](in)[/for]);
in.endArray();
return instance;
[/if]
}
[for a in ca]
[generateConstructorArgumentUnmarshal type a]
[/for]
[/for]
[else]
private [if type.generics.empty][if not type.unmarshaledAttributes]static[/if][/if] [type.typeAbstract] read[type.name](JsonReader in)
throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
[type.typeBuilder] builder = [castBuildStagedBuilder type][type.factoryBuilder]()[/castBuildStagedBuilder];
[if type.unmarshaledAttributes]
[for other = type.gsonOther]
[if other]
JsonObject other = new JsonObject();
[/if]
in.beginObject();
while (in.hasNext()) {
eachAttribute(in, builder[if other], other[/if]);
}
in.endObject();
[if other]
builder.[other.names.init](other);
[/if]
[/for]
[else]
in.skipValue();
[/if]
return builder.[type.names.build]();
}
[if type.unmarshaledAttributes]
private void eachAttribute(JsonReader in, [type.typeBuilder] builder[if type.gsonOther], JsonObject other[/if])
throws IOException {
String attributeName = in.nextName();
[if type.gsonTypeAdapters.fieldNamingStrategy]
[for a in type.unmarshaledAttributes]
if ([serializedName a].equals(attributeName)) {
readIn[toUpper a.name](in, builder);
return;
}
[for alternateName in a.alternateSerializedNames]
if ("[alternateName]".equals(attributeName)) {
readIn[toUpper a.name](in, builder);
return;
}
[/for]
[/for]
[else]
switch (attributeName.charAt(0)) {
[for Mm mm = byFirstCharacter type.unmarshaledAttributes,
e in mm.asMap.entrySet]
case '[e.key]':
[for nm in e.value, name = nm.key, a = nm.value]
if ("[name]".equals(attributeName)) {
readIn[toUpper a.name](in, builder);
return;
}
[/for]
break;
[/for]
default:
}
[/if]
[for other = type.gsonOther]
[if other]
other.add(attributeName, [other.name]TypeAdapter.read(in));
[else]
in.skipValue();
[/if]
[/for]
}
[/if]
[for s in type.unmarshaledAttributes]
[generateUnmarshalAttribute type s]
[/for]
[/if]
[/template]
[template generateConstructorArgumentUnmarshal Type type Attribute a]
private [if a.primitive]static[/if] [a.atNullability][constructorAcceptType a] readParameter[toUpper a.name](JsonReader in)
throws IOException {
[if a.primitive]
return [simpleTypeNext a.type];
[else]
[generateConstructorArgumentComplexUnmarshal type a]
[/if]
}
[/template]
[template generateConstructorArgumentComplexUnmarshal Type type Attribute a]
[if a.optionalType]
JsonToken t = in.peek();
if (t == JsonToken.NULL) {
in.nextNull();
return [optionalEmpty a];
}
[if a.jdkSpecializedOptional]
return [a.rawType].of([simpleTypeNext a.unwrappedElementType]);
[else if a.unwrappedElementPrimitiveType or (a.unwrappedElementType eq 'java.lang.String')]
return [a.rawType].<[a.elementType]>of([simpleTypeNext a.unwrappedElementType]);
[else]
[generateReadAttributeValue type a a.wrappedElementType false]
return [a.rawType].of(value);
[/if]
[else if a.mapType]
[if a.nullable]
JsonToken t = in.peek();
if (t == JsonToken.NULL) {
in.nextNull();
return null;
}
[/if]
[createBuilderForCollection type a 'mappings']
[generateMappingUnmarshalCode type a 'mappings.put']
[returnBuiltCollection type a 'mappings']
[else if a.collectionType]
[if a.nullable]
JsonToken t = in.peek();
if (t == JsonToken.NULL) {
in.nextNull();
return null;
}
[/if]
[createBuilderForCollection type a 'elements']
[generateCollectionUnmarshalCode type a 'elements.add']
[returnBuiltCollection type a 'elements']
[else if a.referenceArrayType]
[if a.nullable]
JsonToken t = in.peek();
if (t == JsonToken.NULL) {
in.nextNull();
return null;
}
[/if]
java.util.List<[a.elementType]> elements = new java.util.ArrayList<[a.elementType]>();
[generateCollectionUnmarshalCode type a ('elements.add')]
return elements.toArray(new [a.elementType]['[']elements.size()[']']);
[else]
[if a.nullable]
JsonToken t = in.peek();
if (t == JsonToken.NULL) {
in.nextNull();
return null;
}
[/if]
[generateReadAttributeValue type a a.type false]
return value;
[/if]
[/template]
[template generateUnmarshalAttribute Type type Attribute a]
private void readIn[toUpper a.name](JsonReader in, [type.typeBuilder] builder)
throws IOException {
[if type.gsonTypeAdapters.nullAsDefault]
[if not a.nullable]
[if not a.mandatory]
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return;
}
[/if]
[/if]
[/if]
[if a.primitive]
builder.[a.names.init]([simpleTypeNext a.type]);
[else]
[generateComplexTypeUnmarshalAttribute type a]
[/if]
}
[/template]
[template createBuilderForCollection Type type Attribute a String variableName]
[if a.mapType]
[if type.generateJdkOnly or a.typeKind.isSortedKind]
java.util.Map[a.genericArgs] [variableName] = new java.util.LinkedHashMap[a.genericArgs]();
[else]
[guava].collect.Immutable[a.rawMapType].Builder[a.genericArgs] [variableName] = [guava].collect.Immutable[a.rawMapType].builder();
[/if]
[else if a.collectionType]
[if a.customCollectionType]
[a.rawType].Builder[a.genericArgs] [variableName] = [a.rawType].builder();
[else if type.generateJdkOnly or a.typeKind.isSortedKind]
java.util.List[a.genericArgs] [variableName] = new java.util.ArrayList[a.genericArgs]();
[else]
[guava].collect.Immutable[a.rawCollectionType].Builder[a.genericArgs] [variableName] = [guava].collect.Immutable[a.rawCollectionType].builder();
[/if]
[/if]
[/template]
[template returnBuiltCollection Type type Attribute a String variableName]
[if type.generateJdkOnly or a.typeKind.isSortedKind]
return [variableName];
[else]
return [variableName].build();
[/if]
[/template]
[template collectionUnmarshalCaseNullSingleArray Attribute a String emptyInitExpression Invokable unmarshalCollectionElement]
if (in.peek() == JsonToken.BEGIN_ARRAY) {
in.beginArray();
while(in.hasNext()) {
[unmarshalCollectionElement]
}
in.endArray();
} else if (in.peek() == JsonToken.NULL) {
in.nextNull();
} else {
[unmarshalCollectionElement]
}
[/template]
[template generateMappingUnmarshalCode Type type Attribute a String collectorExpression][generateMappingUnmarshalCodeAlt type a collectorExpression ''][/template]
[template generateMappingUnmarshalCodeAlt Type type Attribute a String collectorExpression String emptyInitExpression]
[for Boolean emptyInitHack = emptyInitExpression and (a.nullable or a.generateDefault)]
if (in.peek() == JsonToken.NULL) {
in.nextNull();
} else {
in.beginObject();
[if emptyInitHack]boolean empty = true;[/if]
while(in.hasNext()) {
String rawKey = in.nextName();
[if a.unwrappedElementPrimitiveType]
[if a.unwrappedElementType eq 'char']
char key = charValueOf(rawKey);
[else]
[a.unwrappedElementType] key = [a.wrappedElementType].parse[toUpper a.unwrappedElementType](rawKey);
[/if]
[else if a.wrappedElementType eq 'java.lang.String']
[a.wrappedElementType] key = rawKey;
[else][if not a.requiresMarshalingAdapter][output.error]assertion: key read from string with type adapter for attribute: [a.type] [a.name][/output.error][/if]
[a.wrappedElementType] key = [a.name]TypeAdapter.fromJsonTree(new JsonPrimitive(rawKey));
[/if]
[if a.typeKind.isMultimapKind]
if (in.peek() == JsonToken.BEGIN_ARRAY) {
in.beginArray();
while(in.hasNext()) {
[generateReadAttributeValue type a a.wrappedSecondaryElementType true]
[collectorExpression](key, value);
[if emptyInitHack]empty = false;[/if]
}
in.endArray();
} else {
[generateReadAttributeValue type a a.wrappedSecondaryElementType true]
[collectorExpression](key, value);
[if emptyInitHack]empty = false;[/if]
}
[else]
[generateReadAttributeValue type a a.wrappedSecondaryElementType true]
[collectorExpression](key, value);
[if emptyInitHack]empty = false;[/if]
[/if]
}
[if emptyInitHack][-- We are empty but there was an beginArray, so we need to set an empty mapping --]
if (empty) {
[emptyInitExpression]([if a.typeKind.isMultimapKind][guava].collect.ImmutableMultimap.[a.genericArgs]of()[else]java.util.Collections.[a.genericArgs]emptyMap()[/if]);
}
[/if]
in.endObject();
}
[/for]
[/template]
[template generateCollectionUnmarshalCode Type type Attribute a String collectorExpression][generateCollectionUnmarshalCodeAlt type a collectorExpression ''][/template]
[template generateCollectionUnmarshalCodeAlt Type type Attribute a String collectorExpression String emptyInitExpression]
[for Boolean emptyInitHack = emptyInitExpression and (a.nullable or a.generateDefault)]
[if emptyInitHack]boolean empty = true;[/if]
[collectionUnmarshalCaseNullSingleArray a emptyInitExpression]
[if a.unwrappedElementPrimitiveType or (a.unwrappedElementType eq 'java.lang.String')]
[collectorExpression]([simpleTypeNext a.unwrappedElementType]);
[if emptyInitHack]empty = false;[/if]
[else]
[generateReadAttributeValue type a a.wrappedElementType false]
[collectorExpression](value);
[if emptyInitHack]empty = false;[/if]
[/if]
[/collectionUnmarshalCaseNullSingleArray]
[if emptyInitHack]
if (empty) {
[emptyInitExpression](java.util.Collections.[a.genericArgs]emptyList());
}
[/if]
[/for]
[/template]
[template generateComplexTypeUnmarshalAttribute Type type Attribute a]
[if a.optionalType]
if (in.peek() == JsonToken.NULL) {
in.nextNull();
} else {
[if a.unwrappedElementPrimitiveType or (a.unwrappedElementType eq 'java.lang.String')]
builder.[a.names.init]([simpleTypeNext a.unwrappedElementType]);
[else]
[generateReadAttributeValue type a a.wrappedElementType false]
builder.[a.names.init](value);
[/if]
}
[else if a.mapType]
[if a.nullable]
if (in.peek() == JsonToken.NULL) {
in.nextNull();
[if a.generateDefault andnot a.containingType.useStrictBuilder]
builder.[a.names.init](null);
[/if]
} else {
[generateMappingUnmarshalCodeAlt type a ('builder.' a.names.put) ('builder.' a.names.putAll)]
}
[else if a.generateDefault]
[generateMappingUnmarshalCodeAlt type a ('builder.' a.names.put) ('builder.' a.names.putAll)]
[else]
[generateMappingUnmarshalCode type a ('builder.' a.names.put)]
[/if]
[else if a.collectionType]
[if a.nullable]
if (in.peek() == JsonToken.NULL) {
in.nextNull();
[if a.generateDefault andnot a.containingType.useStrictBuilder]
builder.[a.names.init](null);
[/if]
} else {
[generateCollectionUnmarshalCodeAlt type a ('builder.' a.names.add) ('builder.' a.names.addAll)]
}
[else if a.generateDefault]
[generateCollectionUnmarshalCodeAlt type a ('builder.' a.names.add) ('builder.' a.names.addAll)]
[else]
[generateCollectionUnmarshalCode type a ('builder.' a.names.add)]
[/if]
[else if a.referenceArrayType]
[if a.nullable]
if (in.peek() == JsonToken.NULL) {
in.nextNull();
[if a.generateDefault andnot a.containingType.useStrictBuilder]
builder.[a.names.init](null);
[/if]
} else {
java.util.List<[a.elementType]> elements = new java.util.ArrayList<[a.elementType]>();
[generateCollectionUnmarshalCode type a ('elements.add')]
builder.[a.names.init](elements.toArray(new [a.elementType]['[']elements.size()[']']));
}
[else]
java.util.List<[a.elementType]> elements = new java.util.ArrayList<[a.elementType]>();
[generateCollectionUnmarshalCode type a ('elements.add')]
builder.[a.names.init](elements.toArray(new [a.elementType]['[']elements.size()[']']));
[/if]
[else if a.stringType]
[if a.nullable]
if (in.peek() == JsonToken.NULL) {
in.nextNull();
[if a.generateDefault]
builder.[a.names.init](null);
[/if]
} else {
builder.[a.names.init]([simpleTypeNext a.rawType]);
}
[else]
builder.[a.names.init]([simpleTypeNext a.rawType]);
[/if]
[else]
[if a.nullable]
if (in.peek() == JsonToken.NULL) {
in.nextNull();
[if a.generateDefault]
builder.[a.names.init](null);
[/if]
} else {
[generateReadAttributeValue type a a.type false]
builder.[a.names.init](value);
}
[else]
[generateReadAttributeValue type a a.type false]
builder.[a.names.init](value);
[/if]
[/if]
[/template]
[template generateReadAttributeValue Type type Attribute attribute String genericType Boolean secondary]
[if secondary]
[if attribute.requiresMarshalingSecondaryAdapter]
[genericType] value = [attribute.name]SecondaryTypeAdapter.read(in);
[else]
[attribute.unwrappedSecondaryElementType] value = [simpleTypeNext attribute.unwrappedSecondaryElementType];
[/if]
[else]
[if attribute.requiresMarshalingAdapter]
[genericType] value = [attribute.name]TypeAdapter.read(in);
[else]
[attribute.unwrappedElementType] value = [simpleTypeNext attribute.unwrappedElementType];
[/if]
[/if]
[/template]
[template generateWriteAttributeValue Type type Attribute attribute String variableName Boolean secondary]
[if secondary]
[if attribute.requiresMarshalingSecondaryAdapter]
[attribute.name]SecondaryTypeAdapter.write(out, [variableName]);
[else]
[simpleTypeWrite attribute.secondaryElementType variableName]
[/if]
[else]
[if attribute.requiresMarshalingAdapter]
[attribute.name]TypeAdapter.write(out, [variableName]);
[else]
[simpleTypeWrite attribute.unwrappedElementType variableName]
[/if]
[/if]
[/template]
[template simpleTypeWrite String typeName String variableName]
[if typeName eq 'char']
out.value(String.valueOf([variableName]));
[else]
out.value([variableName]);
[/if]
[/template]
[template simpleTypeNext String typeName][output.trim]
[if typeName eq 'boolean']
in.nextBoolean()
[else if typeName eq 'byte']
(byte) in.nextInt()
[else if typeName eq 'short']
(short) in.nextInt()
[else if typeName eq 'int']
in.nextInt()
[else if typeName eq 'long']
in.nextLong()
[else if typeName eq 'float']
(float) in.nextDouble()
[else if typeName eq 'double']
in.nextDouble()
[else if typeName eq 'java.lang.String']
in.nextString()
[else if typeName eq 'char']
charValueOf(in.nextString())
[else]
null
[output.error]unexpected type [typeName], not a simple one[/output.error]
[/if]
[/output.trim][/template]
[template constructorAcceptType Attribute a][output.trim]
[if a.typeKind.isMultimapKind]
[guava].collect.Multimap<[a.elementType], [a.secondaryElementType]>
[else if a.typeKind.isMapKind]
java.util.Map<[a.elementType], [a.secondaryElementType]>
[else if a.typeKind.isCollectionKind]
Iterable<[a.elementType]>
[else]
[a.type]
[/if]
[/output.trim][/template]
[template checkAttributes Type type]
[for a in type.implementedAttributes if a.jsonIgnore and a.mandatory]
[-- mandatory constructor arguments are not checked, should they? --]
[output.error]
@Gson.Ignore could not be used on mandatory attribute '[type.typeAbstract.relative].[a.name]'
[/output.error]
[/for]
[/template]
[template castBuildStagedBuilder Type type String expression][for tb = type.telescopicBuild][if tb](([type.typeBuilderImpl.relative]) [expression])[else][expression][/if][/for][/template]
[template serializedName Attribute a][if a.containingType.gsonTypeAdapters.fieldNamingStrategy][a.name]Name[else]"[a.marshaledName]"[/if][/template]
[template optionalGet Attribute a][if a.jdkSpecializedOptional]getAs[toUpper a.elementType][else]get[/if]()[/template]
[template optionalEmpty Attribute a][a.rawType].[if a.jdkOptional]empty[else if a.fugueOptional or a.javaslangOptional]none[else]absent[/if]()[/template]
[template optionalPresent Attribute a][if a.fugueOptional or a.javaslangOptional]isDefined[else]isPresent[/if]()[/template]