Deserializing BigDecimal using JsonNode loses precision
See original GitHub issueUsing jackson-databind 2.9.6
There seems to be an issue parsing BigDecimals as JsonNodes where it is forced to pass though an intermediate phase as a double and precision is destroyed
The code below illustrates the issue - on the original BigDecimal the precision is 2, after deserializing the precision is 1. If the custom Deserializer is disabled then the test passes.
import java.io.IOException;
import java.math.BigDecimal;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
class SimpleTest {
@Test
void test() throws JsonParseException, JsonMappingException, IOException {
TestClass z = new TestClass();
z.setA(new BigDecimal("0.0060"));
ObjectMapper m = new ObjectMapper();
m.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
String s = m.writeValueAsString(z);
System.out.println(s);
TestClass readValue = m.readValue(s, TestClass.class);
System.out.println(readValue.getA());
assertEquals(z.a.precision(), readValue.a.precision());
}
static class DeSer extends StdDeserializer<TestClass> {
public DeSer() {
super(TestClass.class);
}
@Override
public TestClass deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
System.out.println(node.toString());
ObjectMapper m = new ObjectMapper();
BigDecimal bd = m.treeToValue(node.get("a"), BigDecimal.class);
TestClass testClass = new TestClass();
testClass.setA(bd);
return testClass;
}
}
@JsonDeserialize(using = DeSer.class)
static class TestClass {
BigDecimal a;
public BigDecimal getA() {
return a;
}
public void setA(BigDecimal a) {
this.a = a;
}
}
}```
Issue Analytics
- State:
- Created 5 years ago
- Comments:32 (8 by maintainers)
Top Results From Across the Web
Java to Jackson JSON serialization: Money fields
You can use a custom serializer at your money field. Here's an example with a MoneyBean. ... A BigDecimal is now printed in...
Read more >Jackson: set BigDecimal as type for JSON deserialization of ...
In the JSON serialization of a decimal number, Jackson creates an object of the exact actual type used when the field is defined...
Read more >DeserializationFeature (jackson-databind 2.6.0 API) - FasterXML
Feature that determines whether JSON floating point numbers are to be deserialized into BigDecimal s if only generic type description (either Object or...
Read more >While Converting Java String Which Is Having Big Decimal In ...
Using jacksondatabind 2.9.6 There seems to be an issue parsing Deserializing BigDecimal using JsonNode loses precision #2087 Honestly it just sounds like ...
Read more >How to format BigDecimal as "plain text" when serializing a ...
JsonNode.toString() delegates this operation to DecimalNode.asText() which in turn calls BigDecimal.toString(), which uses the scientific notation... My custom ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ymenager Please do tell me how I use my free time. I really like hearing FUCKING DEMANDS FROM YOU, A PERSON I DON’T EVEN KNOW.
Actually, fuck you. Go away.
@cowtowncoder Do you need a hug?