Serialization: Json.encodeToString uses only changed variables

Hello. I’m trying to encode a data class this way:
Json.encodeToString(RequestData.serializer(), requestData)
But in the result I got only changed variables and it doesn’t include all available vars (or vals)

val requestData = RequestData(page = 1, page_size = 2) = 3


Expected : {"page":3, "page_size":2}  
Actual : {"page":3}

Also within the constructor it uses only variables from static classes:

data class RequestData(
        var page: Int = 1, // will NOT be used
        var page_size: Int = MAX_PAGE_SIZE // will be used
        var range: Range? = Range("2", "3") // not used
        data class Range(
            var from: String,
            var to: String

And I’m not sure why.

Found same case here

By default properties with default values are ignored. To change this behavior use:

Json {
    encodeDefaults = true
1 Like

I tried to use encodeDefaults = true but it didn’t help.

val json = Json { encodeDefaults = true}
json.encodeToString(RequestData.serializer(), requestData)

So, I changed data classes to classes as it was described in the GitHub case and it works

UPD: oops. All is working even with data classes.

1 Like

New question: is it possible to exclude nulls with encodeToString method while encodeDefaults = true? Looks like a popular topic.