Skip to content

Deserializing BigDecimal with large number of decimals result in incorrect value #4694

@lnthai2002

Description

@lnthai2002

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

I have a string represents a number with 652 decimal places (-11000.00000...). When reading this string to BigDecimal i end up with a wrong number -1.1000E-648. The simple example is found at https://github.com/lnthai2002/jackson-databind-test/blob/db3e060ac38c555b001d91c549e004a19f9a17c7/src/main/java/App.java

Version Information

2.17.1

Reproduction

<-- Any of the following

  1. Brief code sample/snippet: include here in preformatted/code section
  2. Longer example stored somewhere else (diff repo, snippet), add a link
  3. Textual explanation: include here
    -->
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;

import java.math.BigDecimal;

public class App {
  public static void main (String [] args) {
    JsonNodeFactory f = JsonNodeFactory.withExactBigDecimals(true);
    ObjectMapper mapper = new ObjectMapper();
    mapper.findAndRegisterModules()
        .setNodeFactory(f)
        .configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true)
        .configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
    String str = "-11000.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
    try {
      BigDecimal num = mapper.readValue(str, BigDecimal.class);
      System.out.println(num);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
}

Expected behavior

should print "-11000.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

Additional context

If i switch back to 2.14.1 then i got the correct result

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.17Issues planned at earliest for 2.17has-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions