Skip to content
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

BigDecimal deserialization issue when using @JsonCreator #4917

Closed
1 task done
dbachdev opened this issue Jan 22, 2025 · 4 comments
Closed
1 task done

BigDecimal deserialization issue when using @JsonCreator #4917

dbachdev opened this issue Jan 22, 2025 · 4 comments
Labels
to-evaluate Issue that has been received but not yet evaluated
Milestone

Comments

@dbachdev
Copy link

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

Deserialization doesn't work correctly on Double/Float fields after processing BigDecimal's wrapper used in conjunction with @JsonCreator annotation.

Version Information

2.18.2

Reproduction

public class DeserializationIssueCheck {

    public static void main(String[] args) throws Exception {

        DeserializationIssue issue = new ObjectMapper()
                .readValue("{\"decimalHolder\":100.00,\"number\":50}", DeserializationIssue.class);

        Logger.getLogger(DeserializationIssueCheck.class.getName())
              .warning("issue.number is incorrect: " + issue.number); // WARNING: issue.number is incorrect: 100.0
    }

    public static class DeserializationIssue {
        public DecimalHolder decimalHolder;
        public Double number;
    }

    public static class DecimalHolder {
        public BigDecimal value;

        public DecimalHolder(BigDecimal value) {
            this.value = value;
        }

        @JsonCreator
        public static DecimalHolder of(BigDecimal value) {
            return new DecimalHolder(value);
        }
    }
}

Expected behavior

Objects are correctly deserialized

Additional context

No response

@dbachdev dbachdev added the to-evaluate Issue that has been received but not yet evaluated label Jan 22, 2025
@pjfanning
Copy link
Member

pjfanning commented Jan 22, 2025

Can you tell us what the issue is? It takes time to reproduce the issue - why not also provide a description of the issue?

Are you saying that 100.0 is not equal to 100.00 and that is your issue?

@dbachdev
Copy link
Author

dbachdev commented Jan 22, 2025

According to the provided input for JSON reading:

{ "decimalHolder": 100.00, "number": 50 }

number's value should be deserialized to DeserializationIssue.number as 50, and instead it's 100.0, which is wrong. Change of number type from Double to Integer works perfertly.


ParserBase keeps cached _numberString value after DecimalHolder deserialization and it's used as an input value for parsing VALUE_NUMBER_INT token which is the value for number property during next iteration (when invoking ParserBase.getDoubleValue()).

pjfanning added a commit to pjfanning/jackson-databind that referenced this issue Jan 22, 2025
pjfanning added a commit to pjfanning/jackson-databind that referenced this issue Jan 22, 2025
pjfanning added a commit to pjfanning/jackson-databind that referenced this issue Jan 22, 2025
pjfanning added a commit to pjfanning/jackson-databind that referenced this issue Jan 22, 2025
pjfanning added a commit to pjfanning/jackson-databind that referenced this issue Jan 22, 2025
@cowtowncoder cowtowncoder changed the title BigDecimal deserialization issue when using @JsonCreator BigDecimal deserialization issue when using @JsonCreator Jan 27, 2025
@cowtowncoder cowtowncoder modified the milestones: 2.18.0, 2.18.3 Jan 27, 2025
@cowtowncoder
Copy link
Member

Fixed via FasterXML/jackson-core#1391 on

  • 2.17.4 (if released)
  • 2.18.3
  • 2.19.0

@dbachdev
Copy link
Author

dbachdev commented Feb 1, 2025

Thank you guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

3 participants