Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix int type transformed as BigDecimal value when parsing as Map #529

Merged
merged 4 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.14},\"map2\":{\"kk1\":3.29,\"kk2\":4.69}}]}";

public void testParser_mapType() throws Exception {
// parse
Expand Down Expand Up @@ -510,16 +512,35 @@ public void testParser_hashmapForMapType() throws Exception {
parser = factory.createJsonParser(MAP_TYPE);
parser.nextToken();
@SuppressWarnings("unchecked")
HashMap<String, ArrayList<ArrayMap<String, ArrayMap<String, BigDecimal>>>> result =
HashMap<String, ArrayList<ArrayMap<String, ArrayMap<String, Integer>>>> result =
parser.parse(HashMap.class);
// serialize
assertEquals(MAP_TYPE, factory.toString(result));
// check parsed result
ArrayList<ArrayMap<String, ArrayMap<String, Integer>>> value = result.get("value");
ArrayMap<String, ArrayMap<String, Integer>> firstMap = value.get(0);
ArrayMap<String, Integer> 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<String, ArrayList<ArrayMap<String, ArrayMap<String, BigDecimal>>>> result =
parser.parse(HashMap.class);
// serialize
assertEquals(BIGDECIMAL_MAP_TYPE, factory.toString(result));
// check parsed result
ArrayList<ArrayMap<String, ArrayMap<String, BigDecimal>>> value = result.get("value");
ArrayMap<String, ArrayMap<String, BigDecimal>> firstMap = value.get(0);
ArrayMap<String, BigDecimal> 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 {
Expand Down Expand Up @@ -547,8 +568,8 @@ public void testParser_wildCardType() throws Exception {
assertEquals(WILDCARD_TYPE, factory.toString(result));
// check parsed result
Collection<?>[] simple = result.simple;
ArrayList<BigDecimal> wildcard = (ArrayList<BigDecimal>) simple[0];
BigDecimal wildcardFirstValue = wildcard.get(0);
ArrayList<Integer> wildcard = (ArrayList<Integer>) simple[0];
Integer wildcardFirstValue = wildcard.get(0);
assertEquals(1, wildcardFirstValue.intValue());
Collection<? extends Integer>[] upper = result.upper;
ArrayList<Integer> wildcardUpper = (ArrayList<Integer>) upper[0];
Expand All @@ -558,8 +579,8 @@ public void testParser_wildCardType() throws Exception {
ArrayList<Integer> wildcardLower = (ArrayList<Integer>) lower[0];
Integer wildcardFirstValueLower = wildcardLower.get(0);
assertEquals(1, wildcardFirstValueLower.intValue());
Map<String, BigDecimal> map = (Map<String, BigDecimal>) result.map;
BigDecimal mapValue = map.get("v");
Map<String, Integer> map = (Map<String, Integer>) result.map;
Integer mapValue = map.get("v");
assertEquals(1, mapValue.intValue());
Map<String, Integer> mapUpper = (Map<String, Integer>) result.mapUpper;
Integer mapUpperValue = mapUpper.get("v");
Expand Down Expand Up @@ -771,16 +792,16 @@ public void testParser_treemapForTypeVariableType() throws Exception {
ArrayList<Object> arr = (ArrayList<Object>) result.get("arr");
assertEquals(2, arr.size());
assertEquals(Data.nullOf(Object.class), arr.get(0));
ArrayList<BigDecimal> subArr = (ArrayList<BigDecimal>) arr.get(1);
ArrayList<Integer> subArr = (ArrayList<Integer>) 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());
}

Expand Down Expand Up @@ -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<String, Object>) dog.get("unused")).get("foo"));
Integer foo = ((Integer) ((ArrayMap<String, Object>) dog.get("unused")).get("foo"));
assertEquals(200, foo.intValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,10 @@ 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() == JsonToken.VALUE_NUMBER_INT
&& (valueClass == null || valueClass.isAssignableFrom(Integer.class))) {
return getIntValue();
}
if (valueClass == null || valueClass.isAssignableFrom(BigDecimal.class)) {
return getDecimalValue();
}
Expand Down