rust处理json
main.js
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Point {
x: i32,
#[serde(default = "default_resource_num")]
y: i32,
}
fn default_resource_num() -> i32 {
404
}
fn main() {
let point = Point { x: 1, y: 2 };
let serialized = serde_json::to_string(&point).unwrap();
println!("serialized = {}", serialized);
let data = r#"
{
"x": 1
}"#;
let d:Point = serde_json::from_str(data).unwrap();
println!("deserialized = {:?}", d);
}
cargo
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.59"