Skip to content

Feature/use type hint #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

- added useTypeHint option to VPack.Builder

## [2.3.1] - 2020-05-05

- shaded jackson dependency
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.arangodb</groupId>
<artifactId>velocypack</artifactId>
<version>2.3.1</version>
<version>2.4.0-SNAPSHOT</version>
<inceptionYear>2017</inceptionYear>
<packaging>jar</packaging>

Expand Down
38 changes: 34 additions & 4 deletions src/main/java/com/arangodb/velocypack/VPack.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class VPack {
private static final String ATTR_KEY = "key";
private static final String ATTR_VALUE = "value";
private static final String DEFAULT_TYPE_KEY = "_class";
private static final boolean DEFAULT_USE_TYPE_HINTS = true;

private final Map<Type, VPackSerializer<?>> serializers;
private final Map<Type, VPackSerializer<?>> enclosingSerializers;
Expand All @@ -65,6 +66,7 @@ public class VPack {
private final VPackDeserializationContext deserializationContext;
private final boolean serializeNullValues;
private final String typeKey;
private final boolean useTypeHints;
private final VPackBuilderUtils builderUtils;
private final VPackCreatorMethodUtils vPackCreatorMethodUtils;

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

public Builder() {
super();
Expand All @@ -99,6 +102,7 @@ public Builder() {
annotationFieldNaming = new HashMap<>();
keyMapAdapters = new HashMap<>();
typeKey = null;
useTypeHints = null;

instanceCreators.put(Iterable.class, VPackInstanceCreators.ITERABLE);
instanceCreators.put(Collection.class, VPackInstanceCreators.COLLECTION);
Expand Down Expand Up @@ -319,19 +323,35 @@ public Builder registerKeyMapAdapter(final Type type, final VPackKeyMapAdapter<?
* @param typeKey Name of the field with type information
* @return {@link VPack.Builder}
*/
public Builder typeKey(final String typeKey) {
@Override
public VPack.Builder typeKey(final String typeKey) {
this.typeKey = typeKey;
return this;
}

/**
* Enables storing type information of the serialized object
*
* @param useTypeHints (default: {@code true})
* @return {@link VPack.Builder}
*/
@Override
public VPack.Builder useTypeHints(final boolean useTypeHints) {
this.useTypeHints = useTypeHints;
return this;
}

public synchronized VPack build() {
return new VPack(new HashMap<>(serializers), new HashMap<>(enclosingSerializers), new HashMap<>(deserializers),
new HashMap<>(deserializersWithSelfNullHandle),
new HashMap<>(instanceCreators), builderOptions, serializeNullValues,
fieldNamingStrategy, new HashMap<>(deserializersByName),
new HashMap<>(deserializersByNameWithSelfNullHandle),
new HashMap<>(annotationFieldFilter), new HashMap<>(annotationFieldNaming),
keyMapAdapters, typeKey != null ? typeKey : DEFAULT_TYPE_KEY);
keyMapAdapters,
typeKey != null ? typeKey : DEFAULT_TYPE_KEY,
useTypeHints != null ? useTypeHints : DEFAULT_USE_TYPE_HINTS
);
}

}
Expand All @@ -345,7 +365,7 @@ private VPack(final Map<Type, VPackSerializer<?>> serializers,
final Map<String, Map<Type, VPackDeserializer<?>>> deserializersByNameWithSelfNullHandle,
final Map<Class<? extends Annotation>, VPackAnnotationFieldFilter<? extends Annotation>> annotationFieldFilter,
final Map<Class<? extends Annotation>, VPackAnnotationFieldNaming<? extends Annotation>> annotationFieldNaming,
final Map<Type, VPackKeyMapAdapter<?>> keyMapAdapters, final String typeKey) {
final Map<Type, VPackKeyMapAdapter<?>> keyMapAdapters, final String typeKey, final boolean useTypeHints) {
super();
this.serializers = serializers;
this.enclosingSerializers = enclosingSerializers;
Expand All @@ -358,6 +378,7 @@ private VPack(final Map<Type, VPackSerializer<?>> serializers,
this.deserializersByNameWithSelfNullHandle = deserializersByNameWithSelfNullHandle;
this.keyMapAdapters = keyMapAdapters;
this.typeKey = typeKey;
this.useTypeHints = useTypeHints;

builderUtils = new VPackBuilderUtils();
vPackCreatorMethodUtils = new VPackCreatorMethodUtils();
Expand Down Expand Up @@ -480,7 +501,7 @@ private <T> T deserializeObject(
}

private Type determineType(final VPackSlice vpack, final Type type) {
if (!vpack.isObject()) {
if (!useTypeHints || !vpack.isObject()) {
return type;
}
final VPackSlice clazz = vpack.get(typeKey);
Expand Down Expand Up @@ -933,6 +954,12 @@ private void addValue(
} else if (shouldAddTypeHint(type, value, fieldInfo)) {
addValue(name, value.getClass(), value, builder, fieldInfo,
Collections.<String, Object>singletonMap(typeKey, value.getClass().getName()));
} else if (value instanceof Iterable) {
serializeIterable(name, value, builder, null);
} else if (value instanceof Map) {
serializeMap(name, value, builder, String.class, additionalFields);
} else if (value.getClass().isArray()) {
serializeArray(name, value, builder, null);
} else {
serializeObject(name, value, builder, additionalFields);
}
Expand All @@ -944,6 +971,9 @@ private boolean shouldAddTypeHint(
final Object value,
final FieldInfo fieldInfo
) {
if (!useTypeHints) {
return false;
}
final AnnotatedElement referencingElement = fieldInfo != null ? fieldInfo.getReferencingElement() : null;
final BuilderInfo builderInfo = builderUtils.getBuilderInfo(type, referencingElement);
if (builderInfo != null) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/arangodb/velocypack/VPackSetupContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ <A extends Annotation> C annotationFieldFilter(
final VPackAnnotationFieldFilter<A> fieldFilter);

<A extends Annotation> C annotationFieldNaming(
final Class<A> type,
final VPackAnnotationFieldNaming<A> fieldNaming);
final Class<A> type,
final VPackAnnotationFieldNaming<A> fieldNaming);

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

C registerModule(VPackModule module);

C registerModules(VPackModule... modules);

C typeKey(final String typeKey);

C useTypeHints(final boolean useTypeHints);

}
Loading