Skip to content

Commit e1e96f2

Browse files
committed
Support serializing collections with null elements and non-generic classes that extend collections.
1 parent 60c2923 commit e1e96f2

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ private void serializeIterable(
652652
builder.add(name, ValueType.ARRAY);
653653
for (final Iterator iterator = Iterable.class.cast(value).iterator(); iterator.hasNext();) {
654654
final Object element = iterator.next();
655-
addValue(null, element.getClass(), element, builder, null, additionalFields);
655+
addValue(null, element != null ? element.getClass() : null, element, builder, null, additionalFields);
656656
}
657657
builder.close();
658658
}

src/main/java/com/arangodb/velocypack/internal/VPackCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private FieldInfo createFieldInfo(final Field field) {
187187
final Class<?> clazz = field.getType();
188188
final Type type;
189189
if (Collection.class.isAssignableFrom(clazz) || Map.class.isAssignableFrom(clazz)) {
190-
type = (ParameterizedType) field.getGenericType();
190+
type = field.getGenericType();
191191
} else {
192192
type = clazz;
193193
}

src/test/java/com/arangodb/velocypack/VPackSerializeDeserializeTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.arangodb.velocypack;
2222

2323
import static org.hamcrest.Matchers.is;
24+
import static org.hamcrest.Matchers.contains;
2425
import static org.hamcrest.Matchers.notNullValue;
2526
import static org.hamcrest.Matchers.nullValue;
2627
import static org.junit.Assert.assertThat;
@@ -981,7 +982,83 @@ public void toArrayInArray() throws VPackException {
981982
}
982983
}
983984

985+
protected static class TestCollection extends LinkedList<String> {
986+
987+
}
988+
989+
protected static class TestEntityCollectionExtendedWithNulls {
990+
991+
protected TestCollection a1;
992+
993+
public TestCollection getA1() {
994+
return a1;
995+
}
996+
997+
public void setA1(final TestCollection a1) {
998+
this.a1 = a1;
999+
}
1000+
1001+
}
1002+
1003+
@Test
1004+
public void fromCollectionExtendedWithNulls() throws Exception {
1005+
1006+
final TestCollection collection = new TestCollection();
1007+
collection.add("one");
1008+
collection.add(null);
1009+
collection.add("two");
1010+
1011+
final TestEntityCollectionExtendedWithNulls entity = new TestEntityCollectionExtendedWithNulls();
1012+
entity.setA1(collection);
1013+
1014+
final VPackSlice vpack = new VPack.Builder()
1015+
.serializeNullValues(true)
1016+
.build()
1017+
.serialize(entity);
1018+
assertThat(vpack, is(notNullValue()));
1019+
assertThat(vpack.isObject(), is(true));
1020+
{
1021+
final VPackSlice a1 = vpack.get("a1");
1022+
assertThat(a1.isArray(), is(true));
1023+
assertThat(a1.getLength(), is(entity.a1.size()));
1024+
1025+
VPackSlice at = a1.get(0);
1026+
assertThat(at.isString(), is(true));
1027+
assertThat(at.getAsString(), is(entity.a1.get(0)));
1028+
at = a1.get(1);
1029+
assertThat(at.isNull(), is(true));
1030+
at = a1.get(2);
1031+
assertThat(at.isString(), is(true));
1032+
assertThat(at.getAsString(), is(entity.a1.get(2)));
1033+
}
1034+
}
1035+
1036+
@Test
1037+
public void toCollectionExtendedWithNulls() throws Exception {
1038+
final VPackBuilder builder = new VPackBuilder();
1039+
{
1040+
builder.add(ValueType.OBJECT);
1041+
{
1042+
builder.add("a1", ValueType.ARRAY);
1043+
builder.add("one");
1044+
builder.add(ValueType.NULL);
1045+
builder.add("two");
1046+
builder.close();
1047+
}
1048+
builder.close();
1049+
}
1050+
1051+
final VPackSlice vpack = builder.slice();
1052+
final TestEntityCollectionExtendedWithNulls entity = new VPack.Builder()
1053+
.build().deserialize(vpack, TestEntityCollectionExtendedWithNulls.class);
1054+
assertThat(entity, is(notNullValue()));
1055+
assertThat(entity.getA1(), is(notNullValue()));
1056+
assertThat(entity.getA1().size(), is(3));
1057+
assertThat(entity.getA1(), contains("one", null, "two"));
1058+
}
1059+
9841060
protected static class TestEntityArrayInArrayInArray {
1061+
9851062
private double[][][] a1;
9861063

9871064
public double[][][] getA1() {

0 commit comments

Comments
 (0)