Shan

    Shan

    3 years ago
    Hey all, getting my feet wet with Kotlinx Serialization and have a question: if I have an API request (in JSON) form which I only need, say, 3 primitives from for my usecase, what is the best practice way to define a @ Serializable data class to process this request? Do I have to generate a data class with every possibly entry and set default values, or can I just make a data class that only has the primitives I need and process it before the data class is generated somehow?
    uhe

    uhe

    3 years ago
    You mean like this?
    import kotlinx.serialization.Serializable
    import kotlinx.serialization.json.Json
    
    
    fun main() {
        @Serializable
        data class Request(val relevant: Int)
        val json = """ { "relevant" : 42, "not-relevant" : 23 }"""
        val relevant = Json.nonstrict.parse(Request.serializer(), json).relevant
        println(relevant) // 42
    }
    Note the
    nonstrict
    part