-
Notifications
You must be signed in to change notification settings - Fork 766
Automatically serialize nulls when a nullable property has a non-null default value #1358
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
Comments
Yup, good call. Need to get this working. |
I think our hands are tied because Kotlin metadata won't tell us what the default value is, only that it has one. That default could be |
Could there be a potential issue if Moshi would always serialize the current value for nullable properties that have a default value? Or perhaps it shouldn't be the default behaviour, but a wrapper, like serializeNulls? I assume this loss of symmetry will mostly be an issue when you're using json locally? I can't think of a short name for that though. |
This came up locally for me but theoretically someone might write both a server and a client using the same base JSON model. I'm fine with this being an optional setting that writes all keys with a default value, though at that point, maybe |
Just using I'm kind of wondering why serializing nulls isn't the default behaviour actually? One that can potentially backfire when working with default values apparently. 😕 |
I'd like to chime in and say that some APIs treat POST |
That's a good point. We probably shouldn't change defaults since that could hurt a lot of existing projects. Maybe we should just close this out and say "use |
The following code, which writes an object to JSON then parses it again, fails at an equality check:
The reason it fails is because
toJson(Foo(null)) == "{}"
, yetfromJson("{}") == Foo("oops")
. That happens only because we're mixing a nullable property has a non-null default value.One solution is to serialize nulls. Theoretically, Moshi could serialize nulls on any property where:
null
.Thus avoiding this lack of symmetry. (In the above example, it would mean that
toJson(Foo(null)) == "{ \"bar\": null }"
)The text was updated successfully, but these errors were encountered: