-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathserde.rs
More file actions
30 lines (24 loc) · 738 Bytes
/
serde.rs
File metadata and controls
30 lines (24 loc) · 738 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
use serde_json::Value;
#[derive(Serialize, Deserialize, Debug)]
struct Contact {
name: String,
age: u32,
}
fn main() {
let contact = Contact {
name: "Brian".to_string(),
age: 21,
};
// Serialize data structures to strings in JSON format
let contact: String = serde_json::to_string(&contact).unwrap();
println!("{}", contact);
// Deserialize data structures from JSON strings
let contact: Contact = serde_json::from_str(&contact).unwrap();
println!("{:?}", contact);
// Convert to arbitrary JSON `Value` type
let contact: Value = serde_json::to_value(&contact).unwrap();
println!("{:?}", contact);
}