I use Moshi for parse json from server. if server send null for item default value not set! but server not send that item default value set.
json:
{"percentChange": null,"change": "-2500.00","value": "130000","name": null}
data class:
@JsonClass(generateAdapter = true) data class Reference(val name:String? = "-",val value: Double,val change: Double,val percentChange: Double? = -10.0,)
but data for name and percentChange is null that should "-" for name and "-10.0" for percentChange. if server not send name and percentChange, default value work, but if send that null default value not work!
I use converter-moshi:2.4.0 and retrofit:2.4.0
Data classes are the replacements of POJOs in Java. Hence, it is natural to think that they would allow for inheritance in Java and Kotlin. The inheritance of data classes in Kotlin doesn't execute well. Hence, it is advised not to use inheritance by extending the data class in Kotlin.
Kotlin Data Class Requirements The parameters of the primary constructor must be marked as either val (read-only) or var (read-write). The class cannot be open, abstract, inner or sealed. The class may extend other classes or implement interfaces.
Data class is a simple class which is used to hold data/state and contains standard functionality. A data keyword is used to declare a class as a data class. Declaring a data class must contains at least one primary constructor with property argument (val or var).
This is working as intended because the null literal as a value for a key in JSON is semantically different than the absence of the key and value.
You can make a custom JsonAdapter for your use case.
@JsonClass(generateAdapter = true)
data class Reference(
@Name val name: String = "-",
val value: Double,
val change: Double,
val percentChange: Double? = -10.0
) {
@Retention(RUNTIME)
@JsonQualifier
annotation class Name
companion object {
@Name @FromJson fun fromJson(reader: JsonReader, delegate: JsonAdapter<String>): String {
if (reader.peek() == JsonReader.Token.NULL) {
reader.nextNull<Unit>()
return "-"
}
return delegate.fromJson(reader)!!
}
@ToJson fun toJson(@Name name: String): String {
return name
}
}
}
@Test fun reference() {
val moshi = Moshi.Builder()
.add(Reference)
.build()
val adapter = moshi.adapter(Reference::class.java)
val decoded = Reference("-", 130_000.toDouble(), (-2_500).toDouble(), null)
assertThat(adapter.fromJson(
"""{"percentChange": null,"change": "-2500.00","value": "130000"}"""))
.isEqualTo(decoded)
assertThat(adapter.fromJson(
"""{"percentChange": null,"change": "-2500.00","value": "130000","name": null}"""))
.isEqualTo(decoded)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With