Closed physics515 closed 5 months ago
Nevermind, I fount the answer elsewhere in the docs.
You have to include serde default
for it to fallback.
use serde_aux::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct Object {
#[serde(deserialize_with = "deserialize_number_from_string")]
property1: u64,
#[serde(deserialize_with = "deserialize_number_from_string")]
property2: u64,
#[serde(default, rename = "sometimesProperty", deserialize_with = "deserialize_option_number_from_string")]
sometimes_property: Option<u64>,
}
I have a JSON object
and a rust struct link this:
This works as long as
sometimesProperty
exists on the json but if it doesn't then I get a Serde error saying that it expectedsometimesProperty
.Is this expected behavior?