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
Like this example, the enum has a serde derive #[serde(tag = "type", content = "value")], and skip_serializing_if of the second variant not works:
version: serde 1.0.197, serde_json 1.0.115
#[derive(Debug,Serialize,Deserialize)]pubstructFoo(String,u64,#[serde(skip_serializing_if = "String::is_empty")]String);#[derive(Debug,Clone,Serialize,Deserialize)]#[serde(tag = "type", content = "value")]pubenumBar{One{#[serde(skip_serializing_if = "String::is_empty")]value:String},// `skip_serializing_if` not works.Two(#[serde(skip_serializing_if = "String::is_empty")]String),}#[test]fntest_foo_bar(){let foo = Foo("hello".into(),1,"".into());let json = serde_json::to_string(&foo).expect("`to_string` should be ok");assert_eq!(&json, r#"["hello",1]"#);// skipped as expectedlet bar = Bar::One{value:"".into()};let json = serde_json::to_string(&bar).expect("`to_string` should be ok");assert_eq!(&json, r#"{"type":"One","value":{}}"#);// skipped as expectedlet bar = Bar::Two("".into());let json = serde_json::to_string(&bar).expect("`to_string` should be ok");assert_eq!(&json, r#"{"type":"Two"}"#);// assert failed, because json is `{"type":"Two","value":""}`}
The text was updated successfully, but these errors were encountered:
Like this example, the enum has a serde derive
#[serde(tag = "type", content = "value")]
, andskip_serializing_if
of the second variant not works:version: serde 1.0.197, serde_json 1.0.115
The text was updated successfully, but these errors were encountered: