Skip to content

Commit da597e8

Browse files
ebuildysduskis
authored andcommitted
Fix int type transformed as BigDecimal value when parsing as Map (#529)
* 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. * added unit test * Fix Single classes * Fix Test failing.
1 parent 5e1e74f commit da597e8

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonFactoryTest.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ public static class MapOfMapType {
482482

483483
static final String MAP_TYPE =
484484
"{\"value\":[{\"map1\":{\"k1\":1,\"k2\":2},\"map2\":{\"kk1\":3,\"kk2\":4}}]}";
485+
static final String BIGDECIMAL_MAP_TYPE =
486+
"{\"value\":[{\"map1\":{\"k1\":1.14566,\"k2\":2.14},\"map2\":{\"kk1\":3.29,\"kk2\":4.69}}]}";
485487

486488
public void testParser_mapType() throws Exception {
487489
// parse
@@ -510,16 +512,35 @@ public void testParser_hashmapForMapType() throws Exception {
510512
parser = factory.createJsonParser(MAP_TYPE);
511513
parser.nextToken();
512514
@SuppressWarnings("unchecked")
513-
HashMap<String, ArrayList<ArrayMap<String, ArrayMap<String, BigDecimal>>>> result =
515+
HashMap<String, ArrayList<ArrayMap<String, ArrayMap<String, Integer>>>> result =
514516
parser.parse(HashMap.class);
515517
// serialize
516518
assertEquals(MAP_TYPE, factory.toString(result));
517519
// check parsed result
520+
ArrayList<ArrayMap<String, ArrayMap<String, Integer>>> value = result.get("value");
521+
ArrayMap<String, ArrayMap<String, Integer>> firstMap = value.get(0);
522+
ArrayMap<String, Integer> map1 = firstMap.get("map1");
523+
Integer integer = map1.get("k1");
524+
assertEquals(1, integer.intValue());
525+
}
526+
527+
public void testParser_hashmapForMapTypeWithBigDecimal() throws Exception {
528+
// parse
529+
JsonFactory factory = newFactory();
530+
JsonParser parser;
531+
parser = factory.createJsonParser(BIGDECIMAL_MAP_TYPE);
532+
parser.nextToken();
533+
@SuppressWarnings("unchecked")
534+
HashMap<String, ArrayList<ArrayMap<String, ArrayMap<String, BigDecimal>>>> result =
535+
parser.parse(HashMap.class);
536+
// serialize
537+
assertEquals(BIGDECIMAL_MAP_TYPE, factory.toString(result));
538+
// check parsed result
518539
ArrayList<ArrayMap<String, ArrayMap<String, BigDecimal>>> value = result.get("value");
519540
ArrayMap<String, ArrayMap<String, BigDecimal>> firstMap = value.get(0);
520541
ArrayMap<String, BigDecimal> map1 = firstMap.get("map1");
521-
BigDecimal integer = map1.get("k1");
522-
assertEquals(1, integer.intValue());
542+
BigDecimal bigDecimal = map1.get("k1");
543+
assertEquals(BigDecimal.valueOf(1.14566).setScale(5), bigDecimal);
523544
}
524545

525546
public static class WildCardTypes {
@@ -547,8 +568,8 @@ public void testParser_wildCardType() throws Exception {
547568
assertEquals(WILDCARD_TYPE, factory.toString(result));
548569
// check parsed result
549570
Collection<?>[] simple = result.simple;
550-
ArrayList<BigDecimal> wildcard = (ArrayList<BigDecimal>) simple[0];
551-
BigDecimal wildcardFirstValue = wildcard.get(0);
571+
ArrayList<Integer> wildcard = (ArrayList<Integer>) simple[0];
572+
Integer wildcardFirstValue = wildcard.get(0);
552573
assertEquals(1, wildcardFirstValue.intValue());
553574
Collection<? extends Integer>[] upper = result.upper;
554575
ArrayList<Integer> wildcardUpper = (ArrayList<Integer>) upper[0];
@@ -558,8 +579,8 @@ public void testParser_wildCardType() throws Exception {
558579
ArrayList<Integer> wildcardLower = (ArrayList<Integer>) lower[0];
559580
Integer wildcardFirstValueLower = wildcardLower.get(0);
560581
assertEquals(1, wildcardFirstValueLower.intValue());
561-
Map<String, BigDecimal> map = (Map<String, BigDecimal>) result.map;
562-
BigDecimal mapValue = map.get("v");
582+
Map<String, Integer> map = (Map<String, Integer>) result.map;
583+
Integer mapValue = map.get("v");
563584
assertEquals(1, mapValue.intValue());
564585
Map<String, Integer> mapUpper = (Map<String, Integer>) result.mapUpper;
565586
Integer mapUpperValue = mapUpper.get("v");
@@ -771,16 +792,16 @@ public void testParser_treemapForTypeVariableType() throws Exception {
771792
ArrayList<Object> arr = (ArrayList<Object>) result.get("arr");
772793
assertEquals(2, arr.size());
773794
assertEquals(Data.nullOf(Object.class), arr.get(0));
774-
ArrayList<BigDecimal> subArr = (ArrayList<BigDecimal>) arr.get(1);
795+
ArrayList<Integer> subArr = (ArrayList<Integer>) arr.get(1);
775796
assertEquals(2, subArr.size());
776797
assertEquals(Data.nullOf(Object.class), subArr.get(0));
777-
BigDecimal arrValue = subArr.get(1);
798+
Integer arrValue = subArr.get(1);
778799
assertEquals(1, arrValue.intValue());
779800
// null value
780801
Object nullValue = result.get("nullValue");
781802
assertEquals(Data.nullOf(Object.class), nullValue);
782803
// value
783-
BigDecimal value = (BigDecimal) result.get("value");
804+
Integer value = (Integer) result.get("value");
784805
assertEquals(1, value.intValue());
785806
}
786807

@@ -1519,7 +1540,7 @@ public void testParser_heterogeneousSchema_genericJson() throws Exception {
15191540
assertEquals(4, dog.numberOfLegs);
15201541
assertEquals(3, ((DogGenericJson) dog).tricksKnown);
15211542
assertEquals("this is not being used!", dog.get("unusedInfo"));
1522-
BigDecimal foo = ((BigDecimal) ((ArrayMap<String, Object>) dog.get("unused")).get("foo"));
1543+
Integer foo = ((Integer) ((ArrayMap<String, Object>) dog.get("unused")).get("foo"));
15231544
assertEquals(200, foo.intValue());
15241545
}
15251546

google-http-client/src/main/java/com/google/api/client/json/JsonParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,10 @@ private final Object parseValue(
828828
Preconditions.checkArgument(
829829
fieldContext == null || fieldContext.getAnnotation(JsonString.class) == null,
830830
"number type formatted as a JSON number cannot use @JsonString annotation");
831+
if (getCurrentToken() == JsonToken.VALUE_NUMBER_INT
832+
&& (valueClass == null || valueClass.isAssignableFrom(Integer.class))) {
833+
return getIntValue();
834+
}
831835
if (valueClass == null || valueClass.isAssignableFrom(BigDecimal.class)) {
832836
return getDecimalValue();
833837
}

0 commit comments

Comments
 (0)