Added support of named companion in serializable class to the FIR
Merge-request: KT-MR-7472
This commit is contained in:
committed by
Space Team
parent
8d8d0d9922
commit
d4cfb002d0
+56
@@ -0,0 +1,56 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
package com.example
|
||||
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.json.*
|
||||
import kotlinx.serialization.encoding.*
|
||||
import kotlinx.serialization.descriptors.*
|
||||
|
||||
|
||||
class NonSerializable {
|
||||
companion object {
|
||||
fun foo(): String {
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class WithCompanion(val i: Int) {
|
||||
companion object {
|
||||
fun foo(): String {
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class WithNamedCompanion(val i: Int) {
|
||||
companion object Named {
|
||||
fun foo(): String {
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
encodeAndDecode(WithCompanion.serializer(), WithCompanion(1), """{"i":1}""")?.let { return it }
|
||||
encodeAndDecode(WithNamedCompanion.serializer(), WithNamedCompanion(2), """{"i":2}""")?.let { return it }
|
||||
if (NonSerializable.foo() != "OK") return NonSerializable.foo()
|
||||
if (WithCompanion.foo() != "OK") return WithCompanion.foo()
|
||||
if (WithNamedCompanion.foo() != "OK") return WithNamedCompanion.foo()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
|
||||
private fun <T> encodeAndDecode(serializer: KSerializer<T>, value: T, expectedEncoded: String, expectedDecoded: T? = null): String? {
|
||||
val encoded = Json.encodeToString(serializer, value)
|
||||
if (encoded != expectedEncoded) return encoded
|
||||
|
||||
val decoded = Json.decodeFromString(serializer, encoded)
|
||||
if (decoded != (expectedDecoded ?: value)) return "DECODED=${decoded.toString()}"
|
||||
return null
|
||||
}
|
||||
Reference in New Issue
Block a user