Skip to content

Commit b381322

Browse files
authored
Feature/use type hint (#26)
* added useTypeHint option to VPack.Builder * parameterized useTypeHint tests * renamed config parameter useTypeHint to useTypeHints * arrays tests without type hint * deserialization of iterable, arrays and maps without type hint * nestedObjectArrayWithoutTypeInformation test * added useTypeHints to VPackSetupContext
1 parent 9992969 commit b381322

File tree

5 files changed

+363
-7
lines changed

5 files changed

+363
-7
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
66

77
## [Unreleased]
88

9+
- added useTypeHint option to VPack.Builder
10+
911
## [2.3.1] - 2020-05-05
1012

1113
- shaded jackson dependency

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.arangodb</groupId>
66
<artifactId>velocypack</artifactId>
7-
<version>2.3.1</version>
7+
<version>2.4.0-SNAPSHOT</version>
88
<inceptionYear>2017</inceptionYear>
99
<packaging>jar</packaging>
1010

src/main/java/com/arangodb/velocypack/VPack.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class VPack {
4949
private static final String ATTR_KEY = "key";
5050
private static final String ATTR_VALUE = "value";
5151
private static final String DEFAULT_TYPE_KEY = "_class";
52+
private static final boolean DEFAULT_USE_TYPE_HINTS = true;
5253

5354
private final Map<Type, VPackSerializer<?>> serializers;
5455
private final Map<Type, VPackSerializer<?>> enclosingSerializers;
@@ -65,6 +66,7 @@ public class VPack {
6566
private final VPackDeserializationContext deserializationContext;
6667
private final boolean serializeNullValues;
6768
private final String typeKey;
69+
private final boolean useTypeHints;
6870
private final VPackBuilderUtils builderUtils;
6971
private final VPackCreatorMethodUtils vPackCreatorMethodUtils;
7072

@@ -83,6 +85,7 @@ public static class Builder implements VPackSetupContext<Builder> {
8385
private final Map<Class<? extends Annotation>, VPackAnnotationFieldNaming<? extends Annotation>> annotationFieldNaming;
8486
private final Map<Type, VPackKeyMapAdapter<?>> keyMapAdapters;
8587
private String typeKey;
88+
private Boolean useTypeHints;
8689

8790
public Builder() {
8891
super();
@@ -99,6 +102,7 @@ public Builder() {
99102
annotationFieldNaming = new HashMap<>();
100103
keyMapAdapters = new HashMap<>();
101104
typeKey = null;
105+
useTypeHints = null;
102106

103107
instanceCreators.put(Iterable.class, VPackInstanceCreators.ITERABLE);
104108
instanceCreators.put(Collection.class, VPackInstanceCreators.COLLECTION);
@@ -319,19 +323,35 @@ public Builder registerKeyMapAdapter(final Type type, final VPackKeyMapAdapter<?
319323
* @param typeKey Name of the field with type information
320324
* @return {@link VPack.Builder}
321325
*/
322-
public Builder typeKey(final String typeKey) {
326+
@Override
327+
public VPack.Builder typeKey(final String typeKey) {
323328
this.typeKey = typeKey;
324329
return this;
325330
}
326331

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+
327344
public synchronized VPack build() {
328345
return new VPack(new HashMap<>(serializers), new HashMap<>(enclosingSerializers), new HashMap<>(deserializers),
329346
new HashMap<>(deserializersWithSelfNullHandle),
330347
new HashMap<>(instanceCreators), builderOptions, serializeNullValues,
331348
fieldNamingStrategy, new HashMap<>(deserializersByName),
332349
new HashMap<>(deserializersByNameWithSelfNullHandle),
333350
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+
);
335355
}
336356

337357
}
@@ -345,7 +365,7 @@ private VPack(final Map<Type, VPackSerializer<?>> serializers,
345365
final Map<String, Map<Type, VPackDeserializer<?>>> deserializersByNameWithSelfNullHandle,
346366
final Map<Class<? extends Annotation>, VPackAnnotationFieldFilter<? extends Annotation>> annotationFieldFilter,
347367
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) {
349369
super();
350370
this.serializers = serializers;
351371
this.enclosingSerializers = enclosingSerializers;
@@ -358,6 +378,7 @@ private VPack(final Map<Type, VPackSerializer<?>> serializers,
358378
this.deserializersByNameWithSelfNullHandle = deserializersByNameWithSelfNullHandle;
359379
this.keyMapAdapters = keyMapAdapters;
360380
this.typeKey = typeKey;
381+
this.useTypeHints = useTypeHints;
361382

362383
builderUtils = new VPackBuilderUtils();
363384
vPackCreatorMethodUtils = new VPackCreatorMethodUtils();
@@ -480,7 +501,7 @@ private <T> T deserializeObject(
480501
}
481502

482503
private Type determineType(final VPackSlice vpack, final Type type) {
483-
if (!vpack.isObject()) {
504+
if (!useTypeHints || !vpack.isObject()) {
484505
return type;
485506
}
486507
final VPackSlice clazz = vpack.get(typeKey);
@@ -933,6 +954,12 @@ private void addValue(
933954
} else if (shouldAddTypeHint(type, value, fieldInfo)) {
934955
addValue(name, value.getClass(), value, builder, fieldInfo,
935956
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);
936963
} else {
937964
serializeObject(name, value, builder, additionalFields);
938965
}
@@ -944,6 +971,9 @@ private boolean shouldAddTypeHint(
944971
final Object value,
945972
final FieldInfo fieldInfo
946973
) {
974+
if (!useTypeHints) {
975+
return false;
976+
}
947977
final AnnotatedElement referencingElement = fieldInfo != null ? fieldInfo.getReferencingElement() : null;
948978
final BuilderInfo builderInfo = builderUtils.getBuilderInfo(type, referencingElement);
949979
if (builderInfo != null) {

src/main/java/com/arangodb/velocypack/VPackSetupContext.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@ <A extends Annotation> C annotationFieldFilter(
6060
final VPackAnnotationFieldFilter<A> fieldFilter);
6161

6262
<A extends Annotation> C annotationFieldNaming(
63-
final Class<A> type,
64-
final VPackAnnotationFieldNaming<A> fieldNaming);
63+
final Class<A> type,
64+
final VPackAnnotationFieldNaming<A> fieldNaming);
6565

6666
C registerKeyMapAdapter(final Type type, final VPackKeyMapAdapter<?> adapter);
6767

6868
C registerModule(VPackModule module);
6969

7070
C registerModules(VPackModule... modules);
7171

72+
C typeKey(final String typeKey);
73+
74+
C useTypeHints(final boolean useTypeHints);
75+
7276
}

0 commit comments

Comments
 (0)