Skip to content

Commit e2dd6ec

Browse files
author
mpv1989
committed
just an idea of jackson integration
1 parent 8e7a5f4 commit e2dd6ec

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@
201201
<groupId>com.arangodb</groupId>
202202
<artifactId>velocypack</artifactId>
203203
</dependency>
204+
<dependency>
205+
<groupId>com.arangodb</groupId>
206+
<artifactId>jackson-dataformat-velocypack</artifactId>
207+
<scope>provided</scope>
208+
</dependency>
204209
<dependency>
205210
<groupId>org.slf4j</groupId>
206211
<artifactId>slf4j-api</artifactId>
@@ -233,6 +238,11 @@
233238
<artifactId>velocypack</artifactId>
234239
<version>${arangodb.velocypack.version}</version>
235240
</dependency>
241+
<dependency>
242+
<groupId>com.arangodb</groupId>
243+
<artifactId>jackson-dataformat-velocypack</artifactId>
244+
<version>1.0.0-SNAPSHOT</version>
245+
</dependency>
236246
<dependency>
237247
<groupId>org.slf4j</groupId>
238248
<artifactId>slf4j-api</artifactId>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.jackson;
22+
23+
import java.io.IOException;
24+
import java.lang.reflect.Type;
25+
import java.util.Map;
26+
27+
import com.arangodb.ArangoDBException;
28+
import com.arangodb.jackson.dataformat.velocypack.VPackMapper;
29+
import com.arangodb.util.ArangoDeserializer;
30+
import com.arangodb.util.ArangoSerializer;
31+
import com.arangodb.velocypack.VPackSlice;
32+
import com.fasterxml.jackson.core.JsonProcessingException;
33+
34+
/**
35+
* @author Mark - mark at arangodb.com
36+
*
37+
*/
38+
public class VelocyJack implements ArangoSerializer, ArangoDeserializer {
39+
40+
private final VPackMapper mapper;
41+
42+
public VelocyJack() {
43+
super();
44+
mapper = new VPackMapper();
45+
}
46+
47+
@SuppressWarnings("unchecked")
48+
@Override
49+
public <T> T deserialize(final VPackSlice vpack, final Type type) throws ArangoDBException {
50+
try {
51+
return mapper.readValue(vpack.getBuffer(), (Class<T>) type);
52+
} catch (final IOException e) {
53+
throw new ArangoDBException(e);
54+
}
55+
}
56+
57+
@Override
58+
public VPackSlice serialize(final Object entity) throws ArangoDBException {
59+
try {
60+
return new VPackSlice(mapper.writeValueAsBytes(entity));
61+
} catch (final JsonProcessingException e) {
62+
throw new ArangoDBException(e);
63+
}
64+
}
65+
66+
@Override
67+
public VPackSlice serialize(final Object entity, final boolean serializeNullValues) throws ArangoDBException {
68+
return null;
69+
}
70+
71+
@Override
72+
public VPackSlice serialize(final Object entity, final boolean serializeNullValues, final boolean stringAsJson)
73+
throws ArangoDBException {
74+
return null;
75+
}
76+
77+
@Override
78+
public VPackSlice serialize(final Object entity, final Type type) throws ArangoDBException {
79+
return null;
80+
}
81+
82+
@Override
83+
public VPackSlice serialize(final Object entity, final Type type, final boolean serializeNullValues)
84+
throws ArangoDBException {
85+
return null;
86+
}
87+
88+
@Override
89+
public VPackSlice serialize(final Object entity, final Type type, final Map<String, Object> additionalFields)
90+
throws ArangoDBException {
91+
return null;
92+
}
93+
94+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.jackson;
22+
23+
import static org.hamcrest.Matchers.is;
24+
import static org.hamcrest.Matchers.notNullValue;
25+
import static org.junit.Assert.assertThat;
26+
27+
import java.io.IOException;
28+
29+
import org.junit.Test;
30+
31+
import com.arangodb.velocypack.VPackSlice;
32+
33+
/**
34+
* @author Mark - mark at arangodb.com
35+
*
36+
*/
37+
public class SimpleTest {
38+
39+
private static final String TEST_STRING = "hello world";
40+
private static final int TEST_INT = 69;
41+
42+
public static class TestEntity {
43+
private String value1;
44+
private int value2;
45+
46+
public TestEntity(final String value1, final int value2) {
47+
super();
48+
this.value1 = value1;
49+
this.value2 = value2;
50+
}
51+
52+
public TestEntity() {
53+
super();
54+
}
55+
56+
public String getValue1() {
57+
return value1;
58+
}
59+
60+
public void setValue1(final String value1) {
61+
this.value1 = value1;
62+
}
63+
64+
public int getValue2() {
65+
return value2;
66+
}
67+
68+
public void setValue2(final int value2) {
69+
this.value2 = value2;
70+
}
71+
72+
}
73+
74+
@Test
75+
public void arangoVPackSerialization() throws IOException {
76+
final VelocyJack arango = new VelocyJack();
77+
78+
final VPackSlice slice = arango.serialize(new TestEntity(TEST_STRING, TEST_INT));
79+
assertThat(slice, is(notNullValue()));
80+
assertThat(slice.isObject(), is(true));
81+
assertThat(slice.size(), is(2));
82+
assertThat(slice.get("value1").isString(), is(true));
83+
assertThat(slice.get("value1").getAsString(), is(TEST_STRING));
84+
assertThat(slice.get("value2").isInteger(), is(true));
85+
assertThat(slice.get("value2").getAsInt(), is(TEST_INT));
86+
87+
final TestEntity entity = arango.deserialize(slice, TestEntity.class);
88+
assertThat(entity, is(notNullValue()));
89+
assertThat(entity.getValue1(), is(TEST_STRING));
90+
assertThat(entity.getValue2(), is(TEST_INT));
91+
}
92+
}

0 commit comments

Comments
 (0)