gabin

    gabin

    2 years ago
    Hello guys. I’m looking for a workaround for an issue https://github.com/Kotlin/kotlinx.serialization/issues/195 Lets say I have a class
    @Serializable
    data class MyUser(
        val fullName: String,
        val middleName: String? = null,
        val age: Int = 42
    )
    I want to achieve the result when following object
    MyUser("John Doe")
    will lead to json:
    {"fullName": "John Doe", age=42}
    From what I understand
    Json(encodeDefaults = false)
    will disable adding default age. Please give an advice
    m

    marstran

    2 years ago
    You can use triple backticks ``` to format the code in a code block.
    Or mark the text and choose "Code block"
    gabin

    gabin

    2 years ago
    ok, thx
    any help regarding the problem?
    @elizarov could you help please?
    m

    miqbaldc

    2 years ago
    I think you need to annotate with
    @Transient
    or
    @Optional
    iinm cmiiw @gabin
    elizarov

    elizarov

    2 years ago
    Out-of-the-box you either have all defaults or none of them. To customize it, you’ll have to write your own serializer for this class or your own JSON format that whould treat nulls in some special way.