Skip to content

Create JsonDuration from java.time.Duration#17

Merged
runeflobakk merged 4 commits intomainfrom
create-jsonduration-from-duration
Mar 17, 2026
Merged

Create JsonDuration from java.time.Duration#17
runeflobakk merged 4 commits intomainfrom
create-jsonduration-from-duration

Conversation

@runeflobakk
Copy link
Copy Markdown
Member

Construct JsonDuration from a Duration

Add a factory method JsonDuration.from(Duration) which "deconstructs" a given Duration into an amount and an appropriate ChronoUnit. The largest possible unit (up to "days") which can be used to accurately represent the duration is chosen.

static final Duration MY_SENSIBLE_DEFAULT = Duration.ofHours(24);
...

@NotNull
JsonDuration durationMappedByJackson = JsonDuration.from(MY_SENSIBLE_DEFAULT);

This allows the use of MY_SENSIBLE_DEFAULT to be used as a default value for a JsonDuration.
Previously, the only way to create an instance of JsonDuration was to parse a String like "24 hours", "4 seconds", etc.

More descriptive factory method

Introducing JsonDuration.parse(String) for parsing a duration, and deprecating JsonDuration.of(String).


Compatibility Report

semver OK

Summary

Note

No incompatible changes found while checking backward compatibility of version n.a.-SNAPSHOT with the previous version 4.0.0.

Expand to see options used.
  • Report only summary: No
  • Report only changes: Yes
  • Report only binary-incompatible changes: Yes
  • Access modifier filter: PROTECTED
  • Old archives:
    • typesafe-config-bundle 4.0.0
  • New archives:
    • typesafe-config-bundle-LOCAL-SNAPSHOT n.a.-SNAPSHOT
  • Evaluate annotations: Yes
  • Include synthetic classes and class members: No
  • Include specific elements: No
  • Exclude specific elements: No
  • Ignore all missing classes: No
  • Ignore specific missing classes: No
  • Treat changes as errors:
    • Any changes: No
    • Binary incompatible changes: No
    • Source incompatible changes: No
    • Incompatible changes caused by excluded classes: Yes
    • Semantically incompatible changes: No
    • Semantically incompatible changes, including development versions: No
  • Classpath mode: ONE_COMMON_CLASSPATH
  • Old classpath:

  • New classpath:

To e.g. enable initializing JsonDuration fields from existing constants
in your application domain, which will typically be of type Duration.

In addition, use a more descriptive name for the method used to parse
String values on the form "1 hours", "42 seconds", etc. Deprecating
JsonDuration.of(String).
To enforce backwards compatibility. Internal representation has not
changed in JsonDuration.
@runeflobakk runeflobakk merged commit 93f365a into main Mar 17, 2026
2 checks passed
@runeflobakk runeflobakk deleted the create-jsonduration-from-duration branch March 17, 2026 09:42
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

Successfully merging this pull request may close these issues.

2 participants