Skip to content

Commit c0cad9f

Browse files
author
mpv1989
committed
Add support for custom serializer
1 parent 8f326e3 commit c0cad9f

22 files changed

+1014
-860
lines changed

Diff for: ChangeLog

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
v4.5.2 (xxxx-xx-xx)
2+
---------------------------
3+
* added support for custom serializer
4+
* added ArangoDB.Builder.serializer(ArangoSerialization)
5+
* added link to jackson-dataformat-velocypack in docs
6+
17
v4.5.1 (2018-06-21)
28
---------------------------
39
* fixed exists() method in ArangoDatabase, ArangoCollection, ArangoGraph: check for ArangoDB error num

Diff for: docs/Drivers/Java/Reference/Serialization.md

+7
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ Added support for:
7575
ArangoDB arangoDB = new ArangoDB.Builder().registerModule(new VPackJodaModule()).build();
7676
```
7777

78+
## Use of jackson as an alternative serializer
79+
80+
Since version 4.5.2, the driver supports alternative serializer to de-/serialize documents, edges and query results. One implementation is `VelocyJack` which is based on [Jackson](https://github.com/FasterXML/jackson) working with [jackson -dataformat-velocypack](https://github.com/arangodb/jackson-dataformat-velocypack#usage-within-arangodb-java-driver).
81+
82+
83+
**Note**: Any registered custom [serializer/deserializer or module](#custom-serialization) will be ignored.
84+
7885
## custom serialization
7986

8087
```Java

Diff for: pom.xml

+31-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.arangodb</groupId>
66
<artifactId>arangodb-java-driver</artifactId>
7-
<version>4.5.1-SNAPSHOT</version>
7+
<version>4.5.2-SNAPSHOT</version>
88
<inceptionYear>2016</inceptionYear>
99
<packaging>jar</packaging>
1010

@@ -23,11 +23,16 @@
2323
<properties>
2424
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2525
<slf4j-api.version>1.7.13</slf4j-api.version>
26+
<arangodb.velocypack.version>1.2.0</arangodb.velocypack.version>
27+
28+
<!-- provided -->
29+
<httpclient.version>4.5.1</httpclient.version>
30+
31+
<!-- test -->
2632
<logback-classic.version>1.1.3</logback-classic.version>
2733
<hamcrest-all.version>1.3</hamcrest-all.version>
2834
<junit.version>4.12</junit.version>
29-
<httpclient.version>4.5.1</httpclient.version>
30-
<arangodb.velocypack.version>1.2.0</arangodb.velocypack.version>
35+
<arangodb.velocypack.jackson.version>0.1.1</arangodb.velocypack.jackson.version>
3136
</properties>
3237

3338
<developers>
@@ -117,6 +122,19 @@
117122
<target>1.6</target>
118123
<compilerArgument></compilerArgument>
119124
</configuration>
125+
<executions>
126+
<execution>
127+
<id>test-compile</id>
128+
<phase>process-test-sources</phase>
129+
<goals>
130+
<goal>testCompile</goal>
131+
</goals>
132+
<configuration>
133+
<source>1.6</source>
134+
<target>1.7</target>
135+
</configuration>
136+
</execution>
137+
</executions>
120138
</plugin>
121139

122140
<plugin>
@@ -213,6 +231,11 @@
213231
<groupId>org.slf4j</groupId>
214232
<artifactId>slf4j-api</artifactId>
215233
</dependency>
234+
<dependency>
235+
<groupId>com.arangodb</groupId>
236+
<artifactId>jackson-dataformat-velocypack</artifactId>
237+
<scope>test</scope>
238+
</dependency>
216239
<dependency>
217240
<groupId>ch.qos.logback</groupId>
218241
<artifactId>logback-classic</artifactId>
@@ -247,6 +270,11 @@
247270
<artifactId>slf4j-api</artifactId>
248271
<version>${slf4j-api.version}</version>
249272
</dependency>
273+
<dependency>
274+
<groupId>com.arangodb</groupId>
275+
<artifactId>jackson-dataformat-velocypack</artifactId>
276+
<version>${arangodb.velocypack.jackson.version}</version>
277+
</dependency>
250278
<dependency>
251279
<groupId>ch.qos.logback</groupId>
252280
<artifactId>logback-classic</artifactId>

Diff for: src/main/java/com/arangodb/ArangoDB.java

