Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stringify writes null values incorrectly #289

Closed
davidbrazilparker opened this issue Nov 27, 2018 · 2 comments
Closed

Stringify writes null values incorrectly #289

davidbrazilparker opened this issue Nov 27, 2018 · 2 comments

Comments

@davidbrazilparker
Copy link

davidbrazilparker commented Nov 27, 2018

I mentioned this in issue #58 But I think it is it's own issue altogether after doing some digging.

Here's an example of a data class that I serialize which has an optional serializable parameter:

@Serializable
data class Result( @Optional @SerialName("status_id") var statusId: Int? = null)

Currently with how the Kotlinx serialization library is written we write to our JSON map the value of null as a Kotlin String "null".

So for the example above the resulting JSON.stringify() return value would look as follows:

{
    "status_id":"null"
}

Cool so this will mean I'm not going to change this value when I POST this payload right? NOPE

"null" is not a valid JSON null value for the API I'm sending POST requests to. Now I'll admit I don't have much experience with API's (I'm learning 🤓). But for my use case in particular this a huge issue. Now for what I think could be the solution.

In the following file:
https://github.com/Kotlin/kotlinx.serialization/blob/master/runtime/common/src/main/kotlin/kotlinx/serialization/json/JsonParser.kt#L23

Change this line:

internal const val NULL = "null"

to

internal const val NULL = null

@qwwdfsad
Copy link
Collaborator

@Serializable
data class Clz(@Optional @SerialName("status_id") var statusId: Int? = null)

@Test
fun foo() {
  val r = Clz()
  println(Json.plain.stringify(Clz.serializer(), r))
}

Will print {"status_id":null} in current dev branch.
Could you please provide a self-contained example?

@davidbrazilparker
Copy link
Author

Ah must be my current version. I'll look into this and see if I can resolve it by updating. Thanks for looking into this.

@qwwdfsad qwwdfsad closed this as completed Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants