Skip to content
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

Serialisation of interfaces doesn't work #1891

Closed
beretis opened this issue Apr 1, 2022 · 2 comments
Closed

Serialisation of interfaces doesn't work #1891

beretis opened this issue Apr 1, 2022 · 2 comments

Comments

@beretis
Copy link

beretis commented Apr 1, 2022

Describe the bug
When I tried Serializing interfaces example from docs but im getting
On Kotlin/Native explicitly declared serializer should be used for interfaces and enums without @Serializable annotation
To Reproduce
Just run the example code from the docs.

`import kotlinx.serialization.*
import kotlinx.serialization.json.*

import kotlinx.serialization.modules.*

val module = SerializersModule {
polymorphic(Project::class) {
subclass(OwnedProject::class)
}
}

val format = Json { serializersModule = module }

interface Project {
val name: String
}

@serializable
@SerialName("owned")
class OwnedProject(override val name: String, val owner: String) : Project

fun main() {
val data: Project = OwnedProject("kotlinx.coroutines", "kotlin")
println(format.encodeToString(data))
} `

Expected behavior

Environment

  • Kotlin version: 1.6.10
  • Library version: 1.6.10
  • Kotlin platforms: Native
  • Gradle version: 7.0.0
  • IDE version: IntelliJ IDEA 2021.3.2 (Community Edition)
  • Other relevant context OS: MacOs Monterey
@aSemy
Copy link
Contributor

aSemy commented Apr 1, 2022

Does this answer your question?

https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#registered-subclasses

Please note that this example works only on JVM because of serializer function restrictions.
For JS and Native, explicit serializer should be used: format.encodeToString(PolymorphicSerializer(Project::class), data)
You can keep track of this issue here.

@beretis
Copy link
Author

beretis commented Apr 2, 2022

@aSemy you're right, im sorry. Thank you for reply, closing the issue :).

@beretis beretis closed this as completed Apr 2, 2022
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

No branches or pull requests

2 participants