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
pub use *;
/// During deserialization process, you may want to complain on invalid fields combination in struct.
/// Every type that implements [TryFromJson](trait.TryFromJson.html) must also implement `ValidateJson`.
/// This trait introduces function `validate_json()`. `try_from_json()` calls this function right after
/// it converted JSON string to target type, and deserialization will stop if `validate_json()` returns `Err`.
///
/// `ValidateJson` can be implemented automatically through `#[derive(TryFromJson, ValidateJson)]`.
/// Default implementation always accepts the validation. To validate the type you need to implement this trait manually.
///
/// # Examples
///
/// ```
/// use nop_json::{Reader, TryFromJson};
/// use std::io;
///
/// #[derive(TryFromJson, Debug)]
/// struct FromTo {from: i32, to: i32}
///
/// impl nop_json::ValidateJson for FromTo
/// { fn validate_json(self) -> Result<Self, String>
/// { if self.from <= self.to
/// { Ok(self)
/// }
/// else
/// { Err("to must be after from".to_string())
/// }
/// }
/// }
///
/// let mut reader = Reader::new(r#" {"from": 0, "to": 10} {"from": 3, "to": -1} "#.bytes());
/// let obj_0: io::Result<FromTo> = reader.read();
/// let obj_1: io::Result<FromTo> = reader.read();
/// assert!(obj_0.is_ok());
/// assert!(obj_1.is_err());
/// ```