-
Notifications
You must be signed in to change notification settings - Fork 636
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
Implicit generic doesn't pick up typealias
serializer
#2264
Comments
I have a similar issue. So I didn't want to open a new issue, it might be related. I’m trying to implement a serializer for a different kind of date format. which looks like this {"date": {"$date": 1688649908793} } This is a global type so I wanted to specify a serializer globally as documented here. Here is what I did: @Serializable
data class Result(
val date: EDate
)
typealias EDate = @Serializable(EDateSerializer::class) Instant
@Serializable
//@SerialName("EDate")
//@SerialName("Instant")
data class InstantSurrogate(
@SerialName("\$date")
val date: Long
)
//@OptIn(ExperimentalSerializationApi::class)
//@Serializer(forClass = Instant::class)
object EDateSerializer : KSerializer<Instant> {
override val descriptor: SerialDescriptor = InstantSurrogate.serializer().descriptor
override fun serialize(encoder: Encoder, value: Instant) {
val millisecond = value.toEpochMilliseconds()
val eDate = InstantSurrogate(millisecond)
encoder.encodeSerializableValue(InstantSurrogate.serializer(), eDate)
}
override fun deserialize(decoder: Decoder): Instant {
val eDate = decoder.decodeSerializableValue(InstantSurrogate.serializer())
return Instant.fromEpochMilliseconds(eDate.date)
}
} When I do this, serializer tries to use |
I guess this was related to #1895 . It works with 1.8.20. I'm not sure about @BenWoodworth 's issue |
Unfortunately this is how type inference in Kotlin works (as it expands typealiases), so I'm not sure we can do anything here. |
I was starting to think the same thing. I'll post an issue to the Kotlin youtrack and link it back here, since typealiases feels like something that should be propagated through inferred type args. And if it turns out to be something that won't be supported, then maybe leaving a note in the kxs docs about |
By the way, specifying value type explicitly in your example works around the issue: |
@sandwwraith |
As per https://youtrack.jetbrains.com/issue/KT-67252 and #2731, we won't support this use-case, so using explicit serializer is a way to go |
But in reality, it works in some cases. @sandwwraith |
@iseki0 If you show us an example of such cases, we'll happily prohibit them as well 😄 |
// {"date": {"$date": 1688649908793} }
@Serializable
data class Result(
val date: EDate
)
typealias EDate = @Serializable(EDateSerializer::class) Instant
@Serializable
@SerialName("EDate")
data class InstantSurrogate(
@SerialName("\$date")
val date: Long,
)
object EDateSerializer : KSerializer<Instant> {
override val descriptor: SerialDescriptor = InstantSurrogate.serializer().descriptor
override fun serialize(encoder: Encoder, value: Instant) {
val millisecond = value.toEpochMilliseconds()
val eDate = InstantSurrogate(millisecond)
encoder.encodeSerializableValue(InstantSurrogate.serializer(), eDate)
}
override fun deserialize(decoder: Decoder): Instant {
val eDate = decoder.decodeSerializableValue(InstantSurrogate.serializer())
return Instant.fromEpochMilliseconds(eDate.date)
}
}
this works on 1.7.1 and kotlin 2.0.0. do what you need to do 🔫 |
Lol, @osrl I always call it @sandwwraith Could you add a compiler options for static serializer assignation? |
You use a special name here. But for other tools? If there's also javax-serialization, databasex-serialization. How to do? |
Oh okay, as I said, it's a special name for a special json format. so it's okay. |
you are probably talking about #507, but this is a feature that is postponed for a long time |
Describe the bug
When calling
serializer()
with an implicit type argument for a@Serializable
typealias, the typealias is effectively ignored, and the actual type is used insteadTo Reproduce
Expected behavior
Serializer provided in the typealias is used, even when the generic is implicit
Environment
The text was updated successfully, but these errors were encountered: