Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/com/arangodb/velocypack/VPackSlice.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ public BigDecimal getAsBigDecimal() {
return new BigDecimal(getAsString());
} else if (isDouble()) {
return BigDecimal.valueOf(getAsDouble());
} else if (isSmallInt() || isInt()) {
return BigDecimal.valueOf(getAsLong());
} else if (isUInt()) {
return new BigDecimal(NumberUtil.toBigInteger(vpack, start + 1, length()));
} else {
throw new VPackValueTypeException(ValueType.STRING, ValueType.DOUBLE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ public void toDouble() throws VPackException {
protected static class TestEntityBigNumber {
private BigInteger bi = BigInteger.valueOf(1L);
private BigDecimal bd = BigDecimal.valueOf(1.5);
private BigDecimal bdi = BigDecimal.valueOf(1);

public BigInteger getBi() {
return bi;
Expand All @@ -733,6 +734,14 @@ public BigDecimal getBd() {
public void setBd(final BigDecimal bd) {
this.bd = bd;
}

public BigDecimal getBdi() {
return bdi;
}

public void setBdi(final BigDecimal bdi) {
this.bdi = bdi;
}
}

@Test
Expand All @@ -750,6 +759,11 @@ public void fromBigNumbers() throws VPackException {
assertThat(bd.isString(), is(true));
assertThat(bd.getAsBigDecimal(), is(BigDecimal.valueOf(1.5)));
}
{
final VPackSlice bdi = vpack.get("bdi");
assertThat(bdi.isString(), is(true));
assertThat(bdi.getAsBigDecimal(), is(BigDecimal.valueOf(1)));
}
}

@Test
Expand All @@ -759,13 +773,15 @@ public void toBigNumbers() throws VPackException {
builder.add(ValueType.OBJECT);
builder.add("bi", BigInteger.valueOf(2));
builder.add("bd", BigDecimal.valueOf(3.75));
builder.add("bdi", BigDecimal.valueOf(3));
builder.close();
}
final VPackSlice vpack = builder.slice();
final TestEntityBigNumber entity = new VPack.Builder().build().deserialize(vpack, TestEntityBigNumber.class);
assertThat(entity, is(notNullValue()));
assertThat(entity.bi, is(BigInteger.valueOf(2)));
assertThat(entity.bd, is(BigDecimal.valueOf(3.75)));
assertThat(entity.bdi, is(BigDecimal.valueOf(3)));
}

@Test
Expand All @@ -777,6 +793,20 @@ public void bigDecimal() {
assertThat(new VPackBuilder().add(fromString).slice().getAsBigDecimal(), is(fromDouble));
}

@Test
public void deserializeIntAsBigDecimal() {
final int integer = Integer.MAX_VALUE;
final BigDecimal fromInt = BigDecimal.valueOf(integer);
assertThat(new VPackBuilder().add(integer).slice().getAsBigDecimal(), is(fromInt));
}

@Test
public void deserializeLongAsBigDecimal() {
final long lng = Long.MAX_VALUE;
final BigDecimal fromLng = BigDecimal.valueOf(lng);
assertThat(new VPackBuilder().add(lng).slice().getAsBigDecimal(), is(fromLng));
}

protected static class TestEntityArray {
private String[] a1 = { "a", "b", "cd" };
private int[] a2 = { 1, 2, 3, 4, 5 };
Expand Down