I'm using Gsson
to convert JSON to Kotlin data class
. While creating data class object I want to parse the value in init
method. But init
never called and generated object's values are null.
data class Configuration (val groupId:Int, val key:String, val source:String){
val query:String
val search:String
init {
val obj = JSONObject(key)
query = obj.getString("q")
search = obj.getString("search")
}
}
When I got the object of Configuration from Gson
, query
and search
value is always null
and init
block was never executed. I also tried constructor
and without data
keyword, results are same.