# Serialization: Preserving object identities?

**URL:** <https://discuss.kotlinlang.org/t/serialization-preserving-object-identities/16412>\
**Category:** Libraries\
**Created:** [February 20, 2020, 7:43am UTC](https://discuss.kotlinlang.org/t/serialization-preserving-object-identities/16412 "2020-02-20T07:43:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![beedaddy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/beedaddy/32/6519_2.png) [@beedaddy](https://discuss.kotlinlang.org/u/beedaddy)\
**Post date:** [February 20, 2020, 7:43am UTC](https://discuss.kotlinlang.org/t/serialization-preserving-object-identities/16412/1 "2020-02-20T07:43:56Z")

</div>

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.

```kotlin
@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:

```kotlin
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.

```kotlin
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. 🙂 )

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [February 20, 2020, 8:04am UTC](https://discuss.kotlinlang.org/t/serialization-preserving-object-identities/16412/2 "2020-02-20T08:04:09Z")

</div>

Short answer: that’s not possible out of the box.

Long answer: in your example there are multiple levels of abstraction that do not support what you need.

It starts with the fact that json does not know about object identities. You would need to encode the identity information into your objects yourself, for example with some custom id property.

That’s not enough though, as out of the box this id property will not be used by kotlinx-serialization for object identities. You would need to configure your own serializer which will reuse objects when they already exist for a given id, for example by using a weakhashmap object pool.
