-
Notifications
You must be signed in to change notification settings - Fork 112
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
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3b65a0e
Implement serialization of an Instant
dkhalanskyjb 357b151
Implement some serializers in common code
dkhalanskyjb 73312a1
Add rough measurements of serialization efficiency
dkhalanskyjb 1881a0d
Add serializers for LocalDate, LocalDateTime, DateTimePeriod
dkhalanskyjb 820516c
Add serializers for DateTimeUnit
dkhalanskyjb c27e81e
Add error handling to serializers
dkhalanskyjb ebdf6bf
Serialize time zones
dkhalanskyjb 2eaac88
Rename serializers
dkhalanskyjb 0452296
Implement handwritten serializers for DateTimeUnit
dkhalanskyjb 038ad25
Add tests for serialization
dkhalanskyjb 1459c5c
ISO8601 serializer for DateTimePeriod
dkhalanskyjb d486f9a
Fix serial descriptor names
dkhalanskyjb 0518207
Only depend on serialization with compileOnly from `core`
dkhalanskyjb 6acf818
Move serializers to a separate package
dkhalanskyjb 756af5c
Cleanup
dkhalanskyjb 2c35bb4
Fixes
dkhalanskyjb af231f7
Remove the serializers that were decided against
dkhalanskyjb d2cb1eb
Disable publishing for the serialization subproject
dkhalanskyjb c5c8ad2
Rename DateTimeUnit serializers
dkhalanskyjb 9ff7e03
Test which serializers are the default ones
dkhalanskyjb b3ce0fe
Add a (failing) test for contextual serializers usage
dkhalanskyjb 31d8fa3
Fix the failing test and add a test for default serializers
dkhalanskyjb 8170830
ISO -> Iso in serializer names
dkhalanskyjb b4b29da
Add a test for manual setting of serializers
dkhalanskyjb f15d28c
Remove unrelated changes to formatting
dkhalanskyjb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
core/common/src/serializers/DateTimePeriodSerializers.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Copyright 2019-2021 JetBrains s.r.o. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package kotlinx.datetime.serializers | ||
|
||
import kotlinx.datetime.DatePeriod | ||
import kotlinx.datetime.DateTimePeriod | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.encoding.* | ||
|
||
object DateTimePeriodComponentSerializer: KSerializer<DateTimePeriod> { | ||
|
||
override val descriptor: SerialDescriptor = | ||
buildClassSerialDescriptor("DateTimePeriod") { | ||
element<Int>("years", isOptional = true) | ||
element<Int>("months", isOptional = true) | ||
element<Int>("days", isOptional = true) | ||
element<Int>("hours", isOptional = true) | ||
element<Int>("minutes", isOptional = true) | ||
element<Int>("seconds", isOptional = true) | ||
element<Long>("nanoseconds", isOptional = true) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): DateTimePeriod = | ||
decoder.decodeStructure(descriptor) { | ||
var years = 0 | ||
var months = 0 | ||
var days = 0 | ||
var hours = 0 | ||
var minutes = 0 | ||
var seconds = 0 | ||
var nanoseconds = 0L | ||
loop@while (true) { | ||
when (val index = decodeElementIndex(descriptor)) { | ||
0 -> years = decodeIntElement(descriptor, 0) | ||
1 -> months = decodeIntElement(descriptor, 1) | ||
2 -> days = decodeIntElement(descriptor, 2) | ||
3 -> hours = decodeIntElement(descriptor, 3) | ||
4 -> minutes = decodeIntElement(descriptor, 4) | ||
5 -> seconds = decodeIntElement(descriptor, 5) | ||
6 -> nanoseconds = decodeLongElement(descriptor, 6) | ||
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262 | ||
else -> throw SerializationException("Unexpected index: $index") | ||
} | ||
} | ||
DateTimePeriod(years, months, days, hours, minutes, seconds, nanoseconds) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: DateTimePeriod) { | ||
encoder.encodeStructure(descriptor) { | ||
with(value) { | ||
if (years != 0) encodeIntElement(descriptor, 0, years) | ||
if (months != 0) encodeIntElement(descriptor, 1, months) | ||
if (days != 0) encodeIntElement(descriptor, 2, days) | ||
if (hours != 0) encodeIntElement(descriptor, 3, hours) | ||
if (minutes != 0) encodeIntElement(descriptor, 4, minutes) | ||
if (seconds != 0) encodeIntElement(descriptor, 5, seconds) | ||
if (nanoseconds != 0) encodeLongElement(descriptor, 6, value.nanoseconds.toLong()) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
object DateTimePeriodIso8601Serializer: KSerializer<DateTimePeriod> { | ||
|
||
override val descriptor: SerialDescriptor = | ||
PrimitiveSerialDescriptor("DateTimePeriod", PrimitiveKind.STRING) | ||
|
||
override fun deserialize(decoder: Decoder): DateTimePeriod = | ||
DateTimePeriod.parse(decoder.decodeString()) | ||
|
||
override fun serialize(encoder: Encoder, value: DateTimePeriod) { | ||
encoder.encodeString(value.toString()) | ||
} | ||
|
||
} | ||
|
||
object DatePeriodComponentSerializer: KSerializer<DatePeriod> { | ||
|
||
private fun unexpectedNonzero(fieldName: String, value: Long) { | ||
if (value != 0L) { | ||
throw SerializationException("DatePeriod should have non-date components be zero, but got $value in '$fieldName'") | ||
} | ||
} | ||
|
||
private fun unexpectedNonzero(fieldName: String, value: Int) = unexpectedNonzero(fieldName, value.toLong()) | ||
|
||
override val descriptor: SerialDescriptor = | ||
buildClassSerialDescriptor("DatePeriod") { | ||
element<Int>("years", isOptional = true) | ||
element<Int>("months", isOptional = true) | ||
element<Int>("days", isOptional = true) | ||
element<Int>("hours", isOptional = true) | ||
element<Int>("minutes", isOptional = true) | ||
element<Int>("seconds", isOptional = true) | ||
element<Long>("nanoseconds", isOptional = true) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): DatePeriod = | ||
decoder.decodeStructure(descriptor) { | ||
var years = 0 | ||
var months = 0 | ||
var days = 0 | ||
loop@while (true) { | ||
when (val index = decodeElementIndex(descriptor)) { | ||
0 -> years = decodeIntElement(descriptor, 0) | ||
1 -> months = decodeIntElement(descriptor, 1) | ||
2 -> days = decodeIntElement(descriptor, 2) | ||
3 -> unexpectedNonzero("hours", decodeIntElement(descriptor, 3)) | ||
4 -> unexpectedNonzero("minutes", decodeIntElement(descriptor, 4)) | ||
5 -> unexpectedNonzero("seconds", decodeIntElement(descriptor, 5)) | ||
6 -> unexpectedNonzero("nanoseconds", decodeLongElement(descriptor, 6)) | ||
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262 | ||
else -> throw SerializationException("Unexpected index: $index") | ||
} | ||
} | ||
DatePeriod(years, months, days) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: DatePeriod) { | ||
encoder.encodeStructure(descriptor) { | ||
with(value) { | ||
if (years != 0) encodeIntElement(DateTimePeriodComponentSerializer.descriptor, 0, years) | ||
if (months != 0) encodeIntElement(DateTimePeriodComponentSerializer.descriptor, 1, months) | ||
if (days != 0) encodeIntElement(DateTimePeriodComponentSerializer.descriptor, 2, days) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
object DatePeriodIso8601Serializer: KSerializer<DatePeriod> { | ||
|
||
override val descriptor: SerialDescriptor = | ||
PrimitiveSerialDescriptor("DatePeriod", PrimitiveKind.STRING) | ||
|
||
// TODO: consider whether should fail when parsing "P1YT0H0M0.0S" | ||
override fun deserialize(decoder: Decoder): DatePeriod = | ||
when (val period = DateTimePeriod.parse(decoder.decodeString())) { | ||
is DatePeriod -> period | ||
else -> throw SerializationException("$period is not a date-based period") | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: DatePeriod) { | ||
encoder.encodeString(value.toString()) | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.