Issue
Given json as follows where the structure of the payload
object will vary:
{
"id": 1,
"displayName": "Success",
"payload": {
"someProperty": "example",
"someOtherProperty": {
"someNestedProperty": "example"
}
}
}
...using kotlinx.serialization
how can I deserialize this into the following data class, where the value of payload
should be the raw json string of the payload object.
@Serializable
data class Stub(
val id: Int,
val displayName: String,
val payload: String
)
Solution
Struggled to find a way of doing this with Serializers, but it was simple enough to implement manually using JsonElement
.
val jsonObject = Json.parseToJsonElement(jsonString).jsonObject
val stub = Stub(
jsonObject["id"]!!.jsonPrimitive.int,
jsonObject["displayName"]!!.jsonPrimitive.content,
jsonObject["payload"]!!.toString()
)
Answered By - user1173706
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.