Reputation: 1441
I have a class defined like this:
data class Medicalnfo(
@Json(name = "allergies") val allergies: List<Allergy>? = null
): ProfileModel(){
@Json(name = "has_allergies") val hasAllergies: Boolean = allergies != null && allergies.isNotEmpty()
}
The problem is that hasAllergies
is not included in the json serialization generated by Moshi. How can this be achieved?
Here is the test
@Test
fun medicalInfoJsonContainsFlags(){
val adapter = moshi.adapter(Medicalnfo::class.java)
val jsonStr = adapter.toJson(Medicalnfo(allergies = listOf())) //value is {"allergies":[]}
assert(jsonStr.contains("has_allergies"))
}
Upvotes: 3
Views: 959
Reputation: 151
If you make hasAllergies
as var it will work
data class Medicalnfo(
@Json(name = "allergies") val allergies: List<Allergy>? = null
): ProfileModel(){
@Json(name = "has_allergies") var hasAllergies: Boolean = allergies != null && allergies.isNotEmpty()
}
Upvotes: 2