cbeust / klaxon

A JSON parser for Kotlin
Apache License 2.0
1.85k stars 121 forks source link

`JsonObject` created by `toJsonObject` from a derived class will not contain members of any superclass. #349

Open TreffnonX opened 2 years ago

TreffnonX commented 2 years ago

Superclass members are not included in the JsonObject generated from an object with toJsonObject.

Setup:

class A (val uuid: UUID) {
    var name: String = "Something"
}

class B (val rank: Int, uuid: UUID) : A (uuid) {
    fun toJson() = Klaxon().toJsonObject(this)
}

B(rank = 4, uuid = UUID.randomUUID()).toJson() // will lack members uuid and name.

Expected: JsonObject contains all members of A and B

Actual: JsonObject contains only member of class B

This is very surprising as it works for the toJsonString method. There all members are contained.

The reason is possibly the line obj::class.declaredMemberProperties in class JsonValue, which could be written as obj::class.memberProperties instead.

TreffnonX commented 2 years ago

Fixed this in PR #350.