-
Notifications
You must be signed in to change notification settings - Fork 103
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
Introduce serializers corresponding to custom formats #350
Labels
formatters
Related to parsing and formatting
Milestone
Comments
This will resolve so much pain we are facing now, thank you! :) |
@morki, if you're truly in pain, as a workaround, you can introduce simple pieces of code like this in the meantime: public class LocalDateCustomSerializer(private val format: DateTimeFormat<LocalDate>): KSerializer<LocalDate> {
override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("kotlinx.datetime.LocalDate", PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): LocalDate =
LocalDate.parse(decoder.decodeString(), format)
override fun serialize(encoder: Encoder, value: LocalDate) {
encoder.encodeString(value.format(format))
}
} |
Thank you very much! :) I did't know it will be so easy, we have been deserializing in custom getters from string for custom formats so far and it was a pain. For others, here is how to use it in serializable class: object SlovakLocalDateSerializer : LocalDateCustomSerializer(LocalDate.Format {
dayOfMonth(Padding.NONE)
char('.')
optional { char(' ') }
monthNumber(Padding.NONE)
char('.')
optional { char(' ') }
year(Padding.NONE)
})
@Serializable
data class Example(
@Serializable(with = SlovakLocalDateSerializer::class)
val exampleDate: LocalDate?,
) |
dkhalanskyjb
added a commit
that referenced
this issue
Jul 23, 2024
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
dkhalanskyjb
added a commit
that referenced
this issue
Jul 23, 2024
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
dkhalanskyjb
added a commit
that referenced
this issue
Jul 24, 2024
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
dkhalanskyjb
added a commit
that referenced
this issue
Jul 25, 2024
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Right now, we provide serializers that format the entities of our library as ISO 8601 strings, using
toString
andparse
functions. However, now thatparse
andformat
can work with custom formats, we could provide things likeclass LocalDateCustomSerializer(format: DateTimeFormat<LocalDate>): KSerializer<LocalDate>
.The text was updated successfully, but these errors were encountered: