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

(datatypes) OptionalInt + YAMLFactory not working correctly for empty case #2

Closed
cowtowncoder opened this issue Nov 3, 2016 · 2 comments

Comments

@cowtowncoder
Copy link
Member

(moved from one reported at old jackson-datatype-jdk8, issue 32)


When deserializing a yaml file if my pojo has OptionalInt rather than Optional, the empty case doesn't seem to be handled right.

Test file:

test:
  value:

Test pojo class:

    private static class MyTestPojo {
    private OptionalInt value = OptionalInt.empty();

    public OptionalInt getValue() {
        return value;
    }

    public void setValue(OptionalInt value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "MyTestPojo{" +
                "value=" + value +
                '}';
    }
}

And the driving code:

private static final ObjectMapper om = new ObjectMapper(new YAMLFactory()).registerModule(new Jdk8Module());
private static final Path testPath = Paths.get("C:/Development/testYaml.txt");

private static final TypeReference<Map<String, MyTestPojo>> tr = new TypeReference<Map<String, MyTestPojo>>() {
};

public static void main(String... args) throws IOException {
    try (BufferedReader br = Files.newBufferedReader(testPath, StandardCharsets.UTF_8)) {
        Map<String, Object> read = om.readValue(br, tr);
        System.out.println(read);
    }
}

When this file is deserialized with this code, I get a response of {test=MyTestPojo{value=OptionalInt[0]}} (as in, it's an optionalint that's present with a value of 0). When I use Optional, I get the correct printout of {test=MyTestPojo{value=Optional.empty}} . For some reason OptionalInt doesn't handle the empty case right here. I've tested without the yaml factory on a json file and it seems to work. I am using version 2.6.4 of Jackson.

@michaelbraun
Copy link

This can be closed anyway, verified it works in the 2.7.7 release. Sorry for the delay.

@cowtowncoder
Copy link
Member Author

@michaelbraun ah ok awesome. Didn't want to close without knowing, but will do now. Thank you for verifying.

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