Skip to content

Commit 1afde9e

Browse files
author
Mark
committed
added VeloyPack serialization support for java.time.Instant, LocalDate,
LocalDateTime
1 parent d225c3c commit 1afde9e

File tree

7 files changed

+282
-1
lines changed

7 files changed

+282
-1
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
v4.1.0 (2016-10-xx)
2+
---------------------------
3+
* changed VelocyStream communication (send protocol header)
4+
* added VeloyPack serialization support for java.time.Instant, LocalDate, LocalDateTime

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@
211211
<dependency>
212212
<groupId>com.arangodb</groupId>
213213
<artifactId>arangodb-java-driver</artifactId>
214-
<version>4.0.1-SNAPSHOT</version>
214+
<version>4.1.0-SNAPSHOT</version>
215215
</dependency>
216216
<dependency>
217217
<groupId>ch.qos.logback</groupId>

src/main/java/com/arangodb/ArangoDBAsync.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.arangodb.internal.DocumentCache;
4040
import com.arangodb.internal.InternalArangoDB;
4141
import com.arangodb.internal.velocypack.VPackConfigure;
42+
import com.arangodb.internal.velocypack.VPackConfigureAsync;
4243
import com.arangodb.internal.velocystream.Communication;
4344
import com.arangodb.internal.velocystream.CommunicationAsync;
4445
import com.arangodb.internal.velocystream.CommunicationSync;
@@ -90,6 +91,7 @@ public Builder() {
9091
collectionCache = new CollectionCache();
9192
vpackParser = new VPackParser();
9293
VPackConfigure.configure(vpackBuilder, vpackParser, collectionCache);
94+
VPackConfigureAsync.configure(vpackBuilder);
9395
loadProperties(ArangoDBAsync.class.getResourceAsStream(DEFAULT_PROPERTY_FILE));
9496
}
9597

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.velocypack;
22+
23+
import java.time.Instant;
24+
import java.time.LocalDate;
25+
import java.time.LocalDateTime;
26+
27+
import com.arangodb.velocypack.VPack;
28+
29+
/**
30+
* @author Mark - mark at arangodb.com
31+
*
32+
*/
33+
public class VPackConfigureAsync {
34+
35+
public static void configure(final VPack.Builder builder) {
36+
37+
builder.registerDeserializer(Instant.class, VPackDeserializersAsync.INSTANT);
38+
builder.registerDeserializer(LocalDate.class, VPackDeserializersAsync.LOCAL_DATE);
39+
builder.registerDeserializer(LocalDateTime.class, VPackDeserializersAsync.LOCAL_DATE_TIME);
40+
41+
builder.registerSerializer(Instant.class, VPackSerializersAsync.INSTANT);
42+
builder.registerSerializer(LocalDate.class, VPackSerializersAsync.LOCAL_DATE);
43+
builder.registerSerializer(LocalDateTime.class, VPackSerializersAsync.LOCAL_DATE_TIME);
44+
45+
}
46+
47+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.velocypack;
22+
23+
import java.time.Instant;
24+
import java.time.LocalDate;
25+
import java.time.LocalDateTime;
26+
import java.time.ZoneId;
27+
28+
import com.arangodb.velocypack.VPackDeserializer;
29+
30+
/**
31+
* @author Mark - mark at arangodb.com
32+
*
33+
*/
34+
public class VPackDeserializersAsync {
35+
36+
public static VPackDeserializer<Instant> INSTANT = (parent, vpack, context) -> {
37+
return vpack.getAsDate().toInstant();
38+
};
39+
public static VPackDeserializer<LocalDate> LOCAL_DATE = (parent, vpack, context) -> {
40+
return vpack.getAsDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
41+
};
42+
public static VPackDeserializer<LocalDateTime> LOCAL_DATE_TIME = (parent, vpack, context) -> {
43+
return vpack.getAsDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
44+
};
45+
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.velocypack;
22+
23+
import java.time.Instant;
24+
import java.time.LocalDate;
25+
import java.time.LocalDateTime;
26+
import java.time.ZoneId;
27+
import java.util.Date;
28+
29+
import com.arangodb.velocypack.VPackSerializer;
30+
31+
/**
32+
* @author Mark - mark at arangodb.com
33+
*
34+
*/
35+
public class VPackSerializersAsync {
36+
37+
public static VPackSerializer<Instant> INSTANT = (builder, attribute, value, context) -> {
38+
builder.add(attribute, Date.from(value));
39+
};
40+
public static VPackSerializer<LocalDate> LOCAL_DATE = (builder, attribute, value, context) -> {
41+
builder.add(attribute, Date.from(value.atStartOfDay(ZoneId.systemDefault()).toInstant()));
42+
};
43+
public static VPackSerializer<LocalDateTime> LOCAL_DATE_TIME = (builder, attribute, value, context) -> {
44+
builder.add(attribute, Date.from(value.atZone(ZoneId.systemDefault()).toInstant()));
45+
};
46+
47+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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.velocypack;
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.time.Instant;
28+
import java.time.LocalDate;
29+
import java.time.LocalDateTime;
30+
import java.time.ZoneId;
31+
import java.util.Date;
32+
33+
import org.junit.Test;
34+
35+
import com.arangodb.internal.velocypack.VPackConfigureAsync;
36+
37+
/**
38+
* @author Mark - mark at arangodb.com
39+
*
40+
*/
41+
public class VPackSerializeDeserializeTest {
42+
43+
protected static class TestEntityDate {
44+
private Instant instant;
45+
private LocalDate localDate;
46+
private LocalDateTime localDateTime;
47+
48+
public TestEntityDate(final long millis) {
49+
super();
50+
instant = Instant.ofEpochMilli(millis);
51+
localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
52+
localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
53+
}
54+
55+
public TestEntityDate() {
56+
super();
57+
}
58+
59+
public Instant getInstant() {
60+
return instant;
61+
}
62+
63+
public void setInstant(final Instant instant) {
64+
this.instant = instant;
65+
}
66+
67+
public LocalDate getLocalDate() {
68+
return localDate;
69+
}
70+
71+
public void setLocalDate(final LocalDate localDate) {
72+
this.localDate = localDate;
73+
}
74+
75+
public LocalDateTime getLocalDateTime() {
76+
return localDateTime;
77+
}
78+
79+
public void setLocalDateTime(final LocalDateTime localDateTime) {
80+
this.localDateTime = localDateTime;
81+
}
82+
83+
}
84+
85+
@SuppressWarnings("deprecation")
86+
@Test
87+
public void serializeDate() {
88+
final VPack.Builder builder = new VPack.Builder();
89+
VPackConfigureAsync.configure(builder);
90+
final VPackSlice vpack = builder.build().serialize(new TestEntityDate(1474988621));
91+
assertThat(vpack, is(notNullValue()));
92+
assertThat(vpack.isObject(), is(true));
93+
assertThat(vpack.get("instant").isDate(), is(true));
94+
assertThat(vpack.get("instant").getAsDate(), is(new Date(1474988621)));
95+
assertThat(vpack.get("localDate").isDate(), is(true));
96+
assertThat(vpack.get("localDate").getAsDate(), is(new Date(70, 0, 18)));
97+
assertThat(vpack.get("localDateTime").isDate(), is(true));
98+
assertThat(vpack.get("localDateTime").getAsDate(), is(new Date(1474988621)));
99+
}
100+
101+
@SuppressWarnings("deprecation")
102+
@Test
103+
public void deserializeDate() {
104+
final VPackBuilder builder = new VPackBuilder();
105+
builder.add(ValueType.OBJECT);
106+
builder.add("instant", new Date(1475062216));
107+
builder.add("localDate", new Date(70, 0, 18));
108+
builder.add("localDateTime", new Date(1475062216));
109+
builder.close();
110+
111+
final VPack.Builder vpackBuilder = new VPack.Builder();
112+
VPackConfigureAsync.configure(vpackBuilder);
113+
final TestEntityDate entity = vpackBuilder.build().deserialize(builder.slice(), TestEntityDate.class);
114+
assertThat(entity, is(notNullValue()));
115+
assertThat(entity.instant, is(Instant.ofEpochMilli(1475062216)));
116+
assertThat(entity.localDate, is(Instant.ofEpochMilli(1475062216).atZone(ZoneId.systemDefault()).toLocalDate()));
117+
assertThat(entity.localDateTime,
118+
is(LocalDateTime.ofInstant(Instant.ofEpochMilli(1475062216), ZoneId.systemDefault())));
119+
}
120+
121+
@Test
122+
public void date() {
123+
final VPack.Builder builder = new VPack.Builder();
124+
VPackConfigureAsync.configure(builder);
125+
final TestEntityDate entity = new TestEntityDate(1474988621);
126+
final VPackSlice vpack = builder.build().serialize(entity);
127+
assertThat(vpack, is(notNullValue()));
128+
final TestEntityDate entity2 = builder.build().deserialize(vpack, TestEntityDate.class);
129+
assertThat(entity2, is(notNullValue()));
130+
assertThat(entity2.instant, is(entity.instant));
131+
assertThat(entity2.localDate, is(entity.localDate));
132+
assertThat(entity2.localDateTime, is(entity.localDateTime));
133+
}
134+
135+
}

0 commit comments

Comments
 (0)