Skip to content

Commit

Permalink
Json storage BigDecimal scaling (again)
Browse files Browse the repository at this point in the history
This basically re-introduces the same fallback mechanism
that was already implemented with eclipse-archived#2685 but got lost with

fixes eclipse-archived#3774
Signed-off-by: Simon Kaufmann <[email protected]>
  • Loading branch information
Simon Kaufmann committed Jun 30, 2017
1 parent 1bada87 commit 6995c74
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public void setUp() throws IOException {
@Test
public void allInsertedNumbersAreLoadedAsBigDecimal() {
objectStorage.put("DummyObject", new DummyObject());

DummyObject dummy = (DummyObject) objectStorage.get("DummyObject");

Assert.assertTrue(dummy.myMap.get("testShort") instanceof BigDecimal);
Expand All @@ -51,6 +50,25 @@ public void allInsertedNumbersAreLoadedAsBigDecimal() {
Assert.assertTrue(dummy.myMap.get("testString") instanceof String);
}

@Test
public void testIntegerScale() {
objectStorage.put("DummyObject", new DummyObject());
DummyObject dummy = (DummyObject) objectStorage.get("DummyObject");

Assert.assertEquals(((BigDecimal) dummy.myMap.get("testShort")).scale(), 0);
Assert.assertEquals(((BigDecimal) dummy.myMap.get("testInt")).scale(), 0);
Assert.assertEquals(((BigDecimal) dummy.myMap.get("testLong")).scale(), 0);
Assert.assertEquals(((BigDecimal) dummy.myMap.get("testBigDecimal")).scale(), 0);
}

@Test
public void testTrailingZeroIsOmmitted() {
objectStorage.put("DummyObject", new DummyObject());
DummyObject dummy = (DummyObject) objectStorage.get("DummyObject");

Assert.assertEquals(((BigDecimal) dummy.myMap.get("testBigDecimalWithTrailingZero")).scale(), 0);
}

private class DummyObject {

public Map<String, Object> myMap = new HashMap<String, Object>();
Expand All @@ -64,6 +82,7 @@ public DummyObject() {
myMap.put("testBigDecimal", new BigDecimal(12));
myMap.put("testBoolean", true);
myMap.put("testString", "hello world");
myMap.put("testBigDecimalWithTrailingZero", new BigDecimal("12.0"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public Map<String, Object> deserialize(JsonElement json, Type typeOfT, JsonDeser
JsonElement v = me.getValue();

if (v.isJsonPrimitive() && ((JsonPrimitive) v).isNumber()) {
map.put(k, v.getAsBigDecimal());
if (v.getAsString().endsWith(".0")) {
map.put(k, v.getAsBigDecimal().setScale(0));
} else {
map.put(k, v.getAsBigDecimal());
}
} else {
Object value = context.deserialize(v, Object.class);
map.put(k, value);
Expand Down

0 comments on commit 6995c74

Please sign in to comment.