+30-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
import com.arangodb.internal.net.HostHandler;
4444
import com.arangodb.internal.net.HostResolver;
4545
import com.arangodb.internal.util.ArangoDeserializerImpl;
46+
import com.arangodb.internal.util.ArangoSerializationFactory;
47+
import com.arangodb.internal.util.ArangoSerializationFactory.Serializer;
4648
import com.arangodb.internal.util.ArangoSerializerImpl;
47-
import com.arangodb.internal.util.ArangoUtilImpl;
49+
import com.arangodb.internal.util.DefaultArangoSerialization;
4850
import com.arangodb.internal.velocypack.VPackDocumentModule;
4951
import com.arangodb.internal.velocystream.VstCommunicationSync;
5052
import com.arangodb.model.LogOptions;
@@ -276,8 +278,10 @@ public Builder registerJsonModules(final VPackParserModule... module) {
276278
*
277279
* @param serializer
278280
* custom serializer
281+
* @deprecated use {@link #serializer(ArangoSerialization)} instead
279282
* @return builder
280283
*/
284+
@Deprecated
281285
public Builder setSerializer(final ArangoSerializer serializer) {
282286
serializer(serializer);
283287
return this;
@@ -291,13 +295,30 @@ public Builder setSerializer(final ArangoSerializer serializer) {
291295
*
292296
* @param deserializer
293297
* custom deserializer
298+
* @deprecated use {@link #serializer(ArangoSerialization)} instead
294299
* @return builder
295300
*/
301+
@Deprecated
296302
public Builder setDeserializer(final ArangoDeserializer deserializer) {
297303
deserializer(deserializer);
298304
return this;
299305
}
300306

307+
/**
308+
* Replace the built-in serializer/deserializer with the given one.
309+
*
310+
* <br />
311+
* <b>ATTENTION!:</b> Any registered custom serializer/deserializer or module will be ignored.
312+
*
313+
* @param serialization
314+
* custom serializer/deserializer
315+
* @return builder
316+
*/
317+
public Builder serializer(final ArangoSerialization serialization) {
318+
setSerializer(serialization);
319+
return this;
320+
}
321+
301322
public synchronized ArangoDB build() {
302323
if (hosts.isEmpty()) {
303324
hosts.add(host);
@@ -313,6 +334,10 @@ public synchronized ArangoDB build() {
313334
: new ArangoSerializerImpl(vpacker, vpackerNull, vpackParser);
314335
final ArangoDeserializer deserializerTemp = deserializer != null ? deserializer
315336
: new ArangoDeserializerImpl(vpackerNull, vpackParser);
337+
final DefaultArangoSerialization internal = new DefaultArangoSerialization(serializerTemp,
338+
deserializerTemp);
339+
final ArangoSerialization custom = customSerializer != null ? customSerializer : internal;
340+
final ArangoSerializationFactory util = new ArangoSerializationFactory(internal, custom);
316341

317342
final HostResolver hostResolver = createHostResolver();
318343
final HostHandler hostHandler = createHostHandler(hostResolver);
@@ -323,7 +348,7 @@ public synchronized ArangoDB build() {
323348
new HttpCommunication.Builder(hostHandler, protocol).timeout(timeout).user(user).password(password)
324349
.useSsl(useSsl).sslContext(sslContext).maxConnections(maxConnections)
325350
.connectionTtl(connectionTtl),
326-
new ArangoUtilImpl(serializerTemp, deserializerTemp), collectionCache, protocol, hostResolver);
351+
util, collectionCache, protocol, hostResolver);
327352
}
328353

329354
}
@@ -591,4 +616,7 @@ UserEntity createUser(final String user, final String passwd, final UserCreateOp
591616
ArangoDB _setCursorInitializer(final ArangoCursorInitializer cursorInitializer);
592617

593618
ArangoSerialization util();
619+
620+
ArangoSerialization util(Serializer serializer);
621+
594622
}
+85-84
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,85 @@
1-
/*
2-
* DISCLAIMER
3-
*
4-
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5-
*
6-
* Licensed under the Apache License, Version 2.0 (the "License");
7-
* you may not use this file except in compliance with the License.
8-
* You may obtain a copy of the License at
9-
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
11-
*
12-
* Unless required by applicable law or agreed to in writing, software
13-
* distributed under the License is distributed on an "AS IS" BASIS,
14-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
* See the License for the specific language governing permissions and
16-
* limitations under the License.
17-
*
18-
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19-
*/
20-
21-
package com.arangodb.internal;
22-
23-
import java.util.Iterator;
24-
import java.util.NoSuchElementException;
25-
26-
import com.arangodb.ArangoCursor;
27-
import com.arangodb.entity.CursorEntity;
28-
import com.arangodb.velocypack.VPackSlice;
29-
30-
/**
31-
* @author Mark Vollmary
32-
* @param <T>
33-
*
34-
*/
35-
public class ArangoCursorIterator<T> implements Iterator<T> {
36-
37-
private CursorEntity result;
38-
private int pos;
39-
40-
private final ArangoCursor<T> cursor;
41-
private final InternalArangoDatabase<?, ?, ?, ?> db;
42-
private final ArangoCursorExecute execute;
43-
44-
public ArangoCursorIterator(final ArangoCursor<T> cursor, final ArangoCursorExecute execute,
45-
final InternalArangoDatabase<?, ?, ?, ?> db, final CursorEntity result) {
46-
super();
47-
this.cursor = cursor;
48-
this.execute = execute;
49-
this.db = db;
50-
this.result = result;
51-
pos = 0;
52-
}
53-
54-
public CursorEntity getResult() {
55-
return result;
56-
}
57-
58-
@Override
59-
public boolean hasNext() {
60-
return pos < result.getResult().size() || result.getHasMore();
61-
}
62-
63-
@Override
64-
public T next() {
65-
if (pos >= result.getResult().size() && result.getHasMore()) {
66-
result = execute.next(cursor.getId());
67-
pos = 0;
68-
}
69-
if (!hasNext()) {
70-
throw new NoSuchElementException();
71-
}
72-
return deserialize(result.getResult().get(pos++), cursor.getType());
73-
}
74-
75-
protected <R> R deserialize(final VPackSlice result, final Class<R> type) {
76-
return db.util().deserialize(result, type);
77-
}
78-
79-
@Override
80-
public void remove() {
81-
throw new UnsupportedOperationException();
82-
}
83-
84-
}
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.internal;
22+
23+
import java.util.Iterator;
24+
import java.util.NoSuchElementException;
25+
26+
import com.arangodb.ArangoCursor;
27+
import com.arangodb.entity.CursorEntity;
28+
import com.arangodb.internal.util.ArangoSerializationFactory.Serializer;
29+
import com.arangodb.velocypack.VPackSlice;
30+
31+
/**
32+
* @author Mark Vollmary
33+
* @param <T>
34+
*
35+
*/
36+
public class ArangoCursorIterator<T> implements Iterator<T> {
37+
38+
private CursorEntity result;
39+
private int pos;
40+
41+
private final ArangoCursor<T> cursor;
42+
private final InternalArangoDatabase<?, ?, ?, ?> db;
43+
private final ArangoCursorExecute execute;
44+
45+
public ArangoCursorIterator(final ArangoCursor<T> cursor, final ArangoCursorExecute execute,
46+
final InternalArangoDatabase<?, ?, ?, ?> db, final CursorEntity result) {
47+
super();
48+
this.cursor = cursor;
49+
this.execute = execute;
50+
this.db = db;
51+
this.result = result;
52+
pos = 0;
53+
}
54+
55+
public CursorEntity getResult() {
56+
return result;
57+
}
58+
59+
@Override
60+
public boolean hasNext() {
61+
return pos < result.getResult().size() || result.getHasMore();
62+
}
63+
64+
@Override
65+
public T next() {
66+
if (pos >= result.getResult().size() && result.getHasMore()) {
67+
result = execute.next(cursor.getId());
68+
pos = 0;
69+
}
70+
if (!hasNext()) {
71+
throw new NoSuchElementException();
72+
}
73+
return deserialize(result.getResult().get(pos++), cursor.getType());
74+
}
75+
76+
protected <R> R deserialize(final VPackSlice result, final Class<R> type) {
77+
return db.util(Serializer.CUSTOM).deserialize(result, type);
78+
}
79+
80+
@Override
81+
public void remove() {
82+
throw new UnsupportedOperationException();
83+
}
84+
85+
}

Diff for: src/main/java/com/arangodb/internal/ArangoDBImpl.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import com.arangodb.internal.net.HostHandle;
4545
import com.arangodb.internal.net.HostResolver;
4646
import com.arangodb.internal.net.HostResolver.EndpointResolver;
47+
import com.arangodb.internal.util.ArangoSerializationFactory;
48+
import com.arangodb.internal.util.ArangoSerializationFactory.Serializer;
4749
import com.arangodb.internal.velocystream.VstCommunicationSync;
4850
import com.arangodb.internal.velocystream.VstProtocol;
4951
import com.arangodb.internal.velocystream.internal.ConnectionSync;
@@ -68,12 +70,14 @@ public class ArangoDBImpl extends InternalArangoDB<ArangoExecutorSync, Response,
6870
private CommunicationProtocol cp;
6971

7072
public ArangoDBImpl(final VstCommunicationSync.Builder vstBuilder, final HttpCommunication.Builder httpBuilder,
71-
final ArangoSerialization util, final CollectionCache collectionCache, final Protocol protocol,
73+
final ArangoSerializationFactory util, final CollectionCache collectionCache, final Protocol protocol,
7274
final HostResolver hostResolver) {
73-
super(new ArangoExecutorSync(createProtocol(vstBuilder, httpBuilder, util, collectionCache, protocol), util,
75+
super(new ArangoExecutorSync(
76+
createProtocol(vstBuilder, httpBuilder, util.get(Serializer.INTERNAL), collectionCache, protocol), util,
7477
new DocumentCache()), util);
7578
cp = createProtocol(new VstCommunicationSync.Builder(vstBuilder).maxConnections(1),
76-
new HttpCommunication.Builder(httpBuilder).maxConnections(1), util, collectionCache, protocol);
79+
new HttpCommunication.Builder(httpBuilder).maxConnections(1), util.get(Serializer.INTERNAL),
80+
collectionCache, protocol);
7781
collectionCache.init(new DBAccess() {
7882
@Override
7983
public ArangoDatabase db(final String name) {
@@ -292,4 +296,5 @@ public ArangoDBImpl _setCursorInitializer(final ArangoCursorInitializer cursorIn
292296
this.cursorInitializer = cursorInitializer;
293297
return this;
294298
}
299+
295300
}

0 commit comments

Comments
 (0)