Skip to content

Jackson can't handle underscores in numbers #146

Closed
@smedelyan

Description

@smedelyan

Not sure I've chosen the right repository to report the issue, but there's a similar one: #65

I can't see a "dataformats-text" explicit dependency so here's how I use it:

compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.9'
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.9.3'

Below are tests proving it fails to parse numbers with underscores.

    private static class LongHolder {
        private Long v;

        public Long getV() {
            return v;
        }

        public void setV(Long v) {
            this.v = v;
        }
    }

    private static class IntHolder {
        private Integer v;

        public Integer getV() {
            return v;
        }

        public void setV(Integer v) {
            this.v = v;
        }
    }

    @Test
    public void testYamlUnderscores() throws IOException {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        mapper.readValue("v: 1000000", IntHolder.class); // ok
        mapper.readValue("v: 1_000_000", IntHolder.class); // ok
        mapper.readValue("v: 1000000", LongHolder.class); // ok

        // throws: com.fasterxml.jackson.databind.JsonMappingException: For input string: "1_000_000"
        mapper.readValue("v: 1_000_000", LongHolder.class);
    }

    @Test
    public void testJsonUnderscores() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.readValue("{\"v\": \"1000000\"}", IntHolder.class); // ok

        // throws:
        // com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java
        // .lang.Integer` from String "1_000_000": not a valid Integer value
        // at [Source: (String)"{"v": "1_000_000"}"; line: 1, column: 7]
        mapper.readValue("{\"v\": \"1_000_000\"}", IntHolder.class);

        mapper.readValue("{\"v\": \"1000000\"}", LongHolder.class); // ok

        // throws:
        // com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java
        // .lang.Long` from String "1_000_000": not a valid Long value
        // at [Source: (String)"{"v": "1_000_000"}"; line: 1, column: 7]
        mapper.readValue("{\"v\": \"1_000_000\"}", LongHolder.class);
    }

Metadata

Metadata

Assignees

Labels

yamlIssue related to YAML format backend

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions