# Deserializing map with non-serializable objects

**URL:** <https://discuss.kotlinlang.org/t/deserializing-map-with-non-serializable-objects/20901>\
**Category:** Libraries\
**Created:** [February 16, 2021, 10:50pm UTC](https://discuss.kotlinlang.org/t/deserializing-map-with-non-serializable-objects/20901 "2021-02-16T22:50:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![karen.frangulyan](https://avatars.discourse-cdn.com/v4/letter/k/dec6dc/32.png) [@karen.frangulyan](https://discuss.kotlinlang.org/u/karen.frangulyan)\
**Post date:** [February 16, 2021, 10:50pm UTC](https://discuss.kotlinlang.org/t/deserializing-map-with-non-serializable-objects/20901/1 "2021-02-16T22:50:44Z")

</div>

I came up with an interesting problem and want to understand how flexible/powerful is Kotlin’s **serialization** library to help me.

I need to deserialize from `Map<String, Any>` - I am getting it from a third-party SDK, which already processes JSON result from network call and creates a `Map` for me. This turns out to be a common problem, and the [solution suggested from Kotlin team](https://github.com/Kotlin/kotlinx.serialization/issues/746) is to convert that map to `JsonObject` and then deserialize it.

There is one little problem though - the third party library may put its own SDK objects into that map, once it recognizes them in the JSON result from network ([documented here](https://github.com/parse-community/Parse-SDK-Android/blob/master/parse/src/main/java/com/parse/ParseCloud.java#L79)). Those objects are not serializable, I don’t have access to them to modify, and I need them untouched in order to be able to use later with other SDK functionality.

Basically, I can break down the problem to having a serializable class:

```
@Serializable
data class MyClass(
    val someNumber: Int,
    val someString: String,
    val customNonSerializableObject: ThirdPartySDKClass
)

```

and then having a map:

```
// this object is coming from SDK codes
mapOf("someNumber" to 42, "someString" to "hello", "customNonSerializableObject" to ThirdPartySDKClass())

```

Is there a way I can use kotlin’s serialization to get `MyClass` from that map?

The ugly way would be to define `customNonSerializableObject` as optional, convert the map to `JsonObject`, deserialize it, and then manually assign:

```
myClassObj.customNonSerializableObject = map["customNonSerializableObject"]

```

which is basically hardcoding for each endpoint call type.

Maybe somehow define a decoder as [shown here](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/formats.md#basic-decoder), delegate everything to super class, and for the custom objects just copy the reference to the target without any change, is that possible?
