Skip to content

StdDeserializer rejects blank (all-whitespace) strings for ints #3234

@pburka

Description

@pburka

Describe the bug
In Jackson 2.10.2 the standard deserializer converts empty strings and all-whitespace strings to zero when deserializing to ints. In 2.12 the default behavior changed: it accepts blank strings but not strings consisting only of whitespace (e.g. " "). Instead of deserializing to zero, it throws InvalidFormatException. I imagine this is related to the changes to leniency handling in 2.12. While it's great to be able to configure leniency, for backwards compatibility the library should maintain the old default behavior.

Version information
2.12.4

To Reproduce

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@JsonDeserialize
public class Jack {
    private final int j;

    @JsonCreator
    public Jack(@JsonProperty("J") int j) {
        this.j = j;
    }

    private static void test(String s) {
        final String json = "{ \"J\" : \"" + s + "\" }";
        try {
            final Jack jack = new ObjectMapper().readValue(json, Jack.class);
            System.out.println("j=" + jack.j);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // With Jackson 2.10.2 these all print j=0
        test("0");
        test("0 ");
        test(" 0");
        test(" 0 ");
        test("");
        // With Jackson 2.12.4 these throw InvalidFormatException
        test(" ");
        test("    ");
        test("\\n");
    }
}

Expected behavior

Expected behavior: the default deserializer should behave consistently in 2.10 and 2.12.

Actual behavior: the 2.12 deserializer is less lenient and rejects non-empty strings that contain only whitespace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions