I’m just starting with the topic serialization and wonder whether/how I can serialize my objects storing references (preserving objet identities) and not storing object copies. I use kotlinx.serialization.
For example, I have a class B which stores a list of instances of A.
@Serializable
data class A(val name: String)
@Serializable
data class B(val name: String) {
val listOfAs = mutableListOf<A>()
}
I can now, lets say, create two instances of B (b1 and b2) which both add a reference to one instance of A (a). I can serialize the objects:
val json = Json(JsonConfiguration.Stable)
val a = A("XXX")
val b1 = B("B1")
b1.myA.add(a)
val b2 = B("B2")
b2.myA.add(a)
val jsonData = json.stringify(B.serializer().list, listOf(b1, b2))
But when I deserialize the data, I don’t get one (!) instance of A but two. That is, my instances of B both reference their own instance of A.
val myBs = json.parse(B.serializer().list, jsonData)
So I wonder if it is possible to preserve the object identities somehow? (And I hope that the problem description is clear. )