Skip to content

Commit 066999e

Browse files
committed
Support byte and Byte serialization and deserialization by default.
1 parent 9051f03 commit 066999e

File tree

6 files changed

+97
-0
lines changed

6 files changed

+97
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ public Builder() {
126126
serializers.put(VPackSlice.class, VPackSerializers.VPACK);
127127
serializers.put(UUID.class, VPackSerializers.UUID);
128128
serializers.put(new byte[] {}.getClass(), VPackSerializers.BYTE_ARRAY);
129+
serializers.put(Byte.class, VPackSerializers.BYTE);
130+
serializers.put(byte.class, VPackSerializers.BYTE);
129131

130132
deserializers.put(String.class, VPackDeserializers.STRING);
131133
deserializers.put(Boolean.class, VPackDeserializers.BOOLEAN);
@@ -151,6 +153,8 @@ public Builder() {
151153
deserializers.put(VPackSlice.class, VPackDeserializers.VPACK);
152154
deserializers.put(UUID.class, VPackDeserializers.UUID);
153155
deserializers.put(new byte[] {}.getClass(), VPackDeserializers.BYTE_ARRAY);
156+
deserializers.put(Byte.class, VPackDeserializers.BYTE);
157+
deserializers.put(byte.class, VPackDeserializers.BYTE);
154158

155159
annotationFieldFilter.put(Expose.class, new VPackAnnotationFieldFilter<Expose>() {
156160
@Override

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ public void append(final VPackBuilder builder, final Short value) throws VPackBu
152152
}
153153
}
154154
};
155+
private static final Appender<Byte> BYTE = new Appender<Byte>() {
156+
@Override
157+
public void append(final VPackBuilder builder, final Byte value) {
158+
if (value <= 9 && value >= -6) {
159+
builder.appendSmallInt(value);
160+
} else {
161+
builder.add((byte) 0x23);
162+
builder.append(value, INTEGER_BYTES);
163+
}
164+
}
165+
};
155166
private static final Appender<BigInteger> BIG_INTEGER = new Appender<BigInteger>() {
156167
@Override
157168
public void append(final VPackBuilder builder, final BigInteger value) throws VPackBuilderException {
@@ -381,6 +392,10 @@ public VPackBuilder add(final String attribute, final Short value) throws VPackB
381392
return addInternal(attribute, SHORT, value);
382393
}
383394

395+
public VPackBuilder add(final String attribute, final Byte value) throws VPackBuilderException {
396+
return addInternal(attribute, BYTE, value);
397+
}
398+
384399
public VPackBuilder add(final String attribute, final BigInteger value) throws VPackBuilderException {
385400
return addInternal(attribute, BIG_INTEGER, value);
386401
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ public short getAsShort() {
253253
return getAsNumber().shortValue();
254254
}
255255

256+
public byte getAsByte() {
257+
return getAsNumber().byteValue();
258+
}
259+
256260
public BigInteger getAsBigInteger() {
257261
if (isSmallInt() || isInt()) {
258262
return BigInteger.valueOf(getAsLong());

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,14 @@ public byte[] deserialize(
231231
}
232232

233233
};
234+
public static final VPackDeserializer<Byte> BYTE = new VPackDeserializer<Byte>() {
235+
@Override
236+
public Byte deserialize(
237+
final VPackSlice parent,
238+
final VPackSlice vpack,
239+
final VPackDeserializationContext context) {
240+
return vpack.getAsByte();
241+
}
242+
};
234243

235244
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,14 @@ public void serialize(
215215
builder.add(attribute, DatatypeConverter.printBase64Binary(value));
216216
}
217217
};
218+
public static final VPackSerializer<Byte> BYTE = new VPackSerializer<Byte>() {
219+
@Override
220+
public void serialize(
221+
final VPackBuilder builder,
222+
final String attribute,
223+
final Byte value,
224+
final VPackSerializationContext context) {
225+
builder.add(attribute, value);
226+
}
227+
};
218228
}

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,61 @@ public void toShort() throws VPackException {
438438
assertThat(entity.s2, is(new Short((short) 3)));
439439
}
440440

441+
442+
protected static class TestEntityByte {
443+
private byte b1 = 1; // short integer path
444+
private Byte b2 = 100; // integer path
445+
446+
public byte getB1() {
447+
return b1;
448+
}
449+
450+
public void setB1(final byte b1) {
451+
this.b1 = b1;
452+
}
453+
454+
public Byte getB2() {
455+
return b2;
456+
}
457+
458+
public void setB2(final Byte b2) {
459+
this.b2 = b2;
460+
}
461+
}
462+
463+
@Test
464+
public void fromByte() throws VPackException {
465+
final VPackSlice vpack = new VPack.Builder().build().serialize(new TestEntityByte());
466+
assertThat(vpack, is(notNullValue()));
467+
assertThat(vpack.isObject(), is(true));
468+
{
469+
final VPackSlice b1 = vpack.get("b1");
470+
assertThat(b1.isInteger(), is(true));
471+
assertThat(b1.getAsByte(), is((byte) 1));
472+
}
473+
{
474+
final VPackSlice b2 = vpack.get("b2");
475+
assertThat(b2.isInteger(), is(true));
476+
assertThat(b2.getAsByte(), is((byte) 100));
477+
}
478+
}
479+
480+
@Test
481+
public void toByte() throws VPackException {
482+
final VPackBuilder builder = new VPackBuilder();
483+
{
484+
builder.add(ValueType.OBJECT);
485+
builder.add("b1", 30); // integer path
486+
builder.add("b2", 4); // short integer path
487+
builder.close();
488+
}
489+
final VPackSlice vpack = builder.slice();
490+
final TestEntityByte entity = new VPack.Builder().build().deserialize(vpack, TestEntityByte.class);
491+
assertThat(entity, is(notNullValue()));
492+
assertThat(entity.b1, is((byte) 30));
493+
assertThat(entity.b2, is(new Byte((byte) 4)));
494+
}
495+
441496
protected static class TestEntityDouble {
442497
private Double d1 = 1.5;
443498
private double d2 = 1.5;

0 commit comments

Comments
 (0)