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

Instant, YearMonth and MonthDay not use pattern in @JsonFormat #101

Closed
devdevx opened this issue Sep 9, 2018 · 4 comments
Closed

Instant, YearMonth and MonthDay not use pattern in @JsonFormat #101

devdevx opened this issue Sep 9, 2018 · 4 comments
Milestone

Comments

@devdevx
Copy link
Contributor

devdevx commented Sep 9, 2018

I'm trying to define a custom patterns for this classes but sems that the @JsonFormat is ignored.

Jackson version: 2.9.3
Jackson datatype version: 2.9.6

Example of @JsonFormat anotation:

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy HH_mm_ss_SSS|z")
    private Instant customInstant;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy/MM")
    private YearMonth customYearMonth;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM_dd")
    private MonthDay customMonthDay;

Current result:

When serialize the fields are formated with the default pattern and when deserialize a exception is throw.

Expected result:

Serialization and deserialization works sucessfully using the defined pattern.

Thanks !

@cowtowncoder
Copy link
Member

Thank you for reporting the problem. I assume reproduction should be easy, if anyone has time to look into this (I may have but right now no guarantees).

@cowtowncoder cowtowncoder added 2.9 active Issue being actively investigated and/or worked on labels Sep 11, 2018
@devdevx
Copy link
Contributor Author

devdevx commented Sep 11, 2018

After reading the source code I think that this could work:

  • YearMonth serialization

YearMonth uses the ToStringSerializer.instance, it should be replaced by a new class (maybe YearMonthSerializer) that extends the JodaDateSerializerBase and should be similar to LocalDateSerializer.

  • YearMonth deserialization

YearMonthDeserializer should extend JodaDateDeserializerBase instead of JodaDeserializerBase.

Doing this the attribute this._formatter should contain the formatter for the custom pattern (or the default pattern if custom not exist?)

In the method deserialize of YearMonthDeserializer we need to change the YearMonth.parse(str) by YearMonth.parse(str, this._formatter)

  • MonthDay serialization

MonthDay uses the ToStringSerializer.instance, it should be replaced by a new class (maybe MonthDaySerializer) that extends the JodaDateSerializerBase and should be similar to LocalDateSerializer.

  • MonthDay deserialization

MonthDayDeserializer should extend JodaDateDeserializerBase instead of JodaDeserializerBase

Doing this the attribute this._formatter should contain the formatter.

In the method deserialize of MonthDayDeserializer we need to change the MonthDay.parse(str) by MonthDay.parse(str, this._formatter)

  • Instant serialization

In InstantSerializer the gen.writeString(value.toString()) of the method serialize should be replaced by gen.writeString(this._format.createFormatter(provider).print(value))

  • Instant deserialization

InstantDeserializer should extend JodaDateDeserializerBase instead of JodaDeserializerBase

Doing this the attribute this._formatter should contain the formatter.

In the method deserialize of InstantDeserializer we need to change the new Instant(str) by Instant.parse(str, this._formatter)

What do you think? If you wish, I think that I could try to do a PR in the comming days.

@cowtowncoder
Copy link
Member

@devdevx I think that makes sense, and help would be welcome. I'll see if I could implement part of this, maybe InstantDeserializer, as a starting point. Changes can and should go against 2.9 branch since there's no API change; and that way would get in 2.9.7.

@cowtowncoder
Copy link
Member

cowtowncoder commented Sep 12, 2018

I was able to implement support for custom format for Instant, leaving MonthDay and YearMonth.

cowtowncoder added a commit that referenced this issue Sep 18, 2018
End work on #101; add custom format support for 'YearMonth' and 'MonthDay' ser, deser
@cowtowncoder cowtowncoder removed the active Issue being actively investigated and/or worked on label Sep 18, 2018
@cowtowncoder cowtowncoder added this to the 2.9.7 milestone Sep 18, 2018
@cowtowncoder cowtowncoder removed the 2.9 label Oct 3, 2019
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