Giter Club home page Giter Club logo

Comments (2)

elizarov avatar elizarov commented on August 25, 2024

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.

from kotlinx.serialization.

sandwwraith avatar sandwwraith commented on August 25, 2024

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

from kotlinx.serialization.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.