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

Support for java Duration #1409

Open
B-hamza opened this issue May 31, 2022 · 7 comments
Open

Support for java Duration #1409

B-hamza opened this issue May 31, 2022 · 7 comments

Comments

@B-hamza
Copy link

B-hamza commented May 31, 2022

Json schema Spec support format duration as this issue mention json-schema-org/json-schema-spec#565
Is it possible to have java time Duration generated for this format, as it the case for uuid format ?

@joelittlejohn
Copy link
Owner

Should be an easy PR to add. Check out the FormatRule and FormatIT.

@unkish
Copy link
Collaborator

unkish commented Aug 8, 2022

It should be noted though that neither java.time.Duration nor java.time.Period are fully ISO 8601 compliant.
Link in the issue mentions durations cited by https://www.rfc-editor.org/rfc/rfc3339.html#page-12

Durations:

dur-second = 1DIGIT "S"
dur-minute = 1
DIGIT "M" [dur-second]
dur-hour = 1DIGIT "H" [dur-minute]
dur-time = "T" (dur-hour / dur-minute / dur-second)
dur-day = 1
DIGIT "D"
dur-week = 1DIGIT "W"
dur-month = 1
DIGIT "M" [dur-day]
dur-year = 1*DIGIT "Y" [dur-month]
dur-date = (dur-day / dur-month / dur-year) [dur-time]

duration = "P" (dur-date / dur-time / dur-week)

For example it is not possible to construct java.time.Duration of values:

  • P1W
  • P1Y
  • ...

@eirnym
Copy link

eirnym commented Aug 8, 2022

@unkish agree. But for the most use cases duration type is good enough. Like default regex implementation as I wrote below.

There's way more to add about compliance of Java types to JSON schema standards. E.g. one of such type types is regex: Java's version still support regexes like {3} (this is the full regex) and doesn't support some features of ECMA's. In this case to support it, I had to add jruby's implementation.

If parser-specific formatter exists in a parser library, it's better to add an annotation for it. Otherwise, I'd add parser-specific things to a parser or a separate library.

@joelittlejohn
Copy link
Owner

joelittlejohn commented Aug 8, 2022

@unkish It's a good point. I've found that using appropriate types provided by the host platform is most useful, even if they are not strictly compliant. The regex example is a good one.

However, in this case, if we don't support W, M or Y this is a serious problem I think. It looks like a common approach is to return TemporalAmount and have an implementation that uses Duration or Period depending on whether M/W/Y are used. I'm also interested to see how Jackson solves this (e.g. does it have a TemporalAmount ser/deser that implements some logic like this?).

@unkish
Copy link
Collaborator

unkish commented Aug 11, 2022

I'm sorry, maybe I misunderstand what You are trying to hint at. Following code would fail in runtime:

final ObjectMapper om = new ObjectMapper().registerModule(new JavaTimeModule());
final String p1y2mJson = om.writeValueAsString(Period.parse("P1Y2M"));
final Duration pAsDuration = om.readValue(p1y2mJson, Duration.class);

@joelittlejohn
Copy link
Owner

Out if interest, does this work:

om.readValue("P1Y2M", TemporalAmount.class);

?

@unkish
Copy link
Collaborator

unkish commented Aug 12, 2022

Unfortunately not:

Cannot construct instance of java.time.temporal.TemporalAmount (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information

I can't imagine what it would return in case something more complex such as P1Y2MT4H5M - java.time.Duration ?

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

4 participants