Closed RoustamManookian closed 9 months ago
This sounds like a duplicate of #1657. Gson currently mainly supports Java; it also happens to support other JVM languages to some extent, such as Kotlin, but the support is limited.
It might be better if you used a JSON library with explicit Kotlin support, such as https://github.com/square/moshi.
Gson version
"com.squareup.retrofit2:converter-gson:2.9.0"
Java / Android version
Android Studio Giraffe | 2022.3.1 Patch 2
Used tools
Description
as described in the documentation The method deserializes the specified Json into an object of the specified class. Returns: an object of type T from the string. Returns null if json is null or if json is empty. Throws: JsonSyntaxException – if json is not a valid representation for an object of type classOfT
Expected behavior
If the JsonString is not a valid representation of the Class, I expect to thaw an exception.
Actual behavior
I get an Object of the Class with null values even though the Class parameters are not nullable.
Reproduction steps
data class Ip( val rpi_id: Int, val rpi_ip: String, val publicIPv4: String )
try { val jsonPing = Gson().fromJson( String(pingResponse.data), PingResponse::class.java ) this.ping.value = jsonPing Log.d("pingResponse", "⭕⭕⭕⭕ from $ping") Log.d("ping", "⛔🔔🔔🔔🔔JSONObject: ${String(pingResponse.data)} ")
2 This is the pingResponse String
⛔🔔🔔🔔🔔JSONObject: {"topic":"$aws/things/home/bash/exec","qos":0,"retain":false,"_msgid":"187805cecf3a3b42","payload":"","rc":{"code":1,"message":"Command failed: ping\nping: usage error: Destination address required\n"}}`And this is the ping Object that the method created. Pay attention that the properties thing_Name, Node_Version and ip are null.
⭕⭕⭕⭕ from MutableState(value=PingResponse(topic=$aws/things/home/bash/exec, thing_Name=null, Node_Version=null, ip=null))@135859162
Exception stack trace