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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! Deserialize JSON documents.
//!
//! # Example
//!
//! ```rust
//! #[derive(serde::Serialize, eserde::Deserialize)]
//! struct Person {
//! name: String,
//! age: u8,
//! phones: Vec<String>,
//! }
//!
//! # fn main() {
//! // Some JSON input data as a &str. Maybe this comes from the user.
//! let data = r#"
//! {
//! "name": "John Doe",
//! "age": 43,
//! "phones": [
//! "+44 1234567",
//! "+44 2345678"
//! ]
//! }"#;
//!
//! // Try to parse the string of data into a `Person` object.
//! match eserde::json::from_str::<Person>(data) {
//! Ok(p) => {
//! println!("Please call {} at the number {}", p.name, p.phones[0]);
//! }
//! Err(errors) => {
//! println!("Something went wrong during deserialization");
//! for error in errors.iter() {
//! println!("{error}")
//! }
//! }
//! }
//! # }
//! ```
//!
//! # Implementation
//!
//! This module relies on [`serde_json`](https://crates.io/crates/serde_json) as
//! the underlying deserializer.
//!
//! All deserializers in this module follow the same two-pass approach.
//! Start by using `serde::Deserialize` to try to deserialize the target type.
//! If it succeeds, return `Ok(value)`.
//! If it fails, use `eserde::EDeserialize` to visit the input again and
//! accumulate as many deserialization errors as possible.
//! The errors are then returned as a vector in the `Err` variant.
//!
//! # Limitations
//!
//! ## Input must be buffered in memory
//!
//! We don't support deserializing from a reader, since it doesn't allow
//! us to perform two passes over the input.\
//! We are restricted to input types that are buffered in memory (byte slices,
//! string slices, etc.).
use crate::;
/// Deserialize an instance of type `T` from a string of JSON text.
///
/// # Example
///
/// ```rust
/// #[derive(eserde::Deserialize, Debug)]
/// struct User {
/// fingerprint: String,
/// location: String,
/// }
///
/// # fn main() {
/// // The type of `j` is `&str`
/// let j = "
/// {
/// \"fingerprint\": \"0xF9BA143B95FF6D82\",
/// \"location\": \"Menlo Park, CA\"
/// }";
///
/// let u: User = eserde::json::from_str(j).unwrap();
/// println!("{:#?}", u);
/// # }
/// ```
/// Deserialize an instance of type `T` from bytes of JSON text.
///
/// # Example
///
/// ```rust
/// #[derive(eserde::Deserialize, Debug)]
/// struct User {
/// fingerprint: String,
/// location: String,
/// }
///
/// # fn main() {
/// // The type of `j` is `&[u8]`
/// let j = b"
/// {
/// \"fingerprint\": \"0xF9BA143B95FF6D82\",
/// \"location\": \"Menlo Park, CA\"
/// }";
///
/// let u: User = eserde::json::from_slice(j).unwrap();
/// println!("{:#?}", u);
/// # }
/// ```
impl_edeserialize_compat!