1#[macro_use]
2extern crate serde;
3#[cfg(test)]
4#[macro_use]
5extern crate serde_derive;
6extern crate base64;
7extern crate failure;
8extern crate ordered_float;
9extern crate try_from;
10
11pub mod de;
12pub mod error;
13mod number;
14pub mod ser;
15mod string;
16pub mod value;
17
18pub use de::from_str;
19pub use error::Error;
20pub use ser::to_string;
21
22pub type Result<T> = std::result::Result<T, Error>;
23
24#[cfg(test)]
25mod tests {
26 use from_str;
27 use to_string;
28
29 #[test]
30 fn test_basic() {
31 #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
32 struct Person {
33 name: String,
34 age: u32,
35 }
36
37 let person = Person {
38 name: "dankogai".into(),
39 age: 48,
40 };
41 let expected = r#"["name":"dankogai","age":48]"#;
42 assert_eq!(to_string(&person).unwrap(), expected);
43
44 let deserialized = from_str::<Person>(expected).unwrap();
45 assert_eq!(deserialized, person);
46 }
47
48 #[test]
49 fn test_compound() {
50 use std::collections::HashMap;
51 #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
52 struct Language {
53 name: String,
54 tag: Vec<String>,
55 }
56
57 let mut compilers = HashMap::new();
58 compilers.insert(
59 "rustc".into(),
60 Language {
61 name: "Rust".into(),
62 tag: vec!["system programming".into(), "Mozilla".into()],
63 },
64 );
65 compilers.insert(
66 "clang".into(),
67 Language {
68 name: "C++".into(),
69 tag: vec![],
70 },
71 );
72
73 let serialized = to_string(&compilers).unwrap();
74 let deserialized: HashMap<String, Language> = from_str(&serialized).unwrap();
75 assert_eq!(deserialized, compilers);
76 }
77}