feat: update serde json test

This commit is contained in:
2025-08-03 16:48:22 +08:00
parent 9eba818de9
commit a1024943ff

View File

@@ -55,6 +55,18 @@ struct TestStruct2 {
test_description: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum StringOrStringArray {
String(String),
Vec(Vec<String>),
}
#[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(())
}