diff options
author | Ram Krishna Verma <ram.krishna.verma@est.tech> | 2019-07-18 11:14:22 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-07-18 11:14:22 +0000 |
commit | f2b5c0111e65d1033157da0a1dc09dd3b681cce9 (patch) | |
tree | 92e66068694272ddf15e518fbcd989802a3c0ba4 /utils | |
parent | 3f54136a63ca29ac2401c2b22a7a66b95faebae3 (diff) | |
parent | 37f4fef730a59fd5230b90e60039b05ed5e4d88b (diff) |
Merge "Convert double to int when decoding via gson"
Diffstat (limited to 'utils')
-rw-r--r-- | utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java | 22 | ||||
-rw-r--r-- | utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java | 9 |
2 files changed, 29 insertions, 2 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java index 1c65be82..80eddb86 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java @@ -38,6 +38,7 @@ import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.nio.charset.StandardCharsets; +import org.onap.policy.common.gson.DoubleConverter; import org.onap.policy.common.gson.MapDoubleAdapterFactory; /** @@ -238,7 +239,7 @@ public class StandardCoder implements Coder { * @return the object represented by the given json string */ protected <T> T fromJson(String json, Class<T> clazz) { - return GSON.fromJson(json, clazz); + return convertFromDouble(clazz, GSON.fromJson(json, clazz)); } /** @@ -249,7 +250,24 @@ public class StandardCoder implements Coder { * @return the object represented by the given json string */ protected <T> T fromJson(Reader source, Class<T> clazz) { - return GSON.fromJson(source, clazz); + return convertFromDouble(clazz, GSON.fromJson(source, clazz)); + } + + /** + * Converts a value from Double to Integer/Long, walking the value's contents if it's + * a List/Map. Only applies if the specified class refers to the Object class. + * Otherwise, it leaves the value unchanged. + * + * @param clazz class of object to be decoded + * @param value value to be converted + * @return the converted value + */ + private <T> T convertFromDouble(Class<T> clazz, T value) { + if (clazz != Object.class) { + return value; + } + + return clazz.cast(DoubleConverter.convertFromDouble(value)); } /** diff --git a/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java b/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java index e8e3bb60..2992933f 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java @@ -201,6 +201,15 @@ public class StandardCoderTest { } @Test + public void testConvertFromDouble() throws Exception { + String text = "[listA, {keyA=100}, 200]"; + assertEquals(text, coder.decode(text, Object.class).toString()); + + text = "{keyB=200}"; + assertEquals(text, coder.decode(text, Object.class).toString()); + } + + @Test public void testToStandard() throws Exception { MyObject obj = new MyObject(); obj.abc = "xyz"; |