Giter Club home page Giter Club logo

Comments (4)

sandwwraith avatar sandwwraith commented on August 25, 2024

The root of the problem is that PolymorphicSerializer was created in a such way that objects written by him are intended to be easily restored in statically typed language. So, efficiently, here you are writing a List<Any>. If you try deserialize it back, parser will fail even with primitives: there's no way to properly create Any from String (except for just coercing it); moreover, parser doesn't know, if he supposed to read string or int or double now. Indeed, right type could be inferenced from parsed string, but it will make parser more complex and probably slower.

So my suggestion is to create something that could be probably called ToDynamicSerializer, which is intended to write strings that can be easily consumed by parsers which don't need full type info (e.g. JavaScript). It can omit types either for primitives or for all objects, or may be tunable - this should be designed. If you have some use cases for it, please feel free to post it here.

from kotlinx.serialization.

csbence avatar csbence commented on August 25, 2024

My use case is the following. I have dynamic data store, that has fixed elements with a type and dynamic fields where the type might be unknown.

class MixedDataStore(private val valueStore: MutableMap<String, Any?> = hashMapOf()) {
    var foo: String by valueStore
    var bar: Long by valueStore

    operator fun get(key: String): Any? = valueStore[key]
    fun <T> getTypedValue(key: String): T? = this[key] as? T
    fun contains(key: String) = valueStore.containsKey(key)
}

from kotlinx.serialization.

pdvrieze avatar pdvrieze commented on August 25, 2024

Note that omitting the wrapping and explicit typing could be omitted by the encoder/decoder. Obviously they then need an alternative way to indicate the type of the object encoded/decoded.

from kotlinx.serialization.

mgroth0 avatar mgroth0 commented on August 25, 2024

Hey, I stumbled upon this while trying to figure out how to register primitives and nulls. I made a stack overflow question about this exact problem here: https://stackoverflow.com/questions/72325605/how-to-properly-register-primitives-and-nulls-in-polymorphic-serialization

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.