From 6995c74918f6789a381956181367bfd29a2495f5 Mon Sep 17 00:00:00 2001 From: Simon Kaufmann Date: Fri, 30 Jun 2017 17:19:05 +0200 Subject: [PATCH] Json storage BigDecimal scaling (again) This basically re-introduces the same fallback mechanism that was already implemented with #2685 but got lost with fixes #3774 Signed-off-by: Simon Kaufmann --- .../storage/json/test/JSonStorageTest.java | 21 ++++++++++++++++++- .../json/StringObjectMapDeserializer.java | 6 +++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/bundles/storage/org.eclipse.smarthome.storage.json.test/src/main/java/org/eclipse/smarthome/storage/json/test/JSonStorageTest.java b/bundles/storage/org.eclipse.smarthome.storage.json.test/src/main/java/org/eclipse/smarthome/storage/json/test/JSonStorageTest.java index 9cc198d6e40..5cf8954b71c 100644 --- a/bundles/storage/org.eclipse.smarthome.storage.json.test/src/main/java/org/eclipse/smarthome/storage/json/test/JSonStorageTest.java +++ b/bundles/storage/org.eclipse.smarthome.storage.json.test/src/main/java/org/eclipse/smarthome/storage/json/test/JSonStorageTest.java @@ -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); @@ -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 myMap = new HashMap(); @@ -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")); } } diff --git a/bundles/storage/org.eclipse.smarthome.storage.json/src/main/java/org/eclipse/smarthome/storage/json/StringObjectMapDeserializer.java b/bundles/storage/org.eclipse.smarthome.storage.json/src/main/java/org/eclipse/smarthome/storage/json/StringObjectMapDeserializer.java index 3f7799c0c13..ec448e83827 100644 --- a/bundles/storage/org.eclipse.smarthome.storage.json/src/main/java/org/eclipse/smarthome/storage/json/StringObjectMapDeserializer.java +++ b/bundles/storage/org.eclipse.smarthome.storage.json/src/main/java/org/eclipse/smarthome/storage/json/StringObjectMapDeserializer.java @@ -39,7 +39,11 @@ public Map 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);