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

how do I send a null field to the server? #117

Open
KotlinWay opened this issue Sep 20, 2021 · 3 comments
Open

how do I send a null field to the server? #117

KotlinWay opened this issue Sep 20, 2021 · 3 comments

Comments

@KotlinWay
Copy link

KotlinWay commented Sep 20, 2021

How do I put null in the network model field?
I have a model like this

@jsonapi(type = "notes")
data class Note(
var name: String? = null,
var description: String? = null,
var note: String? = null,
@field:Json(name = "created-at")
var createdAt: Date? = null
}

I want to patch the null value in the createdAt field to the server, but this field is ignored and it simply does not exist in the body.
{"data":{"type":"notes","id":"168356"}}
How can I send null?

@kamikat
Copy link
Owner

kamikat commented Sep 23, 2021

Use serializeNulls call on a JsonAdapter object may help:

moshi.adapter(Document.class).serializeNulls().toJson(document)

For more adapter options: https://github.com/square/moshi#adapter-convenience-methods

@5treetArt
Copy link

5treetArt commented Dec 9, 2021

@kamikat, .serializeNulls() method is useless in this case because of skipping null fields in ResourceAdapter.writeFields. Of course this skip can be avoided by setting writer.setSerializeNulls(true), but in this case all the null fields of this object will be written into JSON. Which is not the goal, because we want to write only one specific field as null. It will be good to have something like @SerializeNulls annotation for the field.

@antaki93
Copy link

antaki93 commented Apr 6, 2023

There is a custom solution on the StackOverflow: https://stackoverflow.com/a/52265735/7918717

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants