diff --git a/__serialization/serde_json/src/main.rs b/__serialization/serde_json/src/main.rs index 2e04afd..cfaae17 100644 --- a/__serialization/serde_json/src/main.rs +++ b/__serialization/serde_json/src/main.rs @@ -55,6 +55,18 @@ struct TestStruct2 { test_description: Option, } +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(untagged)] +pub enum StringOrStringArray { + String(String), + Vec(Vec), +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +struct TestStruct3 { + sub: StringOrStringArray, +} + fn main() -> Result<()> { let serialized = serde_json::to_string(&TestStruct{ ty: TestEnum::TypeA, @@ -85,7 +97,7 @@ fn main() -> Result<()> { }); println!("{}", &john); - if let Option::Some(ref mut m) = john.as_object_mut() { + if let Some(ref mut m) = john.as_object_mut() { m.insert("test".into(), Value::Bool(true)); m.insert("test2".into(), Value::String("hello world".into())); } @@ -98,5 +110,11 @@ fn main() -> Result<()> { }; println!("{}", serde_json::to_string(&test_struct2)?); + println!(); + let test_struct3_1: TestStruct3 = serde_json::from_str("{\"sub\": \"name\"}")?; + println!("{}", serde_json::to_string_pretty(&test_struct3_1)?); + let test_struct3_2: TestStruct3 = serde_json::from_str("{\"sub\": [\"name1\", \"name2\"]}")?; + println!("{}", serde_json::to_string_pretty(&test_struct3_2)?); + Ok(()) }