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

Float values with integer value beyond int lose precision if bound to long #1940

Closed
maroux opened this issue Feb 23, 2018 · 3 comments
Closed
Milestone

Comments

@maroux
Copy link

maroux commented Feb 23, 2018

Jackson version: 2.9.4

Sample code demonstrating the problem:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="_class")
public class LongContainer {
    @JsonProperty
    private Long longObj;

    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode tree = objectMapper.readTree("{\"longObj\": 1519348261000.0, \"_class\": \"LongContainer\"}");
        LongContainer obj = objectMapper.treeToValue(tree, LongContainer.class);
        System.out.println(obj.longObj);
    }
}

It's caused by this line. Curiously enough, it works fine if you deserialize a plain float value into Long.class since that uses TreeTraversingParser instead of TokenBuffer.

@cowtowncoder
Copy link
Member

Wow. That is curious line. Must have been cut'n pasted. Thanks, will need to fix.

cowtowncoder added a commit that referenced this issue Feb 23, 2018
@cowtowncoder cowtowncoder changed the title Float values serialized into Long class cause integer overflow Float values with integer value beyond int lose precision if bound to long Feb 23, 2018
@cowtowncoder cowtowncoder added this to the 2.9.5 milestone Feb 23, 2018
@cowtowncoder
Copy link
Member

Thank you for reporting this -- seems to be a regression due to refactoring, not covered (yet) with unit tests. Added test, fixed, will be in 2.9.5.

@maroux
Copy link
Author

maroux commented Feb 23, 2018

Awesome. Thanks for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants