From 897444ba13b4d9b0688fae3ee3b9e64a04a14fcd Mon Sep 17 00:00:00 2001 From: Thomas Decaux Date: Wed, 21 Nov 2018 22:29:43 +0100 Subject: [PATCH 1/4] Fix int type transformed as BigDecimal value Using with Jackson2 parser, a simple parsing of { "data" : 1} gives a BigDecimal instead an integer. In my case, ``valueClass`` is Object, but the JSON current token is VALUE_NUMBER_INT not FLOAT. --- .../src/main/java/com/google/api/client/json/JsonParser.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/google-http-client/src/main/java/com/google/api/client/json/JsonParser.java b/google-http-client/src/main/java/com/google/api/client/json/JsonParser.java index 75972a16c..582b5cbe6 100644 --- a/google-http-client/src/main/java/com/google/api/client/json/JsonParser.java +++ b/google-http-client/src/main/java/com/google/api/client/json/JsonParser.java @@ -828,6 +828,9 @@ private final Object parseValue( Preconditions.checkArgument( fieldContext == null || fieldContext.getAnnotation(JsonString.class) == null, "number type formatted as a JSON number cannot use @JsonString annotation"); + if (getCurrentToken() == VALUE_NUMBER_INT && (valueClass == null || valueClass.isAssignableFrom(Integer.class))) { + return getIntValue(); + } if (valueClass == null || valueClass.isAssignableFrom(BigDecimal.class)) { return getDecimalValue(); } From 9bd84bd1a596dd5dd9d373e479b804429c6087ad Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Tue, 26 Feb 2019 23:51:49 +0530 Subject: [PATCH 2/4] added unit test --- .../test/json/AbstractJsonFactoryTest.java | 43 ++++++++++++++----- .../google/api/client/json/JsonParser.java | 3 +- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonFactoryTest.java b/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonFactoryTest.java index 8bf518109..38044b157 100644 --- a/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonFactoryTest.java +++ b/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonFactoryTest.java @@ -482,6 +482,8 @@ public static class MapOfMapType { static final String MAP_TYPE = "{\"value\":[{\"map1\":{\"k1\":1,\"k2\":2},\"map2\":{\"kk1\":3,\"kk2\":4}}]}"; + static final String BIGDECIMAL_MAP_TYPE = + "{\"value\":[{\"map1\":{\"k1\":1.14566,\"k2\":2.14786},\"map2\":{\"kk1\":3.29786,\"kk2\":4.69786}}]}"; public void testParser_mapType() throws Exception { // parse @@ -510,16 +512,35 @@ public void testParser_hashmapForMapType() throws Exception { parser = factory.createJsonParser(MAP_TYPE); parser.nextToken(); @SuppressWarnings("unchecked") - HashMap>>> result = + HashMap>>> result = parser.parse(HashMap.class); // serialize assertEquals(MAP_TYPE, factory.toString(result)); // check parsed result + ArrayList>> value = result.get("value"); + ArrayMap> firstMap = value.get(0); + ArrayMap map1 = firstMap.get("map1"); + Integer integer = map1.get("k1"); + assertEquals(1, integer.intValue()); + } + + public void testParser_hashmapForMapTypeWithBigDecimal() throws Exception { + // parse + JsonFactory factory = newFactory(); + JsonParser parser; + parser = factory.createJsonParser(BIGDECIMAL_MAP_TYPE); + parser.nextToken(); + @SuppressWarnings("unchecked") + HashMap>>> result = + parser.parse(HashMap.class); + // serialize + assertEquals(BIGDECIMAL_MAP_TYPE, factory.toString(result)); + // check parsed result ArrayList>> value = result.get("value"); ArrayMap> firstMap = value.get(0); ArrayMap map1 = firstMap.get("map1"); - BigDecimal integer = map1.get("k1"); - assertEquals(1, integer.intValue()); + BigDecimal bigDecimal = map1.get("k1"); + assertEquals(BigDecimal.valueOf(1.14566).setScale(5), bigDecimal); } public static class WildCardTypes { @@ -547,8 +568,8 @@ public void testParser_wildCardType() throws Exception { assertEquals(WILDCARD_TYPE, factory.toString(result)); // check parsed result Collection[] simple = result.simple; - ArrayList wildcard = (ArrayList) simple[0]; - BigDecimal wildcardFirstValue = wildcard.get(0); + ArrayList wildcard = (ArrayList) simple[0]; + Integer wildcardFirstValue = wildcard.get(0); assertEquals(1, wildcardFirstValue.intValue()); Collection[] upper = result.upper; ArrayList wildcardUpper = (ArrayList) upper[0]; @@ -558,8 +579,8 @@ public void testParser_wildCardType() throws Exception { ArrayList wildcardLower = (ArrayList) lower[0]; Integer wildcardFirstValueLower = wildcardLower.get(0); assertEquals(1, wildcardFirstValueLower.intValue()); - Map map = (Map) result.map; - BigDecimal mapValue = map.get("v"); + Map map = (Map) result.map; + Integer mapValue = map.get("v"); assertEquals(1, mapValue.intValue()); Map mapUpper = (Map) result.mapUpper; Integer mapUpperValue = mapUpper.get("v"); @@ -771,16 +792,16 @@ public void testParser_treemapForTypeVariableType() throws Exception { ArrayList arr = (ArrayList) result.get("arr"); assertEquals(2, arr.size()); assertEquals(Data.nullOf(Object.class), arr.get(0)); - ArrayList subArr = (ArrayList) arr.get(1); + ArrayList subArr = (ArrayList) arr.get(1); assertEquals(2, subArr.size()); assertEquals(Data.nullOf(Object.class), subArr.get(0)); - BigDecimal arrValue = subArr.get(1); + Integer arrValue = subArr.get(1); assertEquals(1, arrValue.intValue()); // null value Object nullValue = result.get("nullValue"); assertEquals(Data.nullOf(Object.class), nullValue); // value - BigDecimal value = (BigDecimal) result.get("value"); + Integer value = (Integer) result.get("value"); assertEquals(1, value.intValue()); } @@ -1519,7 +1540,7 @@ public void testParser_heterogeneousSchema_genericJson() throws Exception { assertEquals(4, dog.numberOfLegs); assertEquals(3, ((DogGenericJson) dog).tricksKnown); assertEquals("this is not being used!", dog.get("unusedInfo")); - BigDecimal foo = ((BigDecimal) ((ArrayMap) dog.get("unused")).get("foo")); + Integer foo = ((Integer) ((ArrayMap) dog.get("unused")).get("foo")); assertEquals(200, foo.intValue()); } diff --git a/google-http-client/src/main/java/com/google/api/client/json/JsonParser.java b/google-http-client/src/main/java/com/google/api/client/json/JsonParser.java index 582b5cbe6..44bec3f51 100644 --- a/google-http-client/src/main/java/com/google/api/client/json/JsonParser.java +++ b/google-http-client/src/main/java/com/google/api/client/json/JsonParser.java @@ -828,7 +828,8 @@ private final Object parseValue( Preconditions.checkArgument( fieldContext == null || fieldContext.getAnnotation(JsonString.class) == null, "number type formatted as a JSON number cannot use @JsonString annotation"); - if (getCurrentToken() == VALUE_NUMBER_INT && (valueClass == null || valueClass.isAssignableFrom(Integer.class))) { + if (getCurrentToken() == JsonToken.VALUE_NUMBER_INT + && (valueClass == null || valueClass.isAssignableFrom(Integer.class))) { return getIntValue(); } if (valueClass == null || valueClass.isAssignableFrom(BigDecimal.class)) { From 9051730feebb7a31c3ad1a406cabb1687d86054a Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Mon, 4 Mar 2019 22:46:04 +0530 Subject: [PATCH 3/4] Fix Single classes --- .../google/api/client/test/json/AbstractJsonFactoryTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonFactoryTest.java b/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonFactoryTest.java index 38044b157..19e7f7d1b 100644 --- a/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonFactoryTest.java +++ b/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonFactoryTest.java @@ -483,7 +483,7 @@ public static class MapOfMapType { static final String MAP_TYPE = "{\"value\":[{\"map1\":{\"k1\":1,\"k2\":2},\"map2\":{\"kk1\":3,\"kk2\":4}}]}"; static final String BIGDECIMAL_MAP_TYPE = - "{\"value\":[{\"map1\":{\"k1\":1.14566,\"k2\":2.14786},\"map2\":{\"kk1\":3.29786,\"kk2\":4.69786}}]}"; + "{\"value\":[{\"map1\":{\"k1\":1.14566,\"k2\":2.14786},\"map2\":{\"kk1\":3.29786,\"kk2\":4.69786}}]}"; public void testParser_mapType() throws Exception { // parse From 6883be186724aea7fe28f690125f4493f6fe9cdf Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Fri, 15 Mar 2019 11:49:42 +0530 Subject: [PATCH 4/4] Fix Test failing. --- .../google/api/client/test/json/AbstractJsonFactoryTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonFactoryTest.java b/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonFactoryTest.java index 19e7f7d1b..1f11dfd38 100644 --- a/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonFactoryTest.java +++ b/google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonFactoryTest.java @@ -483,7 +483,7 @@ public static class MapOfMapType { static final String MAP_TYPE = "{\"value\":[{\"map1\":{\"k1\":1,\"k2\":2},\"map2\":{\"kk1\":3,\"kk2\":4}}]}"; static final String BIGDECIMAL_MAP_TYPE = - "{\"value\":[{\"map1\":{\"k1\":1.14566,\"k2\":2.14786},\"map2\":{\"kk1\":3.29786,\"kk2\":4.69786}}]}"; + "{\"value\":[{\"map1\":{\"k1\":1.14566,\"k2\":2.14},\"map2\":{\"kk1\":3.29,\"kk2\":4.69}}]}"; public void testParser_mapType() throws Exception { // parse