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

Polymorphic primitive collection serialization. #40

Open
csbence opened this issue Dec 1, 2017 · 4 comments
Open

Polymorphic primitive collection serialization. #40

csbence opened this issue Dec 1, 2017 · 4 comments

Comments

@csbence
Copy link

csbence commented Dec 1, 2017

When serializing a polymorphic collation of primitives using the PolymorphicSerializer the type is always included. Including the type of the primitives might not be necessary. I feel like this is a common use case thus it should be addressed.

Example

val list = listOf(1, 2.0, "a")
val json = JSON.stringify(PolymorphicSerializer.list, list)
println(json) 
// [["kotlin.Int",1],["kotlin.Double",2.0],["kotlin.String","a"]]
// jackson would output the following: [1, 2.0, "a"]

Possible solutions

  1. PolymorphicSerializer::save could omit the type information when serializing primitives or standard collections.
  2. Configurable PolymorphicSerializer class or a different object that behaves as described above. It might be desired to fine tune such behavior.
@csbence csbence closed this as completed Dec 1, 2017
@csbence csbence changed the title Polymorphic primitive serialization. Polymorphic primitive collection serialization. Dec 1, 2017
@csbence csbence reopened this Dec 1, 2017
@sandwwraith
Copy link
Member

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.

@csbence
Copy link
Author

csbence commented Dec 28, 2017

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)
}

@pdvrieze
Copy link
Contributor

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.

@mgroth0
Copy link

mgroth0 commented May 20, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants