Allows recursive reflection based serialization and deserialization of json. Supports structs and no derives are needed. Nightly required.
Supported types
- Primitives
- &str
- [T; N]
- (1, 2.0)
- json serialization
- json deserialization
- structs
- &dyn Trait
- enums
- unions
Example:
use json::{from_json, to_json};
#[derive(PartialEq, Debug)]
pub struct Test {
pub a: u8,
pub b: TestB,
}
#[derive(PartialEq, Debug)]
pub struct TestB {
pub c: (f64, String),
}
#[test]
fn test() {
assert_eq!(
from_json::<Test>("{\"a\": 4, \"b\": {\"c\": [1.1, \"2\"]}}"),
Test {
a: 4,
b: TestB {
c: (1.1, 2.to_string())
}
}
);
assert_eq!(
to_json(&Test {
a: 2,
b: TestB {
c: (1.0, 2.to_string())
}
}),
"{\"a\": 2, \"b\": {\"c\": [1.0, \"2\"]}}"
);
assert!(!from_json::<bool>("false"));
assert_eq!(to_json(&String::from("hi")), "\"hi\"");
assert_eq!(to_json(&false), "false");
assert_eq!(to_json(&'d'), "\"d\"");
assert_eq!(to_json(&"lol"), "\"lol\"");
assert_eq!(to_json(&[1, 2]), "[1, 2]");
assert_eq!(from_json::<u8>("1"), 1);
assert_eq!(from_json::<[u8; 2]>("[1,2]"), [1, 2]);
assert_eq!(from_json::<String>("\"hi\""), "hi");
assert_eq!(
from_json::<TestB>("{\"c\": [1.1, \"2\"]}"),
TestB {
c: (1.1, 2.to_string())
}
);
}