Skip to content

Commit

Permalink
Fixed #2784 (and #2644)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jul 10, 2020
1 parent 3dec03c commit 1369c51
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 97 deletions.
5 changes: 5 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project: jackson-databind
(reported by Xiang Z)
#2283: `JsonProperty.Access.READ_ONLY` fails with collections when a property name is specified
(reported by Yona A)
#2644: `BigDecimal` precision not retained for polymorphic deserialization
(reported by rost5000@github)
#2675: Support use of `Void` valued properties (`MapperFeature.ALLOW_VOID_VALUED_PROPERTIES`)
#2683: Explicitly fail (de)serialization of `java.time.*` types in absence of
registered custom (de)serializers
Expand All @@ -39,6 +41,9 @@ Project: jackson-databind
#2751: Add `ValueInstantiator.createContextual(...)
#2776: Explicitly fail (de)serialization of `org.joda.time.*` types in absence of registered
custom (de)serializers
#2784: Trailing zeros are stripped when deserializing BigDecimal values inside a
@JsonUnwrapped property
(reported by mjustin@github)
- Add `BeanDeserializerBase.isCaseInsensitive()`
- Some refactoring of `CollectionDeserializer` to solve CSV array handling issues

Expand Down
14 changes: 4 additions & 10 deletions src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1228,16 +1228,10 @@ private void _copyBufferValue(JsonParser p, JsonToken t) throws IOException
if (_forceBigDecimal) {
writeNumber(p.getDecimalValue());
} else {
switch (p.getNumberType()) {
case BIG_DECIMAL:
writeNumber(p.getDecimalValue());
break;
case FLOAT:
writeNumber(p.getFloatValue());
break;
default:
writeNumber(p.getDoubleValue());
}
// 09-Jul-2020, tatu: Used to just copy using most optimal method, but
// issues like [databind#2644] force to use exact, not optimal type
final Number n = p.getNumberValueExact();
_appendValue(JsonToken.VALUE_NUMBER_FLOAT, n);
}
break;
case VALUE_TRUE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.*;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
Expand Down Expand Up @@ -37,6 +40,42 @@ static class MyBeanValue {
public MyBeanValue(BigDecimal d) { this.decimal = d; }
}

// [databind#2644]
static class NodeRoot2644 {
public String type;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
@JsonSubTypes(value = {
@JsonSubTypes.Type(value = NodeParent2644.class, name = "NodeParent")
})
public Node2644 node;
}

public static class NodeParent2644 extends Node2644 { }

public static abstract class Node2644 {
@JsonProperty("amount")
BigDecimal val;

public BigDecimal getVal() {
return val;
}

public void setVal(BigDecimal val) {
this.val = val;
}
}

// [databind#2784]
static class BigDecimalHolder2784 {
public BigDecimal value;
}

static class NestedBigDecimalHolder2784 {
@JsonUnwrapped
public BigDecimalHolder2784 holder;
}

/*
/**********************************************************************
/* Helper classes, serializers/deserializers/resolvers
Expand Down Expand Up @@ -299,4 +338,28 @@ public void testForceIntsToLongs() throws Exception
}
assertEquals(42, node.asInt());
}

// [databind#2644]
public void testBigDecimalSubtypes() throws Exception
{
ObjectMapper mapper = jsonMapperBuilder()
.registerSubtypes(NodeParent2644.class)
.build();
NodeRoot2644 root = mapper.readValue(
"{\"type\": \"NodeParent\",\"node\": {\"amount\": 9999999999999999.99} }",
NodeRoot2644.class
);

assertEquals(new BigDecimal("9999999999999999.99"), root.node.getVal());
}

// [databind#2784]
public void testBigDecimalUnwrapped() throws Exception
{
final ObjectMapper mapper = newJsonMapper();
// mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
final String JSON = "{\"value\": 5.00}";
NestedBigDecimalHolder2784 result = mapper.readValue(JSON, NestedBigDecimalHolder2784.class);
assertEquals(new BigDecimal("5.00"), result.holder.value);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void testBigDecimalCoercion() throws Exception
final JsonNode jsonNode = MAPPER.reader()
.with(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
.readTree("7976931348623157e309");
assertTrue(jsonNode.isBigDecimal());
assertTrue("Expected DecimalNode, got: "+jsonNode.getClass().getName()+": "+jsonNode, jsonNode.isBigDecimal());
// the following fails with NumberFormatException, because jsonNode is a DoubleNode with a value of POSITIVE_INFINITY
// Assert.assertTrue(jsonNode.decimalValue().compareTo(new BigDecimal("7976931348623157e309")) == 0);
}
Expand Down

0 comments on commit 1369c51

Please sign in to comment.