Java/Kotlin JSON parsing improvement
You can create a generic container/entity for "data" and "metadata" and reuse it. e.g.:



data class MEntity&lt;T&gt;(
        var data: T? = null,
        var metadata: String? = null
)

data class User(
        var name: String? = null,
        var pid: String? = null,
        var position: MEntity&lt;Position&gt;? = null
)

data class Position(
        var x: String? = null,
        var y: String? = null
)


Due to type erasure you must use a TypeToken to deserialize the JSON into a generic type:

gson.fromJson&lt;MEntity&lt;User&gt;&gt;(json, object : TypeToken&lt;MEntity&lt;User&gt;&gt;() {}.type)


This isn't very convenient but thankfully Kotlin allows us to use reified type parameters to define an extension function to simplify this:

inline fun &lt;reified T&gt; Gson.fromJsonToGeneric(json: String): T {
    return fromJson(json, object : TypeToken&lt;T&gt;() {}.type)
}


Now the usage becomes much simpler:

val userMEntity = gson.fromJsonToGeneric&lt;MEntity&lt;User&gt;&gt;(json)
println(userMEntity)


Output:

MEntity(data=User(name=Start, pid=1, position=MEntity(data=Position(x=31, y=330), metadata=empty)), metadata=empty)