-
Notifications
You must be signed in to change notification settings - Fork 635
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
Make PolymorpicSerializer serializer to work in JS #101
Comments
Is there an estimate when this would become available? I am currently looking into alternatives to deserialize JSON output from kotlinx.serialization in Typescript/JS. If this were planned to be released soon I might postpone this instead. |
@sandwwraith Can you guys let us know whether anybody is working on it and/or give us a very very rough ETA? I believe a lot of people depend on that feature and I'm thinking about writing my own hacky, simplified serialization lib if polymorphic serializers wont' work in kotlinx serialization soon. But I need to know whether it makes sense to wait or invest time on writing something on my own... Thanks! |
In multiplatform projects, JavaScript compilation fails when applying Assuming polymorph JavaScript serialization will be supported in the future, I still want to be able to apply To not break compilation, I do the following. In common:
In JS:
In JVM:
The only way I could get the Perhaps this is of use to others. |
Javascript has a number of limitations:
|
I got polymorph serialization up and running in JavaScript for my particular use case (in line with the alternative suggested by @pdvrieze). It relies on specifying custom serializers on class members which need to be serialized as polymorph. Serializing objects as polymorph is thus explicit rather than implicit. Otherwise, JavaScript compilation warnings occur if no explicit serializer is specified. This seems very much in line with the design proposal for Kotlin 1.3.
To make this work with JavaScript I simply adopted the existing
Subsequently, in This serializer can be used as follows (full examples in unit tests).
For lists, the following serializer can be specified: The primary limitation of this approach is that you cannot register classes with the same name. Less severe is the extra step of having to register serializers manually in any polymorph type. However, the big benefit is the same codebase can now be used to serialize from/to Java and JavaScript. After having looked into some JavaScript serialization libraries which also do not support proper polymorph serialization, I am quite pleased with this result, and believe an approach like this is much preferred (for maintainability/scalability) over trying to do something similar outside of Kotlin. |
#394 has this and it is included into |
Great! Is there a readme on how to use this yet? I can't seem to find it. |
For those looking for information: registering polymorphic types needs to be done using An example on how to initialize |
One caveat here that has hit me. The lookup now uses the name property of the serialdescriptor instead of the class name (old behaviour). While the default value for this name is the type name, this can be overridden by specifying |
Currently
PolymorpicSerializer
works only in JVM.The text was updated successfully, but these errors were encountered: