Skip to content

Commit 7f8a6da

Browse files
Support deserializing ints as BigDecimals. (#27)
Co-authored-by: James Bench <[email protected]>
1 parent a43a92d commit 7f8a6da

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ public BigDecimal getAsBigDecimal() {
289289
return new BigDecimal(getAsString());
290290
} else if (isDouble()) {
291291
return BigDecimal.valueOf(getAsDouble());
292+
} else if (isSmallInt() || isInt()) {
293+
return BigDecimal.valueOf(getAsLong());
294+
} else if (isUInt()) {
295+
return new BigDecimal(NumberUtil.toBigInteger(vpack, start + 1, length()));
292296
} else {
293297
throw new VPackValueTypeException(ValueType.STRING, ValueType.DOUBLE);
294298
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ public void toDouble() throws VPackException {
698698
protected static class TestEntityBigNumber {
699699
private BigInteger bi = BigInteger.valueOf(1L);
700700
private BigDecimal bd = BigDecimal.valueOf(1.5);
701+
private BigDecimal bdi = BigDecimal.valueOf(1);
701702

702703
public BigInteger getBi() {
703704
return bi;
@@ -714,6 +715,14 @@ public BigDecimal getBd() {
714715
public void setBd(final BigDecimal bd) {
715716
this.bd = bd;
716717
}
718+
719+
public BigDecimal getBdi() {
720+
return bdi;
721+
}
722+
723+
public void setBdi(final BigDecimal bdi) {
724+
this.bdi = bdi;
725+
}
717726
}
718727

719728
@Test
@@ -731,6 +740,11 @@ public void fromBigNumbers() throws VPackException {
731740
assertThat(bd.isString(), is(true));
732741
assertThat(bd.getAsBigDecimal(), is(BigDecimal.valueOf(1.5)));
733742
}
743+
{
744+
final VPackSlice bdi = vpack.get("bdi");
745+
assertThat(bdi.isString(), is(true));
746+
assertThat(bdi.getAsBigDecimal(), is(BigDecimal.valueOf(1)));
747+
}
734748
}
735749

736750
@Test
@@ -740,13 +754,15 @@ public void toBigNumbers() throws VPackException {
740754
builder.add(ValueType.OBJECT);
741755
builder.add("bi", BigInteger.valueOf(2));
742756
builder.add("bd", BigDecimal.valueOf(3.75));
757+
builder.add("bdi", BigDecimal.valueOf(3));
743758
builder.close();
744759
}
745760
final VPackSlice vpack = builder.slice();
746761
final TestEntityBigNumber entity = new VPack.Builder().build().deserialize(vpack, TestEntityBigNumber.class);
747762
assertThat(entity, is(notNullValue()));
748763
assertThat(entity.bi, is(BigInteger.valueOf(2)));
749764
assertThat(entity.bd, is(BigDecimal.valueOf(3.75)));
765+
assertThat(entity.bdi, is(BigDecimal.valueOf(3)));
750766
}
751767

752768
@Test
@@ -758,6 +774,20 @@ public void bigDecimal() {
758774
assertThat(new VPackBuilder().add(fromString).slice().getAsBigDecimal(), is(fromDouble));
759775
}
760776

777+
@Test
778+
public void deserializeIntAsBigDecimal() {
779+
final int integer = Integer.MAX_VALUE;
780+
final BigDecimal fromInt = BigDecimal.valueOf(integer);
781+
assertThat(new VPackBuilder().add(integer).slice().getAsBigDecimal(), is(fromInt));
782+
}
783+
784+
@Test
785+
public void deserializeLongAsBigDecimal() {
786+
final long lng = Long.MAX_VALUE;
787+
final BigDecimal fromLng = BigDecimal.valueOf(lng);
788+
assertThat(new VPackBuilder().add(lng).slice().getAsBigDecimal(), is(fromLng));
789+
}
790+
761791
protected static class TestEntityArray {
762792
private String[] a1 = { "a", "b", "cd" };
763793
private int[] a2 = { 1, 2, 3, 4, 5 };

0 commit comments

Comments
 (0)