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

Support generic user-defined classes #1

Closed
elizarov opened this issue Sep 22, 2017 · 2 comments
Closed

Support generic user-defined classes #1

elizarov opened this issue Sep 22, 2017 · 2 comments
Labels

Comments

@elizarov
Copy link
Contributor

It should be possible to define:

@Serializable class Box<T>(val value: T)

and the enjoy type-safe serialization of Box(42) and Box("foo") alike. This is a meta-issue to discuss design problems surrounding type-safe access to instances of serializers for generic classes.

@elizarov elizarov changed the title Support generic use-defined classes Support generic user-defined classes Sep 22, 2017
@elizarov
Copy link
Contributor Author

elizarov commented Oct 5, 2017

The way it can/should work is this in Kotlin < 1.3 (w/o any language changes):

@Serializable data class MyData(val x: Int)
@Serializable data class Box<T>(val v: T)

val b = Box(MyData(42))
JSON.stringify(b, Box.serializer(MyData)) // produces "{v: {x: 42}}" -- types are fully static
val c = JSON.parse("...", Box.serializer(MyData))

In short, the idea that for generic classes a serialier(...) function in their companion object will be generated by serialization plugin. This function will have as many parameters as there are generic type parameters in the class, each parameter to the serializer function being the corresponding serializer.

In Koltin 1.3 we can rollout experimental language support for serialization and redefine JSON.parse function like this:

fun <serializable T> parse(): T

The serializable modifier is going to work similarly to reified (with or without the need for inline). It will plug the corresponding type-specific serializer as an additional parameter to the parse function behind the scenes. Inside the parse it could be accessed like T::serializer.

@sandwwraith
Copy link
Member

Generic user classes supported (modulo bugs) in 0.3 with syntax described here

qwwdfsad pushed a commit that referenced this issue Jan 28, 2019
qwwdfsad added a commit that referenced this issue Jun 4, 2022
Part #1, serializers, encoders and decoders
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants