Skip to content

Rework the serializers using the DateTimeFormat API #415

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

dkhalanskyjb
Copy link
Collaborator

@dkhalanskyjb dkhalanskyjb commented Jul 23, 2024

  • Default serializers are separated from the ISO serializers, which now mirror the behavior of Formats.ISO instead of toString/parse.
  • New API: custom serializers parameterized with a DateTimeFormat.
  • Serial names are properly distinct for incompatible formats.

Fixes #350
Fixes #351
Fixes #416

@dkhalanskyjb dkhalanskyjb force-pushed the custom-serializers branch 2 times, most recently from 314121a to aec4a6c Compare July 25, 2024 13:12
@dkhalanskyjb dkhalanskyjb changed the title Provide custom serializers based on DateTimeFormat Rework the serializers using the DateTimeFormat API Jul 25, 2024
@dkhalanskyjb dkhalanskyjb marked this pull request as ready for review July 25, 2024 13:45
@dkhalanskyjb dkhalanskyjb requested a review from ilya-g July 25, 2024 13:45
@dkhalanskyjb dkhalanskyjb added this to the 0.7.0 milestone Mar 3, 2025
@dkhalanskyjb dkhalanskyjb force-pushed the custom-serializers branch 4 times, most recently from a2af7bf to 05f7e25 Compare March 20, 2025 14:15
*/
public object InstantIso8601Serializer : KSerializer<Instant> {

override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("kotlinx.datetime.Instant", PrimitiveKind.STRING)
PrimitiveSerialDescriptor("kotlinx.datetime.Instant ISO", PrimitiveKind.STRING)
Copy link
Member

@ilya-g ilya-g May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While there are no apparent restrictions on what can be used as a serial name, and no recommendations how to name multiple alternate serializers for the same type, this name may be used in error messages without quotes, so I think it would be better not to use spaces, so it can be read as one word.
Perhaps a slash can be used? E.g. kotlinx.datetime.Instant/ISO, kotlinx.datetime.Instant/components, and kotlinx.datetime.Instant/serializer/$customName
Or have we already come to a decision about the current serial descriptor naming scheme?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do remember that we have explicitly (but arbitrarily!) chosen spaces, yes, but the point about error messages was not raised at the time. The naming scheme you propose looks good.

The names of the serializers are still under discussion, but
it's already decided that the serializers are going to be abstract
classes.

Fixes #350
Sometimes, `X.Formats.ISO` and `X.parse()`/`X.toString()` behave
subtly differently; currently, it's the case for `LocalDateTime`
and `LocalTime`.

With this change, every entity that supports custom formats obtains
a separate default serializer in addition to the
ISO 8601 serializer, which now properly delegates to the
corresponding `DateTimeFormat`.

Fixes #351
User-visible error messages include serial descriptors verbatim.
@dkhalanskyjb dkhalanskyjb requested a review from ilya-g May 28, 2025 08:35
* See [Instant.parse] for details of how deserialization is performed.
*
* [name] is the name of the serializer.
* The [SerialDescriptor.serialName] of the resulting serializer is `kotlinx.datetime.Instant serializer `[name].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update in docs of abstract serializers as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants