Klaxon parse null

222 Views Asked by At

I have issue when using Klaxon 5.5

Class :

data class QRResponse(
   @field:SerializedName("qrType")
   val qrType: String? = null,

   @field:SerializedName("qrData")
   val qrData: String? = null
)

Code :

val dataContents = result.contents
Log.d("ScanQRData", "result.contents : $dataContents")

val dataQR = Klaxon().parse<QRResponse>(dataContents)
Log.d("ScanQRData", "dataQR : $dataQR")

Result :

ScanQRData: result.contents : {"qrType": "product", "qrData":"352307811"}

ScanQRData: dataQR : QRResponse(qrType=null, qrData=null)

Any suggestion what happened to qrType and qrData null after parse from Klaxon?

1

There are 1 best solutions below

0
On

Klaxon doesn't process @field:SerializedName annotation (where you have imported it from?). The correct way to customize mapping between your JSON documents and Kotlin objects in Klaxon is @Json annotation:

data class QRResponse(
    @Json(name = "qrType")
    val qrType: String? = null,

    @Json(name = "qrData")
    val qrData: String? = null
)