Last active
August 23, 2020 10:41
-
-
Save yutakahashi114/deb3a0ced497fb783bbc6ca993326dcb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate rust_map_macro; | |
use serde::{Deserialize, Serialize}; | |
use serde_json::{Map, Value}; | |
use std::collections::HashMap; | |
fn main() { | |
let test = Test { | |
null: None, | |
boolean: true, | |
int: 1234, | |
float: 56.78, | |
string: "str".to_string(), | |
array: vec!["array1".to_string(), "array2".to_string()], | |
map: vec![("seconds".to_string(), 1234), ("nanos".to_string(), 5678)] | |
.into_iter() | |
.collect(), | |
time: Time { | |
seconds: 1234, | |
nanos: 5678, | |
}, | |
}; | |
let r = serde_json::to_string(&test).unwrap(); | |
let test_map: Map<String, Value> = serde_json::from_str(&r).unwrap(); | |
println!("map"); | |
println!("{:?}", test_map); | |
let r = serde_json::to_string(&test_map).unwrap(); | |
let test: Test = serde_json::from_str(&r).unwrap(); | |
println!("struct"); | |
println!("{:?}", test); | |
} | |
#[derive(Debug, Deserialize, Serialize)] | |
struct Test { | |
null: Option<String>, | |
boolean: bool, | |
int: i64, | |
float: f64, | |
string: String, | |
array: Vec<String>, | |
map: HashMap<String, i64>, | |
time: Time, | |
} | |
#[derive(Debug, Deserialize, Serialize)] | |
pub struct Time { | |
pub seconds: i64, | |
pub nanos: i32, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment