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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//! ```
//! use wjp::{Deserialize, map, ParseError, Serialize, SerializeHelper, Values};
//!
//! // Example Struct to show how this library works
//! #[derive(Debug)]
//! struct Example {
//! code: f32,
//! messages: Vec<String>,
//! opt: Option<bool>,
//! }
//!
//! // Implementing the Serialize Trait allows you to call the .json() method on your struct
//! impl Serialize for Example {
//! fn serialize(&self) -> Values {
//! // The map!() macro is a helper to create a hashmap from the given values
//! Values::Struct(map!(
//! // Many Data Structures and Types already have Serialize implemented
//! ("code", &self.code),
//! ("messages", &self.messages),
//! ("opt", &self.opt)
//! ))
//! }
//! }
//!
//! // Implementing the TryFrom<Values> Trait allows you to deserialize a JSON String into your struct
//! impl TryFrom<Values> for Example {
//! // We advise on using the ParseError because many helper methods build on this error
//! type Error = ParseError;
//! fn try_from(value: Values) -> Result<Self, Self::Error> {
//! // Now you just need to get your struct / array and get the keys with their appropriate values
//! let mut struc = value.get_struct().ok_or(ParseError::new())?;
//! let code = struc.map_val("code", f32::try_from)?;
//! // Many Data Structures and Types already have TryFrom<Values> implemented
//! let messages = struc.map_val("messages", Vec::try_from)?;
//! // This is sadly not the case for Option<T> where your need to find out what the type of T is and parse that
//! let opt = struc.map_opt_val("opt", |val| val.get_bool())?;
//! Ok(Self {
//! opt,
//! messages,
//! code,
//! })
//! }
//! }
//! let example = Example { ///
//! code: 123.0,
//! messages: vec!["Important".to_string(), "Message".to_string()],
//! opt: None,
//! };
//! // After implementing these two traits you can call the .json() method to serialize your struct
//! let json = example.json();
//! println!("{}", json);
//! // And the <Your Type>::deserialize(&str/String) to deserialize it
//! let back = Example::deserialize(json);
//! println!("{:?}", back);
//! ```
//! Output of the Example above:
//!
//! ```text
//! {"opt":null,"code":123,"messages":["Important","Message"]}
//!
//! Ok(Example { code: 123.0, messages: ["Important", "Message"], opt: None })
//! ```
//!
//!
pub use Deserialize;
pub use ParseError;
pub use SerializeHelper;
pub use Serialize;
pub use Values;
pub const NULL: Values = Null;
pub const TRUE: Values = Boolean;
pub const FALSE: Values = Boolean;