You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With this code, if I deserialize { "thing": { }, "other": 0, }, I end up with enabled = true because the enabled field is not present, so we use the enabled_true function to fill it, but if I deserialize { "other": 0 }, then thing is not present, so we use its Default implementation, which will set enabled = false.
Proposal: let's move to #[serde(default)] on the struct everywhere, with a custom Default implementation for the struct if we need some specific default values.
The text was updated successfully, but these errors were encountered:
The
#[serde(default)]
attribute can be set on the struct and on the filed:This was confusing to me because in some cases, I did not get the default values I was expecting. Example:
With this code, if I deserialize
{ "thing": { }, "other": 0, }
, I end up withenabled = true
because theenabled
field is not present, so we use theenabled_true
function to fill it, but if I deserialize{ "other": 0 }
, thenthing
is not present, so we use itsDefault
implementation, which will setenabled = false
.Proposal: let's move to
#[serde(default)]
on the struct everywhere, with a customDefault
implementation for the struct if we need some specific default values.The text was updated successfully, but these errors were encountered: