Skip to content

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

@maroux

Description

@maroux

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions