@@ -49,6 +49,7 @@ public class VPack {
49
49
private static final String ATTR_KEY = "key" ;
50
50
private static final String ATTR_VALUE = "value" ;
51
51
private static final String DEFAULT_TYPE_KEY = "_class" ;
52
+ private static final boolean DEFAULT_USE_TYPE_HINTS = true ;
52
53
53
54
private final Map <Type , VPackSerializer <?>> serializers ;
54
55
private final Map <Type , VPackSerializer <?>> enclosingSerializers ;
@@ -65,6 +66,7 @@ public class VPack {
65
66
private final VPackDeserializationContext deserializationContext ;
66
67
private final boolean serializeNullValues ;
67
68
private final String typeKey ;
69
+ private final boolean useTypeHints ;
68
70
private final VPackBuilderUtils builderUtils ;
69
71
private final VPackCreatorMethodUtils vPackCreatorMethodUtils ;
70
72
@@ -83,6 +85,7 @@ public static class Builder implements VPackSetupContext<Builder> {
83
85
private final Map <Class <? extends Annotation >, VPackAnnotationFieldNaming <? extends Annotation >> annotationFieldNaming ;
84
86
private final Map <Type , VPackKeyMapAdapter <?>> keyMapAdapters ;
85
87
private String typeKey ;
88
+ private Boolean useTypeHints ;
86
89
87
90
public Builder () {
88
91
super ();
@@ -99,6 +102,7 @@ public Builder() {
99
102
annotationFieldNaming = new HashMap <>();
100
103
keyMapAdapters = new HashMap <>();
101
104
typeKey = null ;
105
+ useTypeHints = null ;
102
106
103
107
instanceCreators .put (Iterable .class , VPackInstanceCreators .ITERABLE );
104
108
instanceCreators .put (Collection .class , VPackInstanceCreators .COLLECTION );
@@ -319,19 +323,35 @@ public Builder registerKeyMapAdapter(final Type type, final VPackKeyMapAdapter<?
319
323
* @param typeKey Name of the field with type information
320
324
* @return {@link VPack.Builder}
321
325
*/
322
- public Builder typeKey (final String typeKey ) {
326
+ @ Override
327
+ public VPack .Builder typeKey (final String typeKey ) {
323
328
this .typeKey = typeKey ;
324
329
return this ;
325
330
}
326
331
332
+ /**
333
+ * Enables storing type information of the serialized object
334
+ *
335
+ * @param useTypeHints (default: {@code true})
336
+ * @return {@link VPack.Builder}
337
+ */
338
+ @ Override
339
+ public VPack .Builder useTypeHints (final boolean useTypeHints ) {
340
+ this .useTypeHints = useTypeHints ;
341
+ return this ;
342
+ }
343
+
327
344
public synchronized VPack build () {
328
345
return new VPack (new HashMap <>(serializers ), new HashMap <>(enclosingSerializers ), new HashMap <>(deserializers ),
329
346
new HashMap <>(deserializersWithSelfNullHandle ),
330
347
new HashMap <>(instanceCreators ), builderOptions , serializeNullValues ,
331
348
fieldNamingStrategy , new HashMap <>(deserializersByName ),
332
349
new HashMap <>(deserializersByNameWithSelfNullHandle ),
333
350
new HashMap <>(annotationFieldFilter ), new HashMap <>(annotationFieldNaming ),
334
- keyMapAdapters , typeKey != null ? typeKey : DEFAULT_TYPE_KEY );
351
+ keyMapAdapters ,
352
+ typeKey != null ? typeKey : DEFAULT_TYPE_KEY ,
353
+ useTypeHints != null ? useTypeHints : DEFAULT_USE_TYPE_HINTS
354
+ );
335
355
}
336
356
337
357
}
@@ -345,7 +365,7 @@ private VPack(final Map<Type, VPackSerializer<?>> serializers,
345
365
final Map <String , Map <Type , VPackDeserializer <?>>> deserializersByNameWithSelfNullHandle ,
346
366
final Map <Class <? extends Annotation >, VPackAnnotationFieldFilter <? extends Annotation >> annotationFieldFilter ,
347
367
final Map <Class <? extends Annotation >, VPackAnnotationFieldNaming <? extends Annotation >> annotationFieldNaming ,
348
- final Map <Type , VPackKeyMapAdapter <?>> keyMapAdapters , final String typeKey ) {
368
+ final Map <Type , VPackKeyMapAdapter <?>> keyMapAdapters , final String typeKey , final boolean useTypeHints ) {
349
369
super ();
350
370
this .serializers = serializers ;
351
371
this .enclosingSerializers = enclosingSerializers ;
@@ -358,6 +378,7 @@ private VPack(final Map<Type, VPackSerializer<?>> serializers,
358
378
this .deserializersByNameWithSelfNullHandle = deserializersByNameWithSelfNullHandle ;
359
379
this .keyMapAdapters = keyMapAdapters ;
360
380
this .typeKey = typeKey ;
381
+ this .useTypeHints = useTypeHints ;
361
382
362
383
builderUtils = new VPackBuilderUtils ();
363
384
vPackCreatorMethodUtils = new VPackCreatorMethodUtils ();
@@ -480,7 +501,7 @@ private <T> T deserializeObject(
480
501
}
481
502
482
503
private Type determineType (final VPackSlice vpack , final Type type ) {
483
- if (!vpack .isObject ()) {
504
+ if (!useTypeHints || ! vpack .isObject ()) {
484
505
return type ;
485
506
}
486
507
final VPackSlice clazz = vpack .get (typeKey );
@@ -933,6 +954,12 @@ private void addValue(
933
954
} else if (shouldAddTypeHint (type , value , fieldInfo )) {
934
955
addValue (name , value .getClass (), value , builder , fieldInfo ,
935
956
Collections .<String , Object >singletonMap (typeKey , value .getClass ().getName ()));
957
+ } else if (value instanceof Iterable ) {
958
+ serializeIterable (name , value , builder , null );
959
+ } else if (value instanceof Map ) {
960
+ serializeMap (name , value , builder , String .class , additionalFields );
961
+ } else if (value .getClass ().isArray ()) {
962
+ serializeArray (name , value , builder , null );
936
963
} else {
937
964
serializeObject (name , value , builder , additionalFields );
938
965
}
@@ -944,6 +971,9 @@ private boolean shouldAddTypeHint(
944
971
final Object value ,
945
972
final FieldInfo fieldInfo
946
973
) {
974
+ if (!useTypeHints ) {
975
+ return false ;
976
+ }
947
977
final AnnotatedElement referencingElement = fieldInfo != null ? fieldInfo .getReferencingElement () : null ;
948
978
final BuilderInfo builderInfo = builderUtils .getBuilderInfo (type , referencingElement );
949
979
if (builderInfo != null ) {
0 commit comments