Hello, I am trying to serialize/deserialize a clas...
# serialization
g
Hello, I am trying to serialize/deserialize a class with an inner class. Upon deserialization, the inner class does not have a reference to its enclosing outer class, and will throw a null pointer exception when referencing a property of the outer class. Below is an example. Class definition:
Copy code
@Serializable
class OuterTestClass(val x: Int){
    val inner = InnerTestClass(5)

    @Serializable
    inner class InnerTestClass(val y: Int){
        fun compute(): Int{
            return x*y
        }
    }
}
Example test, where a null pointer exception is thrown at the call to o2.inner.compute():
Copy code
@Test
fun testInnerClassesAreSerializable(){
    val o1 = OuterTestClass(3)
    val str = Json.encodeToString(o1)
    val o2 = Json.decodeFromString<OuterTestClass>(str)

    assertEquals(o1.x, o2.x)
    assertEquals(o1.inner.y, o2.inner.y)
    assertEquals(o1.inner.compute(), o2.inner.compute())
}
The intermediate JSON string looks like what I would expect: {"x":3,"inner":{"y":5}} I am using Kotlin 1.4.10 and Kotlinx Serialization version 1.0.0-RC. I checked the serialization documentation for any exclusions for inner class support but didn't find anything that looked relevant, and also checked open issues on Github. Does anyone know if this is an intentional unsupported use case, or otherwise a known open issue? Thanks.
s
I think this is more of an open issue — we have not tested this functionality yet, so please open a new ticket on github