-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Scale of deserialized BigDecimal are not always the same #632
Comments
Odd. It'd seem scales should be the same. There isn't much support to change handling of |
Reproduced with jackson 2.5.1
Then us the deserializer like this to make the test pass
|
I did the same kind of workarround using a JsonDeserializer but I used a module to make it global : public class BigDecimalMoneyDeserializer extends JsonDeserializer<BigDecimal> {
@Override
public BigDecimal deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return jp.getDecimalValue().setScale(2, BigDecimal.ROUND_HALF_UP);
}
} public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper() {
super();
SimpleModule testModule = new SimpleModule("my-module", new Version(1, 0, 0, null))
.addDeserializer(BigDecimal.class, new BigDecimalMoneyDeserializer());
this.registerModule(testModule);
}
} |
I'd stumbled on this when poking around before submitting #965, which seemed different, but due to |
Patch 2.6.3 was just released, and it does contain one fix to |
Assuming this is fixed. |
@azzoti that was exactly what I'm looking for, and it work for me. Thanks! |
@cowtowncoder — I just ran across this in my own code. I've confirmed that the test case in the ticket fails in the latest version of Jackson: 2.11.1. Here's the JUnit 5 test I wrote that verifies this same functionality: import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class BigDecimalTrailingZeroesTest {
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void bigDecimalTrailingZeroes() throws JsonProcessingException {
// Passes
assertEquals(new BigDecimal("5.00"),
objectMapper.readValue("{\"value\": 5.00}", BigDecimalHolder.class).getValue());
}
@Test
public void unwrappedBigDecimalTrailingZeroes() throws JsonProcessingException {
// org.opentest4j.AssertionFailedError:
// Expected :5.00
// Actual :5.0
assertEquals(new BigDecimal("5.00"),
objectMapper.readValue("{\"value\": 5.00}", NestedBigDecimalHolder.class).getHolder().getValue());
}
private static class BigDecimalHolder {
private BigDecimal value;
public BigDecimal getValue() {
return value;
}
public void setValue(BigDecimal value) {
this.value = value;
}
}
private static class NestedBigDecimalHolder {
@JsonUnwrapped
private BigDecimalHolder holder;
public BigDecimalHolder getHolder() {
return holder;
}
public void setHolder(BigDecimalHolder holder) {
this.holder = holder;
}
}
} |
@mjustin Please file a new issue (with possible ref to this as background); I do not usually re-open closed issues as that complicates release notes significantly. |
Hi,
When I use the annotation @JsonUnwrapped to materialize a component bean, the scale of the BigDecimal deserialized is different. I've written a test case below to reproduce the issue.
Is there a way to control the scale of BigDecimal? Do I have to configure a custom deserializer?
(Test case reproduced with Jacskon 2.4.0 and 1.9.13 on Java 6)
Thanks,
Benoit
The text was updated successfully, but these errors were encountered: