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
I'm sorry in advance if this is a bad suggestion, I'm new to Rust. However, I find myself having a problem where I need to deserialize some settings and I need sane defaults for some of the fields, and serde does not provide a good way for me to do that.
I would like to make the ports and available optional, but I would like hostname (and other fields not shown here) to be mandatory. However, there is no good way to tell serde to use defaults for these fields, short of littering my code with one function for each field and telling serde to call that.
It would help a lot if I could just say something like #[serde(default=110)] and have serde assign that value if that field were missing from the JSON file.
The text was updated successfully, but these errors were encountered:
The challenge is that if default=110 means assign 110 as the default value, then one would assume default="x" means assign "x" as the default value. Instead it means assign x().
Let us know in the other issue if you have a preference for how to solve this.
I'm sorry in advance if this is a bad suggestion, I'm new to Rust. However, I find myself having a problem where I need to deserialize some settings and I need sane defaults for some of the fields, and serde does not provide a good way for me to do that.
I have a struct that looks like this:
I would like to make the ports and
available
optional, but I would likehostname
(and other fields not shown here) to be mandatory. However, there is no good way to tell serde to use defaults for these fields, short of littering my code with one function for each field and telling serde to call that.It would help a lot if I could just say something like
#[serde(default=110)]
and have serde assign that value if that field were missing from the JSON file.The text was updated successfully, but these errors were encountered: