I have these Model classes
@Serializable
open class BaseModel(
var network: String? = null,
var type: String? = null,
var createdOn: String? = null,
var updatedOn: String? = null,
var name: String? = null,
var imageUrl: String? = null,
var description: String? = null,
var validity: String? = null
)
@Serializable
open class BundleOffer(
var sms: String? = null,
var onNet: String? = null,
var offNet: String? = null,
var internet: String? = null
) : BaseModel()
now these classes define in kotlin-jvm
module and i need to create child type for framework-specific use case so i have created test for that like that
@Serializable
class BundleOfferChildType(val isFavourite: Boolean = false) : BundleOffer()
@Test
fun decodeArrayOfBundleOfferAsChildType() {
val jsonString = javaClass.classLoader
.getResourceAsStream("test_bundles_offers_data.json")!!
.readBytes().toString(Charsets.UTF_8)
val json = Json {
encodeDefaults = true
ignoreUnknownKeys = true
}
val result =
json.decodeFromString(ListSerializer(BundleOfferChildType.serializer()), jsonString)
assertThat(result.size).isEqualTo(2)
}
i have test_bundles_offers.json
file data as below but there is no createdOn
,updatedOn
in data and giving MissingFieldException for them but these should be ignored or should be assigned default value as defined in Json configuration
[
{
"network": "...",
"type": "...",
"name": "Bundle offer 1",
"imageUrl": "...",
"description": "...",
"validity": "...",
"sms": "...",
"offNet": "...",
"onNet": "...",
"internet": "..."
},
{
"network": "...",
"type": "...",
"name": "Bundle offer 2",
"imageUrl": "...",
"description": "...",
"validity": "...",
"sms": "...",
"offNet": "...",
"onNet": "...",
"internet": "..."
}
]