iddm / serde-aux

An auxiliary serde library providing helpful functions for serialisation and deserialisation for containers, struct fields and others.
MIT License
152 stars 26 forks source link

Cannot deserialize null #17

Closed GTB3NW closed 3 years ago

GTB3NW commented 3 years ago

Hi I'm having an issue with conditional serialization.

#[serde(rename = "price")]
#[serde(deserialize_with = "deserialize_number_from_string")]
price: u32,

The payload: "price":null

I can't change price to Option<u32> since it isn't compatible with deserialize_number_from_string.

I think this is a bug?

GTB3NW commented 3 years ago
error[E0277]: the trait bound `std::option::Option<u32>: FromStr` is not satisfied
   --> src/types.rs:91:32
    |
91  |     #[serde(deserialize_with = "deserialize_number_from_string")]
    |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `FromStr` is not implemented for `std::option::Option<u32>`
    | 
   ::: <REDACTED>
    |
111 |     T: FromStr + serde::Deserialize<'de>,
    |        ------- required by this bound in `serde_aux::field_attributes::deserialize_number_from_string`
GTB3NW commented 3 years ago

I have now RTFM - deserialize_option_number_from_string

Sorry for any notification!