feat: update serde json test
This commit is contained in:
@@ -55,6 +55,18 @@ struct TestStruct2 {
|
|||||||
test_description: Option<String>,
|
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<()> {
|
fn main() -> Result<()> {
|
||||||
let serialized = serde_json::to_string(&TestStruct{
|
let serialized = serde_json::to_string(&TestStruct{
|
||||||
ty: TestEnum::TypeA,
|
ty: TestEnum::TypeA,
|
||||||
@@ -85,7 +97,7 @@ fn main() -> Result<()> {
|
|||||||
});
|
});
|
||||||
println!("{}", &john);
|
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("test".into(), Value::Bool(true));
|
||||||
m.insert("test2".into(), Value::String("hello world".into()));
|
m.insert("test2".into(), Value::String("hello world".into()));
|
||||||
}
|
}
|
||||||
@@ -98,5 +110,11 @@ fn main() -> Result<()> {
|
|||||||
};
|
};
|
||||||
println!("{}", serde_json::to_string(&test_struct2)?);
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user