add serde-json-test

This commit is contained in:
2020-01-19 00:36:25 +08:00
parent ff35eab3c4
commit dc65673cea
3 changed files with 65 additions and 1 deletions

View File

@@ -0,0 +1,53 @@
#[macro_use]
extern crate serde_json;
use serde::{Deserialize, Serialize};
use serde_json::{Result, Value};
// https://serde.rs/container-attrs.html
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
enum TestEnum {
#[serde(rename = "A")]
TypeA,
#[serde(rename = "B")]
TypeB,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
struct TestStruct {
ty: TestEnum,
}
fn main() -> Result<()> {
let serialized = serde_json::to_string(&TestStruct{
ty: TestEnum::TypeA,
}).unwrap();
println!("{}", serialized);
let data = r#"
{
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
}"#;
let v: Value = serde_json::from_str(data)?;
println!("{}", v);
let john = json!({
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
});
println!("{}", john);
Ok(())
}