Comments (4)
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.
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.
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.
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)
- Allow (de-)serializing value classes if their backing property is serializable HOT 1
- Cannot serializer flow of Objects HOT 4
- kotlinx.serialization.SerializationException: Enum class Country does not contain element with name 'MISSING' at path $[2] HOT 1
- [Possible duplicate] Small inconsistency in the logic of encoding of fields with defaults HOT 1
- Didn't find class "kotlinx.serialization.json.Json" on path: DexPathList HOT 1
- typealiased @Serializable cannot be used as type hints HOT 1
- Value classes not using encodeInlineElement and inefficient boxing of primitive values
- Errors revealed only on run-time HOT 1
- ERROR: org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationResolveExtension caused LinkageError HOT 3
- Allow annotation meta-annotations with Serializable
- Streaming performance: kotlinx-io & Okio integrations
- kotlinx.serialization.json deserialize error HOT 1
- Quoted booleans are being allowed even with `isLenient = false` HOT 6
- very strange serialization HOT 2
- The built-in String type (JsonPrimitive/JsonLiteral) puts double quotes around the string value HOT 8
- Don't check kind if needn't type discriminator for enum and primitive HOT 5
- 2.0.20 with generatedSerializer and reference to interface: SealedClassSerializer init: subclassSerializers contains null HOT 2
- [protobuf] How to encode/decode map with null value?
- Option to serialize enums as lowercase/snake_case HOT 4
- hande single quotes when `isLenient=true` HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlinx.serialization.