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
There is a bit of a dance that happens when deserializing a struct, and one of the fields is missing. serde_codegen effectively does:
Was the field attributed with #[serde(default)]? If so, set the value Default::default(), assuming that the field type actually implements Default.
Otherwise, call the Deserializer's missing_field() method. If the format has some built-in support for a value for a missing field, such as JSON considering a missing value as a null value, try to deserialize from that value, or return an error.
This unfortunately means that as a consumer, you may have to newtype yourself an implementation of Default. Instead, it might be better if we optionally allowed for #[serde(default = $expr)], where if the value is not present, we evaluate some expression. This would cut down on some of the boilerplate necessary for using serde_codegen.
Open question: If we do implement this, is this expression being evaluated in the same scope as Deserialize::deserialize? On one level it'd be potentially interesting that it'd be able to access one of the other values to implement itself, but I consider the serde_codegen generated bodies to not be a stable interface. So I'm leaning towards us putting the $expr into a sub-function to keep it from accessing the internal deserialization state.
cc #198, which has a similar idea for specifying alternative serializers/deserializers.
The text was updated successfully, but these errors were encountered:
This makes it possible to build `traits` without `std`. For this a new
trait `BasicFloat` was introduced, implementing some basic functionality
that works with `core`. Most notably this is lacking functions like
`cos`, `sin`, etc.
`Float` is not available without `std`.
Refs serde-rs#216.
rubdos
pushed a commit
to rubdos/serde
that referenced
this issue
Jun 20, 2017
traits: Introduce std feature
This makes it possible to build `traits` without `std`. For this a new
trait `BasicFloat` was introduced, implementing some basic functionality
that works with `core`. Most notably this is lacking functions like
`cos`, `sin`, etc.
`Float` is not available without `std`.
Refs serde-rs#216.
There is a bit of a dance that happens when deserializing a struct, and one of the fields is missing.
serde_codegen
effectively does:#[serde(default)]
? If so, set the valueDefault::default()
, assuming that the field type actually implementsDefault
.Deserializer
'smissing_field()
method. If the format has some built-in support for a value for a missing field, such as JSON considering a missing value as anull
value, try to deserialize from that value, or return an error.This unfortunately means that as a consumer, you may have to newtype yourself an implementation of
Default
. Instead, it might be better if we optionally allowed for#[serde(default = $expr)]
, where if the value is not present, we evaluate some expression. This would cut down on some of the boilerplate necessary for usingserde_codegen
.Open question: If we do implement this, is this expression being evaluated in the same scope as
Deserialize::deserialize
? On one level it'd be potentially interesting that it'd be able to access one of the other values to implement itself, but I consider theserde_codegen
generated bodies to not be a stable interface. So I'm leaning towards us putting the$expr
into a sub-function to keep it from accessing the internal deserialization state.cc #198, which has a similar idea for specifying alternative serializers/deserializers.
The text was updated successfully, but these errors were encountered: