Skip to content

Serialise Instant as ISO 8601 string, Duration as number of milliseconds, and allow registering of custom TypeAdapterFactories#390

Open
ettersi wants to merge 4 commits intomasterfrom
json-refinements
Open

Serialise Instant as ISO 8601 string, Duration as number of milliseconds, and allow registering of custom TypeAdapterFactories#390
ettersi wants to merge 4 commits intomasterfrom
json-refinements

Conversation

@ettersi
Copy link
Copy Markdown
Collaborator

@ettersi ettersi commented Mar 23, 2026

This PR bundles two unrelated changes.

Serialisation of Instant and Duration

By default, Gson serialises java.time.Instant and java.time.Duration objects as JSON objects with seconds and nanos fields (same JSON format for both Java classes).

{ "seconds": ..., "nanos": ... }

This representation feels needlessly Java-specific. This PR proposes to serialise Instant values as ISO 8601 strings ("2026-03-23T05:20:06.323Z"), and Duration values as number of milliseconds (1000 == 1 second) instead. The rationale behind this is that ISO 8601 strings are instantly human-readable and easily parsable in any reasonable language (including C), and fjage already uses milliseconds in many places to represent durations.

Support custom type adapter factories

Fjage already supports registering of custom type hierarchy adapters, but not type adapter factories. I'm assuming the only reason for this is that so far no one had a need for type adapter factories, but now I do. Therefore, I've added a new method JsonMessage.addTypeAdapterFactory().

@ettersi ettersi requested a review from mchitre March 23, 2026 05:33
@ettersi
Copy link
Copy Markdown
Collaborator Author

ettersi commented Mar 26, 2026

Moved the Instant and Duration serialisation code into a named class so it can be used elsewhere. That's just a refactor, it doesn't change anything about the behaviour.

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