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
use crateParseError;
use crateParser;
use crateValues;
/// Auto Trait for Deserializing JSON into predefined Structs.
///
/// The Methods in this trait will appear when implementing the [`TryFrom<Values>`] trait
/// and is used to deserialize JSON strings.
///
/// Usage:
/// ```
/// use wjp::{Deserialize, ParseError, SerializeHelper, Values};
/// // Example Struct to show how this library works
/// #[derive(Debug)]
/// struct Example {
/// code: f32,
/// messages: Vec<String>,
/// opt: Option<bool>,
/// }
/// // 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,
/// })
/// }
/// }
///
/// //And the <Your Type>::deserialize(&str/String) to deserialize it
/// let json = "{\"opt\":null,\"code\":123,\"messages\":[\"Important\",\"Message\"]}";
/// let back = Example::deserialize_str(json);
/// println!("{:?}", back);
/// ```
/// Info: The [`TryFrom<Values>`] impl needs to have Error = ParseError.
///
/// Implementing this Trait on any Struct won't bring any benefits
///
/// [`TryFrom<Values>`]: TryFrom