# XML serialization in Kotlin JavaScript

**URL:** <https://discuss.kotlinlang.org/t/xml-serialization-in-kotlin-javascript/1585>\
**Category:** JavaScript\
**Created:** [April 7, 2016, 1:30pm UTC](https://discuss.kotlinlang.org/t/xml-serialization-in-kotlin-javascript/1585 "2016-04-07T13:30:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jean79](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@jean79](https://discuss.kotlinlang.org/u/jean79)\
**Post date:** [April 7, 2016, 1:30pm UTC](https://discuss.kotlinlang.org/t/xml-serialization-in-kotlin-javascript/1585/1 "2016-04-07T13:30:35Z")

</div>

Hi,  
I currently get the data from/to Java backend into Kotlin JavaScript via simple Ajax call like this:  
class Person(name:String, age:Int)  
val person = (Person)JSON.parse("{ name: ‘Jan’, age: 12 } ") //json from some ajax rest call  
this works fine for simple classes. However I would like to use data classes like:  
data class Person(name:String, age:Int)  
to be able to use data classes features.

Does JavaScript Kotlin now supports reflection? Or is it in a roadmap? Is anybody using some XML/JSON parser from json/xml/protobuffs into Kotlin data classes?  
Thanks,  
Jan

---

<div class="post-metadata">

**Author:** ![sergey.mashkov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sergey.mashkov/32/1678_2.png) [@sergey.mashkov](https://discuss.kotlinlang.org/u/sergey.mashkov)\
**Post date:** [April 7, 2016, 2:51pm UTC](https://discuss.kotlinlang.org/t/xml-serialization-in-kotlin-javascript/1585/2 "2016-04-07T14:51:00Z")

</div>

Unfortunately there is no standard solution for this yet. I would start from this:

```kotlin
data class Person(val name: String = "", val age: Int = 0)

fun <T : Any> deserialize(json: String, receiver: T): T {
    val d: dynamic = receiver

    JSON.parse<T>(json) { key, value ->
        d[key] = value
        d
    }

    return receiver
}

fun main(args: Array<String>) {
    val p = deserialize("{\"name\": \"uuu\", \"age\": 7}", Person())
    console.log(p.toString())
}

```

it does not recursive deserialization so you need to think deeper

**UPD**  
You also better to check browsers compatibility for `JSON.parse`
