Comments (5)
@class
should be a configurable field name. E.g. in Jackson:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@Type(value = Car.class, name = "car"),
@Type(value = Truck.class, name = "truck")
})
This would be highly useful - things that get JSON-serialized are more often than not algebraic data types.
from kotlinx.serialization.
Another example in klaxon "Polymorphic classes" https://github.com/cbeust/klaxon#polymorphic-classes
[
{ "type": "rectangle", "width": 100, "height": 50 },
{ "type": "circle", "radius": 20}
]
@TypeFor(field = "type", adapter = ShapeTypeAdapter::class)
open class Shape(val type: String)
data class Rectangle(val width: Int, val height: Int): Shape()
data class Circle(val radius: Int): Shape()
class ShapeTypeAdapter: TypeAdapter<Shape> {
override fun classFor(type: Any): KClass<out Shape> = when(type as String) {
"rectangle" -> Rectangle::class
"circle" -> Circle::class
else -> throw IllegalArgumentException("Unknown type: $type")
}
}
from kotlinx.serialization.
Any plans for this?
from kotlinx.serialization.
Seems KInput
needs to be able to peek for values in order to implement a serial loader that can read polymorphic values with @class
.
from kotlinx.serialization.
Also, will be very useful to support multi-level inheritance.
An example will be for articles, it can be published or draft, and it can also have types: text, link, video.
[
{ "state": "draft", "type": "link", "url": "http..." },
{ "state": "published", "type": "link", "url": "http..." },
{ "state": "published", "type": "text", "content": "xyz" }
]
from kotlinx.serialization.
Related Issues (20)
- 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
- Support for `serializer<@Contextual T>` HOT 3
- Not a Bug But missing step in the Documentation when working with KMP HOT 1
- "Serializer for class List is not found" on Native. HOT 2
- [FEATURE] Allow defining a sealed class as a subclass of an open polymorphic class HOT 2
- @file:UseContextualSerialization doesn't work on Kotlin 2.0.20-RC2 HOT 1
- CBOR/COSE: support sorting of keys
- Deserializing, Transitient field use default value instead of the values from subclass constructor
- Implement additional extension serializers for frequently utilized data types in web requests. HOT 1
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.