Kotlin / kotlinx.serialization

Kotlin multiplatform / multi-format serialization
Apache License 2.0
5.36k stars 619 forks source link

Class with non properties can not be automatically serializable #501

Open majkrzak opened 5 years ago

majkrzak commented 5 years ago

Describe the bug When class constructors contains non property parameter it can not be made serializable with @Serializable annotaion

To Reproduce

@Serializable
class Foo(bar: String){
    val lol: String = bar.toLowerCase()
}

Expected behavior

Json.stringify(Foo.serializer(), Foo("LoL"))

{
    "lol": "lol"
}

Environment

sandwwraith commented 5 years ago

This is a conceptual limitation. See, on deserialization, when the lol field is missing, you need to recreate it as bar.toLowerCase() (properties with default values are considered optional). But you can't because you don't have bar. It's not even a field, so it has no chance to be serialized. Therefore such classes can't be serializable.

majkrzak commented 5 years ago

You are messing serialization and deserialization. Also class can be deserialized if delegate provides the setvalue

sandwwraith commented 5 years ago

Yes, I'm talking about deserialization. While 'serializable-only' concept is legit and has its use cases, this library focuses on making both processes available without corner cases, so making a class serializable and non-deserialziable at the same time is a non-goal.

Also class can be deserialized if delegate provides the setvalue

That's what I've mentioned in the issue about delegates.

majkrzak commented 5 years ago

Sorry, I've messed the tickets.