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

YearMonthDeserializer fails for year > 9999 #249

Closed
bent-lorentzen opened this issue Aug 23, 2022 · 1 comment
Closed

YearMonthDeserializer fails for year > 9999 #249

bent-lorentzen opened this issue Aug 23, 2022 · 1 comment
Milestone

Comments

@bent-lorentzen
Copy link

bent-lorentzen commented Aug 23, 2022

YearMonths with years outside the range 0000 to 9999 must be prefixed by the plus or minus symbol when the format uuuu-MM (default format) is used. Unfortunately the toString of YearMonth does not add a plus sign for years > 9999.
The code

        val writeValueAsString = objectMapper.writeValueAsString(YearMonth.of(10000, 1))
        objectMapper.readValue<YearMonth>(writeValueAsString)

fails with

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.YearMonth` from String "10000-01": Failed to deserialize java.time.YearMonth: (java.time.format.DateTimeParseException) Text '10000-01' could not be parsed at index 0
 at [Source: (String)""10000-01""; line: 1, column: 1]

Changing the constructor

    public YearMonthDeserializer() // public since 2.12
    {
        this(DateTimeFormatter.ofPattern("uuuu-MM"));
    }

to

    public YearMonthDeserializer() // public since 2.12
    {
        this(DateTimeFormatter.ofPattern("u-MM"));
    }

solves this problem,

@cowtowncoder
Copy link
Member

I was first unable to reproduce, since the default serialization of YearMonth is an array (with 2.14 at least?), but when forcing output as String issue occurs.

@cowtowncoder cowtowncoder changed the title YearMonthDeserializer fails for year > 9999 YearMonthDeserializer fails for year > 9999 Sep 24, 2022
@cowtowncoder cowtowncoder added this to the 2.14.0 milestone Sep 24, 2022
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