Skip to content

kotlinx-serialization support #101

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

Merged
merged 25 commits into from
Mar 29, 2021
Merged

Conversation

dkhalanskyjb
Copy link
Collaborator

@dkhalanskyjb dkhalanskyjb commented Mar 11, 2021

Fixes #37

Additionally, check gzipped JSON and Protobuf in the "benchmarks".
* A new subproject was created just for testing.
* DateTimePeriod serialization is not tested yet
* Tests that serialize 64-bit numbers fail on nodejs for some reason
* Enum classes serialization had to be implemented manually.
  Though https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/builtin-classes.md#enum-classes
  claims that enums are automatically serialized, on Kotlin/Native
  and Kotlin/JS runtime crashes occur if no serializer is passed
  explicitly.
* Some code had to be modified due to a bug in the legacy JS codegen
  that led to an infinite hanging of deserialization.
Copy link
Member

@sandwwraith sandwwraith left a comment

Choose a reason for hiding this comment

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

It looks quite OK, but my main concern here is inconsistency of exceptions


@ExperimentalSerializationApi
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
override fun deserialize(decoder: Decoder): DateTimeUnit.DateBased.MonthBased {
Copy link
Member

Choose a reason for hiding this comment

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

Since this method is mostly identical for all serializers, I'd suggest to abstract it out to a single method like decodeStructureWithOneInt(descriptor, fieldName)

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 attempted such a refactoring and decided against it: I only found two serializer that share this code, the ones for DateTimeUnit.DateBased.MonthBased and DateTimeUnit.DateBased.DayBased; DateTimeUnit.TimeBased also looks very similarly, but it actually uses a Long.

Copy link
Member

Choose a reason for hiding this comment

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

You can decode Long and that check if it fits into Int. Or I'm missing something?

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 don't know. Can I safely call decodeLongElement when the serial descriptor claims that the field is an Int?

Copy link
Member

Choose a reason for hiding this comment

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

Think so. Descriptors are mainly used for scheme, and your scheme accepts ints

@dkhalanskyjb dkhalanskyjb force-pushed the serialization-experiments branch from fe34484 to b3ce0fe Compare March 24, 2021 13:04

}

object DatePeriodISO8601Serializer: KSerializer<DatePeriod> {

Choose a reason for hiding this comment

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

Not sure what you all use as a style guide, but I try really hard to evangelize the practice of treating acronyms and acronym-like things such as ISO, IPv6, iOS, etc. as words and casing them as such.

Google's Java and Kotlin style guide recommend this practice:

That would make this type be DatePeriodIso8601Serializer. And I think there's one or two more in the change.

Choose a reason for hiding this comment

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

+1 for DatePeriodIso8601Serializer

Copy link
Member

Choose a reason for hiding this comment

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

Agree, it would be consistent with Duration.toIsoString in stdlib, for example.

What do you think about dropping 8601 suffix? On one hand, it's not that there are many ISO standards on date representation, on the other hand, the serializer name is not supposed to be used often, so the character economy there may be not worth it.

@dkhalanskyjb dkhalanskyjb requested a review from ilya-g March 26, 2021 11:27
@@ -20,7 +20,6 @@ public expect enum class Month {
OCTOBER,
NOVEMBER,
DECEMBER;

Copy link
Member

Choose a reason for hiding this comment

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

minor: unrelated change

@dkhalanskyjb dkhalanskyjb merged commit 93c9150 into master Mar 29, 2021
@dkhalanskyjb dkhalanskyjb deleted the serialization-experiments branch March 29, 2021 08:37
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.

Support @Serializable
5 participants