question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserializing BigDecimal using JsonNode loses precision

See original GitHub issue

Using 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:closed
  • Created 5 years ago
  • Comments:32 (8 by maintainers)

github_iconTop GitHub Comments

55reactions
cowtowncodercommented, Mar 4, 2019

@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.

14reactions
CowboyCodingRulescommented, Mar 6, 2019

@cowtowncoder Do you need a hug?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found