/rust/registry/src/index.crates.io-6f17d22bba15001f/serde_json-1.0.120/src/de.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! Deserialize JSON data to a Rust data structure. |
2 | | |
3 | | use crate::error::{Error, ErrorCode, Result}; |
4 | | #[cfg(feature = "float_roundtrip")] |
5 | | use crate::lexical; |
6 | | use crate::number::Number; |
7 | | use crate::read::{self, Fused, Reference}; |
8 | | use alloc::string::String; |
9 | | use alloc::vec::Vec; |
10 | | #[cfg(feature = "float_roundtrip")] |
11 | | use core::iter; |
12 | | use core::iter::FusedIterator; |
13 | | use core::marker::PhantomData; |
14 | | use core::result; |
15 | | use core::str::FromStr; |
16 | | use serde::de::{self, Expected, Unexpected}; |
17 | | use serde::forward_to_deserialize_any; |
18 | | |
19 | | #[cfg(feature = "arbitrary_precision")] |
20 | | use crate::number::NumberDeserializer; |
21 | | |
22 | | pub use crate::read::{Read, SliceRead, StrRead}; |
23 | | |
24 | | #[cfg(feature = "std")] |
25 | | #[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
26 | | pub use crate::read::IoRead; |
27 | | |
28 | | ////////////////////////////////////////////////////////////////////////////// |
29 | | |
30 | | /// A structure that deserializes JSON into Rust values. |
31 | | pub struct Deserializer<R> { |
32 | | read: R, |
33 | | scratch: Vec<u8>, |
34 | | remaining_depth: u8, |
35 | | #[cfg(feature = "float_roundtrip")] |
36 | | single_precision: bool, |
37 | | #[cfg(feature = "unbounded_depth")] |
38 | | disable_recursion_limit: bool, |
39 | | } |
40 | | |
41 | | impl<'de, R> Deserializer<R> |
42 | | where |
43 | | R: read::Read<'de>, |
44 | | { |
45 | | /// Create a JSON deserializer from one of the possible serde_json input |
46 | | /// sources. |
47 | | /// |
48 | | /// Typically it is more convenient to use one of these methods instead: |
49 | | /// |
50 | | /// - Deserializer::from_str |
51 | | /// - Deserializer::from_slice |
52 | | /// - Deserializer::from_reader |
53 | 16.0k | pub fn new(read: R) -> Self { |
54 | 16.0k | Deserializer { |
55 | 16.0k | read, |
56 | 16.0k | scratch: Vec::new(), |
57 | 16.0k | remaining_depth: 128, |
58 | 16.0k | #[cfg(feature = "float_roundtrip")] |
59 | 16.0k | single_precision: false, |
60 | 16.0k | #[cfg(feature = "unbounded_depth")] |
61 | 16.0k | disable_recursion_limit: false, |
62 | 16.0k | } |
63 | 16.0k | } <serde_json::de::Deserializer<serde_json::read::StrRead>>::new Line | Count | Source | 53 | 16.0k | pub fn new(read: R) -> Self { | 54 | 16.0k | Deserializer { | 55 | 16.0k | read, | 56 | 16.0k | scratch: Vec::new(), | 57 | 16.0k | remaining_depth: 128, | 58 | 16.0k | #[cfg(feature = "float_roundtrip")] | 59 | 16.0k | single_precision: false, | 60 | 16.0k | #[cfg(feature = "unbounded_depth")] | 61 | 16.0k | disable_recursion_limit: false, | 62 | 16.0k | } | 63 | 16.0k | } |
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::new |
64 | | } |
65 | | |
66 | | #[cfg(feature = "std")] |
67 | | impl<R> Deserializer<read::IoRead<R>> |
68 | | where |
69 | | R: crate::io::Read, |
70 | | { |
71 | | /// Creates a JSON deserializer from an `io::Read`. |
72 | | /// |
73 | | /// Reader-based deserializers do not support deserializing borrowed types |
74 | | /// like `&str`, since the `std::io::Read` trait has no non-copying methods |
75 | | /// -- everything it does involves copying bytes out of the data source. |
76 | | pub fn from_reader(reader: R) -> Self { |
77 | | Deserializer::new(read::IoRead::new(reader)) |
78 | | } |
79 | | } |
80 | | |
81 | | impl<'a> Deserializer<read::SliceRead<'a>> { |
82 | | /// Creates a JSON deserializer from a `&[u8]`. |
83 | 0 | pub fn from_slice(bytes: &'a [u8]) -> Self { |
84 | 0 | Deserializer::new(read::SliceRead::new(bytes)) |
85 | 0 | } |
86 | | } |
87 | | |
88 | | impl<'a> Deserializer<read::StrRead<'a>> { |
89 | | /// Creates a JSON deserializer from a `&str`. |
90 | 0 | pub fn from_str(s: &'a str) -> Self { |
91 | 0 | Deserializer::new(read::StrRead::new(s)) |
92 | 0 | } |
93 | | } |
94 | | |
95 | | macro_rules! overflow { |
96 | | ($a:ident * 10 + $b:ident, $c:expr) => { |
97 | | match $c { |
98 | | c => $a >= c / 10 && ($a > c / 10 || $b > c % 10), |
99 | | } |
100 | | }; |
101 | | } |
102 | | |
103 | | pub(crate) enum ParserNumber { |
104 | | F64(f64), |
105 | | U64(u64), |
106 | | I64(i64), |
107 | | #[cfg(feature = "arbitrary_precision")] |
108 | | String(String), |
109 | | } |
110 | | |
111 | | impl ParserNumber { |
112 | 9.48k | fn visit<'de, V>(self, visitor: V) -> Result<V::Value> |
113 | 9.48k | where |
114 | 9.48k | V: de::Visitor<'de>, |
115 | 9.48k | { |
116 | 9.48k | match self { |
117 | 3.73k | ParserNumber::F64(x) => visitor.visit_f64(x), |
118 | 5.58k | ParserNumber::U64(x) => visitor.visit_u64(x), |
119 | 166 | ParserNumber::I64(x) => visitor.visit_i64(x), |
120 | | #[cfg(feature = "arbitrary_precision")] |
121 | | ParserNumber::String(x) => visitor.visit_map(NumberDeserializer { number: x.into() }), |
122 | | } |
123 | 9.48k | } <serde_json::de::ParserNumber>::visit::<rhai::serde::deserialize::DynamicVisitor> Line | Count | Source | 112 | 9.48k | fn visit<'de, V>(self, visitor: V) -> Result<V::Value> | 113 | 9.48k | where | 114 | 9.48k | V: de::Visitor<'de>, | 115 | 9.48k | { | 116 | 9.48k | match self { | 117 | 3.73k | ParserNumber::F64(x) => visitor.visit_f64(x), | 118 | 5.58k | ParserNumber::U64(x) => visitor.visit_u64(x), | 119 | 166 | ParserNumber::I64(x) => visitor.visit_i64(x), | 120 | | #[cfg(feature = "arbitrary_precision")] | 121 | | ParserNumber::String(x) => visitor.visit_map(NumberDeserializer { number: x.into() }), | 122 | | } | 123 | 9.48k | } |
Unexecuted instantiation: <serde_json::de::ParserNumber>::visit::<<serde_json::value::Value as serde::de::Deserialize>::deserialize::ValueVisitor> |
124 | | |
125 | 0 | fn invalid_type(self, exp: &dyn Expected) -> Error { |
126 | 0 | match self { |
127 | 0 | ParserNumber::F64(x) => de::Error::invalid_type(Unexpected::Float(x), exp), |
128 | 0 | ParserNumber::U64(x) => de::Error::invalid_type(Unexpected::Unsigned(x), exp), |
129 | 0 | ParserNumber::I64(x) => de::Error::invalid_type(Unexpected::Signed(x), exp), |
130 | | #[cfg(feature = "arbitrary_precision")] |
131 | | ParserNumber::String(_) => de::Error::invalid_type(Unexpected::Other("number"), exp), |
132 | | } |
133 | 0 | } |
134 | | } |
135 | | |
136 | | impl<'de, R: Read<'de>> Deserializer<R> { |
137 | | /// The `Deserializer::end` method should be called after a value has been fully deserialized. |
138 | | /// This allows the `Deserializer` to validate that the input stream is at the end or that it |
139 | | /// only has trailing whitespace. |
140 | 5.37k | pub fn end(&mut self) -> Result<()> { |
141 | 5.37k | match tri!(self.parse_whitespace()) { |
142 | 5.24k | Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)), |
143 | 128 | None => Ok(()), |
144 | | } |
145 | 5.37k | } |
146 | | |
147 | | /// Turn a JSON deserializer into an iterator over values of type T. |
148 | 0 | pub fn into_iter<T>(self) -> StreamDeserializer<'de, R, T> |
149 | 0 | where |
150 | 0 | T: de::Deserialize<'de>, |
151 | 0 | { |
152 | 0 | // This cannot be an implementation of std::iter::IntoIterator because |
153 | 0 | // we need the caller to choose what T is. |
154 | 0 | let offset = self.read.byte_offset(); |
155 | 0 | StreamDeserializer { |
156 | 0 | de: self, |
157 | 0 | offset, |
158 | 0 | failed: false, |
159 | 0 | output: PhantomData, |
160 | 0 | lifetime: PhantomData, |
161 | 0 | } |
162 | 0 | } |
163 | | |
164 | | /// Parse arbitrarily deep JSON structures without any consideration for |
165 | | /// overflowing the stack. |
166 | | /// |
167 | | /// You will want to provide some other way to protect against stack |
168 | | /// overflows, such as by wrapping your Deserializer in the dynamically |
169 | | /// growing stack adapter provided by the serde_stacker crate. Additionally |
170 | | /// you will need to be careful around other recursive operations on the |
171 | | /// parsed result which may overflow the stack after deserialization has |
172 | | /// completed, including, but not limited to, Display and Debug and Drop |
173 | | /// impls. |
174 | | /// |
175 | | /// *This method is only available if serde_json is built with the |
176 | | /// `"unbounded_depth"` feature.* |
177 | | /// |
178 | | /// # Examples |
179 | | /// |
180 | | /// ``` |
181 | | /// use serde::Deserialize; |
182 | | /// use serde_json::Value; |
183 | | /// |
184 | | /// fn main() { |
185 | | /// let mut json = String::new(); |
186 | | /// for _ in 0..10000 { |
187 | | /// json = format!("[{}]", json); |
188 | | /// } |
189 | | /// |
190 | | /// let mut deserializer = serde_json::Deserializer::from_str(&json); |
191 | | /// deserializer.disable_recursion_limit(); |
192 | | /// let deserializer = serde_stacker::Deserializer::new(&mut deserializer); |
193 | | /// let value = Value::deserialize(deserializer).unwrap(); |
194 | | /// |
195 | | /// carefully_drop_nested_arrays(value); |
196 | | /// } |
197 | | /// |
198 | | /// fn carefully_drop_nested_arrays(value: Value) { |
199 | | /// let mut stack = vec![value]; |
200 | | /// while let Some(value) = stack.pop() { |
201 | | /// if let Value::Array(array) = value { |
202 | | /// stack.extend(array); |
203 | | /// } |
204 | | /// } |
205 | | /// } |
206 | | /// ``` |
207 | | #[cfg(feature = "unbounded_depth")] |
208 | | #[cfg_attr(docsrs, doc(cfg(feature = "unbounded_depth")))] |
209 | | pub fn disable_recursion_limit(&mut self) { |
210 | | self.disable_recursion_limit = true; |
211 | | } |
212 | | |
213 | 316k | pub(crate) fn peek(&mut self) -> Result<Option<u8>> { |
214 | 316k | self.read.peek() |
215 | 316k | } |
216 | | |
217 | 214k | fn peek_or_null(&mut self) -> Result<u8> { |
218 | 214k | Ok(tri!(self.peek()).unwrap_or(b'\x00')) |
219 | 214k | } |
220 | | |
221 | 245k | fn eat_char(&mut self) { |
222 | 245k | self.read.discard(); |
223 | 245k | } |
224 | | |
225 | 20.2k | fn next_char(&mut self) -> Result<Option<u8>> { |
226 | 20.2k | self.read.next() |
227 | 20.2k | } |
228 | | |
229 | 0 | fn next_char_or_null(&mut self) -> Result<u8> { |
230 | 0 | Ok(tri!(self.next_char()).unwrap_or(b'\x00')) |
231 | 0 | } |
232 | | |
233 | | /// Error caused by a byte from next_char(). |
234 | | #[cold] |
235 | 3.90k | fn error(&self, reason: ErrorCode) -> Error { |
236 | 3.90k | let position = self.read.position(); |
237 | 3.90k | Error::syntax(reason, position.line, position.column) |
238 | 3.90k | } |
239 | | |
240 | | /// Error caused by a byte from peek(). |
241 | | #[cold] |
242 | 17.3k | fn peek_error(&self, reason: ErrorCode) -> Error { |
243 | 17.3k | let position = self.read.peek_position(); |
244 | 17.3k | Error::syntax(reason, position.line, position.column) |
245 | 17.3k | } |
246 | | |
247 | | /// Returns the first non-whitespace byte without consuming it, or `None` if |
248 | | /// EOF is encountered. |
249 | 50.6k | fn parse_whitespace(&mut self) -> Result<Option<u8>> { |
250 | | loop { |
251 | 102k | match tri!(self.peek()) { |
252 | 51.3k | Some(b' ' | b'\n' | b'\t' | b'\r') => { |
253 | 51.3k | self.eat_char(); |
254 | 51.3k | } |
255 | 50.6k | other => { |
256 | 50.6k | return Ok(other); |
257 | | } |
258 | | } |
259 | | } |
260 | 50.6k | } |
261 | | |
262 | | #[cold] |
263 | 0 | fn peek_invalid_type(&mut self, exp: &dyn Expected) -> Error { |
264 | 0 | let err = match self.peek_or_null().unwrap_or(b'\x00') { |
265 | | b'n' => { |
266 | 0 | self.eat_char(); |
267 | 0 | if let Err(err) = self.parse_ident(b"ull") { |
268 | 0 | return err; |
269 | 0 | } |
270 | 0 | de::Error::invalid_type(Unexpected::Unit, exp) |
271 | | } |
272 | | b't' => { |
273 | 0 | self.eat_char(); |
274 | 0 | if let Err(err) = self.parse_ident(b"rue") { |
275 | 0 | return err; |
276 | 0 | } |
277 | 0 | de::Error::invalid_type(Unexpected::Bool(true), exp) |
278 | | } |
279 | | b'f' => { |
280 | 0 | self.eat_char(); |
281 | 0 | if let Err(err) = self.parse_ident(b"alse") { |
282 | 0 | return err; |
283 | 0 | } |
284 | 0 | de::Error::invalid_type(Unexpected::Bool(false), exp) |
285 | | } |
286 | | b'-' => { |
287 | 0 | self.eat_char(); |
288 | 0 | match self.parse_any_number(false) { |
289 | 0 | Ok(n) => n.invalid_type(exp), |
290 | 0 | Err(err) => return err, |
291 | | } |
292 | | } |
293 | 0 | b'0'..=b'9' => match self.parse_any_number(true) { |
294 | 0 | Ok(n) => n.invalid_type(exp), |
295 | 0 | Err(err) => return err, |
296 | | }, |
297 | | b'"' => { |
298 | 0 | self.eat_char(); |
299 | 0 | self.scratch.clear(); |
300 | 0 | match self.read.parse_str(&mut self.scratch) { |
301 | 0 | Ok(s) => de::Error::invalid_type(Unexpected::Str(&s), exp), |
302 | 0 | Err(err) => return err, |
303 | | } |
304 | | } |
305 | 0 | b'[' => de::Error::invalid_type(Unexpected::Seq, exp), |
306 | 0 | b'{' => de::Error::invalid_type(Unexpected::Map, exp), |
307 | 0 | _ => self.peek_error(ErrorCode::ExpectedSomeValue), |
308 | | }; |
309 | | |
310 | 0 | self.fix_position(err) |
311 | 0 | } |
312 | | |
313 | 0 | pub(crate) fn deserialize_number<'any, V>(&mut self, visitor: V) -> Result<V::Value> |
314 | 0 | where |
315 | 0 | V: de::Visitor<'any>, |
316 | 0 | { |
317 | 0 | let peek = match tri!(self.parse_whitespace()) { |
318 | 0 | Some(b) => b, |
319 | | None => { |
320 | 0 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
321 | | } |
322 | | }; |
323 | | |
324 | 0 | let value = match peek { |
325 | | b'-' => { |
326 | 0 | self.eat_char(); |
327 | 0 | tri!(self.parse_integer(false)).visit(visitor) |
328 | | } |
329 | 0 | b'0'..=b'9' => tri!(self.parse_integer(true)).visit(visitor), |
330 | 0 | _ => Err(self.peek_invalid_type(&visitor)), |
331 | | }; |
332 | | |
333 | 0 | match value { |
334 | 0 | Ok(value) => Ok(value), |
335 | 0 | Err(err) => Err(self.fix_position(err)), |
336 | | } |
337 | 0 | } |
338 | | |
339 | | #[cfg(feature = "float_roundtrip")] |
340 | | pub(crate) fn do_deserialize_f32<'any, V>(&mut self, visitor: V) -> Result<V::Value> |
341 | | where |
342 | | V: de::Visitor<'any>, |
343 | | { |
344 | | self.single_precision = true; |
345 | | let val = self.deserialize_number(visitor); |
346 | | self.single_precision = false; |
347 | | val |
348 | | } |
349 | | |
350 | 0 | pub(crate) fn do_deserialize_i128<'any, V>(&mut self, visitor: V) -> Result<V::Value> |
351 | 0 | where |
352 | 0 | V: de::Visitor<'any>, |
353 | 0 | { |
354 | 0 | let mut buf = String::new(); |
355 | | |
356 | 0 | match tri!(self.parse_whitespace()) { |
357 | 0 | Some(b'-') => { |
358 | 0 | self.eat_char(); |
359 | 0 | buf.push('-'); |
360 | 0 | } |
361 | 0 | Some(_) => {} |
362 | | None => { |
363 | 0 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
364 | | } |
365 | | }; |
366 | | |
367 | 0 | tri!(self.scan_integer128(&mut buf)); |
368 | | |
369 | 0 | let value = match buf.parse() { |
370 | 0 | Ok(int) => visitor.visit_i128(int), |
371 | | Err(_) => { |
372 | 0 | return Err(self.error(ErrorCode::NumberOutOfRange)); |
373 | | } |
374 | | }; |
375 | | |
376 | 0 | match value { |
377 | 0 | Ok(value) => Ok(value), |
378 | 0 | Err(err) => Err(self.fix_position(err)), |
379 | | } |
380 | 0 | } |
381 | | |
382 | 0 | pub(crate) fn do_deserialize_u128<'any, V>(&mut self, visitor: V) -> Result<V::Value> |
383 | 0 | where |
384 | 0 | V: de::Visitor<'any>, |
385 | 0 | { |
386 | 0 | match tri!(self.parse_whitespace()) { |
387 | | Some(b'-') => { |
388 | 0 | return Err(self.peek_error(ErrorCode::NumberOutOfRange)); |
389 | | } |
390 | 0 | Some(_) => {} |
391 | | None => { |
392 | 0 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
393 | | } |
394 | | } |
395 | | |
396 | 0 | let mut buf = String::new(); |
397 | 0 | tri!(self.scan_integer128(&mut buf)); |
398 | | |
399 | 0 | let value = match buf.parse() { |
400 | 0 | Ok(int) => visitor.visit_u128(int), |
401 | | Err(_) => { |
402 | 0 | return Err(self.error(ErrorCode::NumberOutOfRange)); |
403 | | } |
404 | | }; |
405 | | |
406 | 0 | match value { |
407 | 0 | Ok(value) => Ok(value), |
408 | 0 | Err(err) => Err(self.fix_position(err)), |
409 | | } |
410 | 0 | } |
411 | | |
412 | 0 | fn scan_integer128(&mut self, buf: &mut String) -> Result<()> { |
413 | 0 | match tri!(self.next_char_or_null()) { |
414 | | b'0' => { |
415 | 0 | buf.push('0'); |
416 | | // There can be only one leading '0'. |
417 | 0 | match tri!(self.peek_or_null()) { |
418 | 0 | b'0'..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)), |
419 | 0 | _ => Ok(()), |
420 | | } |
421 | | } |
422 | 0 | c @ b'1'..=b'9' => { |
423 | 0 | buf.push(c as char); |
424 | 0 | while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) { |
425 | 0 | self.eat_char(); |
426 | 0 | buf.push(c as char); |
427 | 0 | } |
428 | 0 | Ok(()) |
429 | | } |
430 | 0 | _ => Err(self.error(ErrorCode::InvalidNumber)), |
431 | | } |
432 | 0 | } |
433 | | |
434 | | #[cold] |
435 | 7.71k | fn fix_position(&self, err: Error) -> Error { |
436 | 7.71k | err.fix_position(move |code| self.error(code)) |
437 | 7.71k | } |
438 | | |
439 | 1.13k | fn parse_ident(&mut self, ident: &[u8]) -> Result<()> { |
440 | 2.01k | for expected in ident { |
441 | 1.87k | match tri!(self.next_char()) { |
442 | | None => { |
443 | 170 | return Err(self.error(ErrorCode::EofWhileParsingValue)); |
444 | | } |
445 | 1.70k | Some(next) => { |
446 | 1.70k | if next != *expected { |
447 | 828 | return Err(self.error(ErrorCode::ExpectedSomeIdent)); |
448 | 879 | } |
449 | | } |
450 | | } |
451 | | } |
452 | | |
453 | 133 | Ok(()) |
454 | 1.13k | } |
455 | | |
456 | 14.1k | fn parse_integer(&mut self, positive: bool) -> Result<ParserNumber> { |
457 | 14.1k | let next = match tri!(self.next_char()) { |
458 | 14.0k | Some(b) => b, |
459 | | None => { |
460 | 102 | return Err(self.error(ErrorCode::EofWhileParsingValue)); |
461 | | } |
462 | | }; |
463 | | |
464 | 14.0k | match next { |
465 | | b'0' => { |
466 | | // There can be only one leading '0'. |
467 | 3.13k | match tri!(self.peek_or_null()) { |
468 | 2.37k | b'0'..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)), |
469 | 1.52k | _ => self.parse_number(positive, 0), |
470 | | } |
471 | | } |
472 | 10.9k | c @ b'1'..=b'9' => { |
473 | 10.9k | let mut significand = (c - b'0') as u64; |
474 | | |
475 | | loop { |
476 | 101k | match tri!(self.peek_or_null()) { |
477 | 94.2k | c @ b'0'..=b'9' => { |
478 | 91.3k | let digit = (c - b'0') as u64; |
479 | 91.3k | |
480 | 91.3k | // We need to be careful with overflow. If we can, |
481 | 91.3k | // try to keep the number as a `u64` until we grow |
482 | 91.3k | // too large. At that point, switch to parsing the |
483 | 91.3k | // value as a `f64`. |
484 | 91.3k | if overflow!(significand * 10 + digit, u64::MAX) { |
485 | 314 | return Ok(ParserNumber::F64(tri!( |
486 | 976 | self.parse_long_integer(positive, significand), |
487 | | ))); |
488 | 90.3k | } |
489 | 90.3k | |
490 | 90.3k | self.eat_char(); |
491 | 90.3k | significand = significand * 10 + digit; |
492 | | } |
493 | | _ => { |
494 | 9.93k | return self.parse_number(positive, significand); |
495 | | } |
496 | | } |
497 | | } |
498 | | } |
499 | 50 | _ => Err(self.error(ErrorCode::InvalidNumber)), |
500 | | } |
501 | 14.1k | } |
502 | | |
503 | 11.4k | fn parse_number(&mut self, positive: bool, significand: u64) -> Result<ParserNumber> { |
504 | 11.4k | Ok(match tri!(self.peek_or_null()) { |
505 | 3.50k | b'.' => ParserNumber::F64(tri!(self.parse_decimal(positive, significand, 0))), |
506 | 2.14k | b'e' | b'E' => ParserNumber::F64(tri!(self.parse_exponent(positive, significand, 0))), |
507 | | _ => { |
508 | 5.79k | if positive { |
509 | 5.58k | ParserNumber::U64(significand) |
510 | | } else { |
511 | 216 | let neg = (significand as i64).wrapping_neg(); |
512 | 216 | |
513 | 216 | // Convert into a float if we underflow, or on `-0`. |
514 | 216 | if neg >= 0 { |
515 | 50 | ParserNumber::F64(-(significand as f64)) |
516 | | } else { |
517 | 166 | ParserNumber::I64(neg) |
518 | | } |
519 | | } |
520 | | } |
521 | | }) |
522 | 11.4k | } |
523 | | |
524 | 4.01k | fn parse_decimal( |
525 | 4.01k | &mut self, |
526 | 4.01k | positive: bool, |
527 | 4.01k | mut significand: u64, |
528 | 4.01k | exponent_before_decimal_point: i32, |
529 | 4.01k | ) -> Result<f64> { |
530 | 4.01k | self.eat_char(); |
531 | 4.01k | |
532 | 4.01k | let mut exponent_after_decimal_point = 0; |
533 | 20.4k | while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) { |
534 | 19.2k | let digit = (c - b'0') as u64; |
535 | 19.2k | |
536 | 19.2k | if overflow!(significand * 10 + digit, u64::MAX) { |
537 | 2.78k | let exponent = exponent_before_decimal_point + exponent_after_decimal_point; |
538 | 2.78k | return self.parse_decimal_overflow(positive, significand, exponent); |
539 | 16.4k | } |
540 | 16.4k | |
541 | 16.4k | self.eat_char(); |
542 | 16.4k | significand = significand * 10 + digit; |
543 | 16.4k | exponent_after_decimal_point -= 1; |
544 | | } |
545 | | |
546 | | // Error if there is not at least one digit after the decimal point. |
547 | 1.23k | if exponent_after_decimal_point == 0 { |
548 | 198 | match tri!(self.peek()) { |
549 | 149 | Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)), |
550 | 49 | None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)), |
551 | | } |
552 | 1.03k | } |
553 | 1.03k | |
554 | 1.03k | let exponent = exponent_before_decimal_point + exponent_after_decimal_point; |
555 | 1.03k | match tri!(self.peek_or_null()) { |
556 | 455 | b'e' | b'E' => self.parse_exponent(positive, significand, exponent), |
557 | 581 | _ => self.f64_from_parts(positive, significand, exponent), |
558 | | } |
559 | 4.01k | } |
560 | | |
561 | 4.21k | fn parse_exponent( |
562 | 4.21k | &mut self, |
563 | 4.21k | positive: bool, |
564 | 4.21k | significand: u64, |
565 | 4.21k | starting_exp: i32, |
566 | 4.21k | ) -> Result<f64> { |
567 | 4.21k | self.eat_char(); |
568 | | |
569 | 4.21k | let positive_exp = match tri!(self.peek_or_null()) { |
570 | | b'+' => { |
571 | 964 | self.eat_char(); |
572 | 964 | true |
573 | | } |
574 | | b'-' => { |
575 | 539 | self.eat_char(); |
576 | 539 | false |
577 | | } |
578 | 2.71k | _ => true, |
579 | | }; |
580 | | |
581 | 4.21k | let next = match tri!(self.next_char()) { |
582 | 2.12k | Some(b) => b, |
583 | | None => { |
584 | 2.08k | return Err(self.error(ErrorCode::EofWhileParsingValue)); |
585 | | } |
586 | | }; |
587 | | |
588 | | // Make sure a digit follows the exponent place. |
589 | 2.12k | let mut exp = match next { |
590 | 1.88k | c @ b'0'..=b'9' => (c - b'0') as i32, |
591 | | _ => { |
592 | 327 | return Err(self.error(ErrorCode::InvalidNumber)); |
593 | | } |
594 | | }; |
595 | | |
596 | 12.5k | while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) { |
597 | 11.6k | self.eat_char(); |
598 | 11.6k | let digit = (c - b'0') as i32; |
599 | 11.6k | |
600 | 11.6k | if overflow!(exp * 10 + digit, i32::MAX) { |
601 | 923 | let zero_significand = significand == 0; |
602 | 923 | return self.parse_exponent_overflow(positive, zero_significand, positive_exp); |
603 | 10.7k | } |
604 | 10.7k | |
605 | 10.7k | exp = exp * 10 + digit; |
606 | | } |
607 | | |
608 | 879 | let final_exp = if positive_exp { |
609 | 483 | starting_exp.saturating_add(exp) |
610 | | } else { |
611 | 396 | starting_exp.saturating_sub(exp) |
612 | | }; |
613 | | |
614 | 879 | self.f64_from_parts(positive, significand, final_exp) |
615 | 4.21k | } |
616 | | |
617 | | #[cfg(feature = "float_roundtrip")] |
618 | | fn f64_from_parts(&mut self, positive: bool, significand: u64, exponent: i32) -> Result<f64> { |
619 | | let f = if self.single_precision { |
620 | | lexical::parse_concise_float::<f32>(significand, exponent) as f64 |
621 | | } else { |
622 | | lexical::parse_concise_float::<f64>(significand, exponent) |
623 | | }; |
624 | | |
625 | | if f.is_infinite() { |
626 | | Err(self.error(ErrorCode::NumberOutOfRange)) |
627 | | } else { |
628 | | Ok(if positive { f } else { -f }) |
629 | | } |
630 | | } |
631 | | |
632 | | #[cfg(not(feature = "float_roundtrip"))] |
633 | 3.09k | fn f64_from_parts( |
634 | 3.09k | &mut self, |
635 | 3.09k | positive: bool, |
636 | 3.09k | significand: u64, |
637 | 3.09k | mut exponent: i32, |
638 | 3.09k | ) -> Result<f64> { |
639 | 3.09k | let mut f = significand as f64; |
640 | 3.40k | loop { |
641 | 3.40k | match POW10.get(exponent.wrapping_abs() as usize) { |
642 | 2.69k | Some(&pow) => { |
643 | 2.69k | if exponent >= 0 { |
644 | 873 | f *= pow; |
645 | 873 | if f.is_infinite() { |
646 | 1 | return Err(self.error(ErrorCode::NumberOutOfRange)); |
647 | 872 | } |
648 | 1.82k | } else { |
649 | 1.82k | f /= pow; |
650 | 1.82k | } |
651 | 2.69k | break; |
652 | | } |
653 | | None => { |
654 | 710 | if f == 0.0 { |
655 | 168 | break; |
656 | 542 | } |
657 | 542 | if exponent >= 0 { |
658 | 230 | return Err(self.error(ErrorCode::NumberOutOfRange)); |
659 | 312 | } |
660 | 312 | f /= 1e308; |
661 | 312 | exponent += 308; |
662 | | } |
663 | | } |
664 | | } |
665 | 2.86k | Ok(if positive { f } else { -f }) |
666 | 3.09k | } |
667 | | |
668 | | #[cfg(feature = "float_roundtrip")] |
669 | | #[cold] |
670 | | #[inline(never)] |
671 | | fn parse_long_integer(&mut self, positive: bool, partial_significand: u64) -> Result<f64> { |
672 | | // To deserialize floats we'll first push the integer and fraction |
673 | | // parts, both as byte strings, into the scratch buffer and then feed |
674 | | // both slices to lexical's parser. For example if the input is |
675 | | // `12.34e5` we'll push b"1234" into scratch and then pass b"12" and |
676 | | // b"34" to lexical. `integer_end` will be used to track where to split |
677 | | // the scratch buffer. |
678 | | // |
679 | | // Note that lexical expects the integer part to contain *no* leading |
680 | | // zeroes and the fraction part to contain *no* trailing zeroes. The |
681 | | // first requirement is already handled by the integer parsing logic. |
682 | | // The second requirement will be enforced just before passing the |
683 | | // slices to lexical in f64_long_from_parts. |
684 | | self.scratch.clear(); |
685 | | self.scratch |
686 | | .extend_from_slice(itoa::Buffer::new().format(partial_significand).as_bytes()); |
687 | | |
688 | | loop { |
689 | | match tri!(self.peek_or_null()) { |
690 | | c @ b'0'..=b'9' => { |
691 | | self.scratch.push(c); |
692 | | self.eat_char(); |
693 | | } |
694 | | b'.' => { |
695 | | self.eat_char(); |
696 | | return self.parse_long_decimal(positive, self.scratch.len()); |
697 | | } |
698 | | b'e' | b'E' => { |
699 | | return self.parse_long_exponent(positive, self.scratch.len()); |
700 | | } |
701 | | _ => { |
702 | | return self.f64_long_from_parts(positive, self.scratch.len(), 0); |
703 | | } |
704 | | } |
705 | | } |
706 | | } |
707 | | |
708 | | #[cfg(not(feature = "float_roundtrip"))] |
709 | | #[cold] |
710 | | #[inline(never)] |
711 | 976 | fn parse_long_integer(&mut self, positive: bool, significand: u64) -> Result<f64> { |
712 | 976 | let mut exponent = 0; |
713 | | loop { |
714 | 14.5k | match tri!(self.peek_or_null()) { |
715 | 13.7k | b'0'..=b'9' => { |
716 | 13.5k | self.eat_char(); |
717 | 13.5k | // This could overflow... if your integer is gigabytes long. |
718 | 13.5k | // Ignore that possibility. |
719 | 13.5k | exponent += 1; |
720 | 13.5k | } |
721 | | b'.' => { |
722 | 508 | return self.parse_decimal(positive, significand, exponent); |
723 | | } |
724 | | b'e' | b'E' => { |
725 | 160 | return self.parse_exponent(positive, significand, exponent); |
726 | | } |
727 | | _ => { |
728 | 308 | return self.f64_from_parts(positive, significand, exponent); |
729 | | } |
730 | | } |
731 | | } |
732 | 976 | } |
733 | | |
734 | | #[cfg(feature = "float_roundtrip")] |
735 | | #[cold] |
736 | | fn parse_long_decimal(&mut self, positive: bool, integer_end: usize) -> Result<f64> { |
737 | | let mut at_least_one_digit = integer_end < self.scratch.len(); |
738 | | while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) { |
739 | | self.scratch.push(c); |
740 | | self.eat_char(); |
741 | | at_least_one_digit = true; |
742 | | } |
743 | | |
744 | | if !at_least_one_digit { |
745 | | match tri!(self.peek()) { |
746 | | Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)), |
747 | | None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)), |
748 | | } |
749 | | } |
750 | | |
751 | | match tri!(self.peek_or_null()) { |
752 | | b'e' | b'E' => self.parse_long_exponent(positive, integer_end), |
753 | | _ => self.f64_long_from_parts(positive, integer_end, 0), |
754 | | } |
755 | | } |
756 | | |
757 | | #[cfg(feature = "float_roundtrip")] |
758 | | fn parse_long_exponent(&mut self, positive: bool, integer_end: usize) -> Result<f64> { |
759 | | self.eat_char(); |
760 | | |
761 | | let positive_exp = match tri!(self.peek_or_null()) { |
762 | | b'+' => { |
763 | | self.eat_char(); |
764 | | true |
765 | | } |
766 | | b'-' => { |
767 | | self.eat_char(); |
768 | | false |
769 | | } |
770 | | _ => true, |
771 | | }; |
772 | | |
773 | | let next = match tri!(self.next_char()) { |
774 | | Some(b) => b, |
775 | | None => { |
776 | | return Err(self.error(ErrorCode::EofWhileParsingValue)); |
777 | | } |
778 | | }; |
779 | | |
780 | | // Make sure a digit follows the exponent place. |
781 | | let mut exp = match next { |
782 | | c @ b'0'..=b'9' => (c - b'0') as i32, |
783 | | _ => { |
784 | | return Err(self.error(ErrorCode::InvalidNumber)); |
785 | | } |
786 | | }; |
787 | | |
788 | | while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) { |
789 | | self.eat_char(); |
790 | | let digit = (c - b'0') as i32; |
791 | | |
792 | | if overflow!(exp * 10 + digit, i32::MAX) { |
793 | | let zero_significand = self.scratch.iter().all(|&digit| digit == b'0'); |
794 | | return self.parse_exponent_overflow(positive, zero_significand, positive_exp); |
795 | | } |
796 | | |
797 | | exp = exp * 10 + digit; |
798 | | } |
799 | | |
800 | | let final_exp = if positive_exp { exp } else { -exp }; |
801 | | |
802 | | self.f64_long_from_parts(positive, integer_end, final_exp) |
803 | | } |
804 | | |
805 | | // This cold code should not be inlined into the middle of the hot |
806 | | // decimal-parsing loop above. |
807 | | #[cfg(feature = "float_roundtrip")] |
808 | | #[cold] |
809 | | #[inline(never)] |
810 | | fn parse_decimal_overflow( |
811 | | &mut self, |
812 | | positive: bool, |
813 | | significand: u64, |
814 | | exponent: i32, |
815 | | ) -> Result<f64> { |
816 | | let mut buffer = itoa::Buffer::new(); |
817 | | let significand = buffer.format(significand); |
818 | | let fraction_digits = -exponent as usize; |
819 | | self.scratch.clear(); |
820 | | if let Some(zeros) = fraction_digits.checked_sub(significand.len() + 1) { |
821 | | self.scratch.extend(iter::repeat(b'0').take(zeros + 1)); |
822 | | } |
823 | | self.scratch.extend_from_slice(significand.as_bytes()); |
824 | | let integer_end = self.scratch.len() - fraction_digits; |
825 | | self.parse_long_decimal(positive, integer_end) |
826 | | } |
827 | | |
828 | | #[cfg(not(feature = "float_roundtrip"))] |
829 | | #[cold] |
830 | | #[inline(never)] |
831 | 2.78k | fn parse_decimal_overflow( |
832 | 2.78k | &mut self, |
833 | 2.78k | positive: bool, |
834 | 2.78k | significand: u64, |
835 | 2.78k | exponent: i32, |
836 | 2.78k | ) -> Result<f64> { |
837 | | // The next multiply/add would overflow, so just ignore all further |
838 | | // digits. |
839 | 37.2k | while let b'0'..=b'9' = tri!(self.peek_or_null()) { |
840 | 34.4k | self.eat_char(); |
841 | 34.4k | } |
842 | | |
843 | 2.78k | match tri!(self.peek_or_null()) { |
844 | 1.45k | b'e' | b'E' => self.parse_exponent(positive, significand, exponent), |
845 | 1.32k | _ => self.f64_from_parts(positive, significand, exponent), |
846 | | } |
847 | 2.78k | } |
848 | | |
849 | | // This cold code should not be inlined into the middle of the hot |
850 | | // exponent-parsing loop above. |
851 | | #[cold] |
852 | | #[inline(never)] |
853 | 923 | fn parse_exponent_overflow( |
854 | 923 | &mut self, |
855 | 923 | positive: bool, |
856 | 923 | zero_significand: bool, |
857 | 923 | positive_exp: bool, |
858 | 923 | ) -> Result<f64> { |
859 | 923 | // Error instead of +/- infinity. |
860 | 923 | if !zero_significand && positive_exp { |
861 | 103 | return Err(self.error(ErrorCode::NumberOutOfRange)); |
862 | 820 | } |
863 | | |
864 | 5.92k | while let b'0'..=b'9' = tri!(self.peek_or_null()) { |
865 | 5.10k | self.eat_char(); |
866 | 5.10k | } |
867 | 820 | Ok(if positive { 0.0 } else { -0.0 }) |
868 | 923 | } |
869 | | |
870 | | #[cfg(feature = "float_roundtrip")] |
871 | | fn f64_long_from_parts( |
872 | | &mut self, |
873 | | positive: bool, |
874 | | integer_end: usize, |
875 | | exponent: i32, |
876 | | ) -> Result<f64> { |
877 | | let integer = &self.scratch[..integer_end]; |
878 | | let fraction = &self.scratch[integer_end..]; |
879 | | |
880 | | let f = if self.single_precision { |
881 | | lexical::parse_truncated_float::<f32>(integer, fraction, exponent) as f64 |
882 | | } else { |
883 | | lexical::parse_truncated_float::<f64>(integer, fraction, exponent) |
884 | | }; |
885 | | |
886 | | if f.is_infinite() { |
887 | | Err(self.error(ErrorCode::NumberOutOfRange)) |
888 | | } else { |
889 | | Ok(if positive { f } else { -f }) |
890 | | } |
891 | | } |
892 | | |
893 | 0 | fn parse_any_signed_number(&mut self) -> Result<ParserNumber> { |
894 | 0 | let peek = match tri!(self.peek()) { |
895 | 0 | Some(b) => b, |
896 | | None => { |
897 | 0 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
898 | | } |
899 | | }; |
900 | | |
901 | 0 | let value = match peek { |
902 | | b'-' => { |
903 | 0 | self.eat_char(); |
904 | 0 | self.parse_any_number(false) |
905 | | } |
906 | 0 | b'0'..=b'9' => self.parse_any_number(true), |
907 | 0 | _ => Err(self.peek_error(ErrorCode::InvalidNumber)), |
908 | | }; |
909 | | |
910 | 0 | let value = match tri!(self.peek()) { |
911 | 0 | Some(_) => Err(self.peek_error(ErrorCode::InvalidNumber)), |
912 | 0 | None => value, |
913 | | }; |
914 | | |
915 | 0 | match value { |
916 | 0 | Ok(value) => Ok(value), |
917 | | // The de::Error impl creates errors with unknown line and column. |
918 | | // Fill in the position here by looking at the current index in the |
919 | | // input. There is no way to tell whether this should call `error` |
920 | | // or `peek_error` so pick the one that seems correct more often. |
921 | | // Worst case, the position is off by one character. |
922 | 0 | Err(err) => Err(self.fix_position(err)), |
923 | | } |
924 | 0 | } |
925 | | |
926 | | #[cfg(not(feature = "arbitrary_precision"))] |
927 | 14.1k | fn parse_any_number(&mut self, positive: bool) -> Result<ParserNumber> { |
928 | 14.1k | self.parse_integer(positive) |
929 | 14.1k | } |
930 | | |
931 | | #[cfg(feature = "arbitrary_precision")] |
932 | | fn parse_any_number(&mut self, positive: bool) -> Result<ParserNumber> { |
933 | | let mut buf = String::with_capacity(16); |
934 | | if !positive { |
935 | | buf.push('-'); |
936 | | } |
937 | | tri!(self.scan_integer(&mut buf)); |
938 | | if positive { |
939 | | if let Ok(unsigned) = buf.parse() { |
940 | | return Ok(ParserNumber::U64(unsigned)); |
941 | | } |
942 | | } else { |
943 | | if let Ok(signed) = buf.parse() { |
944 | | return Ok(ParserNumber::I64(signed)); |
945 | | } |
946 | | } |
947 | | Ok(ParserNumber::String(buf)) |
948 | | } |
949 | | |
950 | | #[cfg(feature = "arbitrary_precision")] |
951 | | fn scan_or_eof(&mut self, buf: &mut String) -> Result<u8> { |
952 | | match tri!(self.next_char()) { |
953 | | Some(b) => { |
954 | | buf.push(b as char); |
955 | | Ok(b) |
956 | | } |
957 | | None => Err(self.error(ErrorCode::EofWhileParsingValue)), |
958 | | } |
959 | | } |
960 | | |
961 | | #[cfg(feature = "arbitrary_precision")] |
962 | | fn scan_integer(&mut self, buf: &mut String) -> Result<()> { |
963 | | match tri!(self.scan_or_eof(buf)) { |
964 | | b'0' => { |
965 | | // There can be only one leading '0'. |
966 | | match tri!(self.peek_or_null()) { |
967 | | b'0'..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)), |
968 | | _ => self.scan_number(buf), |
969 | | } |
970 | | } |
971 | | b'1'..=b'9' => loop { |
972 | | match tri!(self.peek_or_null()) { |
973 | | c @ b'0'..=b'9' => { |
974 | | self.eat_char(); |
975 | | buf.push(c as char); |
976 | | } |
977 | | _ => { |
978 | | return self.scan_number(buf); |
979 | | } |
980 | | } |
981 | | }, |
982 | | _ => Err(self.error(ErrorCode::InvalidNumber)), |
983 | | } |
984 | | } |
985 | | |
986 | | #[cfg(feature = "arbitrary_precision")] |
987 | | fn scan_number(&mut self, buf: &mut String) -> Result<()> { |
988 | | match tri!(self.peek_or_null()) { |
989 | | b'.' => self.scan_decimal(buf), |
990 | | e @ (b'e' | b'E') => self.scan_exponent(e as char, buf), |
991 | | _ => Ok(()), |
992 | | } |
993 | | } |
994 | | |
995 | | #[cfg(feature = "arbitrary_precision")] |
996 | | fn scan_decimal(&mut self, buf: &mut String) -> Result<()> { |
997 | | self.eat_char(); |
998 | | buf.push('.'); |
999 | | |
1000 | | let mut at_least_one_digit = false; |
1001 | | while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) { |
1002 | | self.eat_char(); |
1003 | | buf.push(c as char); |
1004 | | at_least_one_digit = true; |
1005 | | } |
1006 | | |
1007 | | if !at_least_one_digit { |
1008 | | match tri!(self.peek()) { |
1009 | | Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)), |
1010 | | None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)), |
1011 | | } |
1012 | | } |
1013 | | |
1014 | | match tri!(self.peek_or_null()) { |
1015 | | e @ (b'e' | b'E') => self.scan_exponent(e as char, buf), |
1016 | | _ => Ok(()), |
1017 | | } |
1018 | | } |
1019 | | |
1020 | | #[cfg(feature = "arbitrary_precision")] |
1021 | | fn scan_exponent(&mut self, e: char, buf: &mut String) -> Result<()> { |
1022 | | self.eat_char(); |
1023 | | buf.push(e); |
1024 | | |
1025 | | match tri!(self.peek_or_null()) { |
1026 | | b'+' => { |
1027 | | self.eat_char(); |
1028 | | buf.push('+'); |
1029 | | } |
1030 | | b'-' => { |
1031 | | self.eat_char(); |
1032 | | buf.push('-'); |
1033 | | } |
1034 | | _ => {} |
1035 | | } |
1036 | | |
1037 | | // Make sure a digit follows the exponent place. |
1038 | | match tri!(self.scan_or_eof(buf)) { |
1039 | | b'0'..=b'9' => {} |
1040 | | _ => { |
1041 | | return Err(self.error(ErrorCode::InvalidNumber)); |
1042 | | } |
1043 | | } |
1044 | | |
1045 | | while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) { |
1046 | | self.eat_char(); |
1047 | | buf.push(c as char); |
1048 | | } |
1049 | | |
1050 | | Ok(()) |
1051 | | } |
1052 | | |
1053 | 40 | fn parse_object_colon(&mut self) -> Result<()> { |
1054 | 40 | match tri!(self.parse_whitespace()) { |
1055 | | Some(b':') => { |
1056 | 0 | self.eat_char(); |
1057 | 0 | Ok(()) |
1058 | | } |
1059 | 40 | Some(_) => Err(self.peek_error(ErrorCode::ExpectedColon)), |
1060 | 0 | None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)), |
1061 | | } |
1062 | 40 | } |
1063 | | |
1064 | 5.99k | fn end_seq(&mut self) -> Result<()> { |
1065 | 5.99k | match tri!(self.parse_whitespace()) { |
1066 | | Some(b']') => { |
1067 | 92 | self.eat_char(); |
1068 | 92 | Ok(()) |
1069 | | } |
1070 | | Some(b',') => { |
1071 | 579 | self.eat_char(); |
1072 | 579 | match self.parse_whitespace() { |
1073 | 100 | Ok(Some(b']')) => Err(self.peek_error(ErrorCode::TrailingComma)), |
1074 | 479 | _ => Err(self.peek_error(ErrorCode::TrailingCharacters)), |
1075 | | } |
1076 | | } |
1077 | 3.48k | Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)), |
1078 | 1.83k | None => Err(self.peek_error(ErrorCode::EofWhileParsingList)), |
1079 | | } |
1080 | 5.99k | } |
1081 | | |
1082 | 593 | fn end_map(&mut self) -> Result<()> { |
1083 | 593 | match tri!(self.parse_whitespace()) { |
1084 | | Some(b'}') => { |
1085 | 68 | self.eat_char(); |
1086 | 68 | Ok(()) |
1087 | | } |
1088 | 94 | Some(b',') => Err(self.peek_error(ErrorCode::TrailingComma)), |
1089 | 292 | Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)), |
1090 | 139 | None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)), |
1091 | | } |
1092 | 593 | } |
1093 | | |
1094 | 0 | fn ignore_value(&mut self) -> Result<()> { |
1095 | 0 | self.scratch.clear(); |
1096 | 0 | let mut enclosing = None; |
1097 | | |
1098 | | loop { |
1099 | 0 | let peek = match tri!(self.parse_whitespace()) { |
1100 | 0 | Some(b) => b, |
1101 | | None => { |
1102 | 0 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1103 | | } |
1104 | | }; |
1105 | | |
1106 | 0 | let frame = match peek { |
1107 | | b'n' => { |
1108 | 0 | self.eat_char(); |
1109 | 0 | tri!(self.parse_ident(b"ull")); |
1110 | 0 | None |
1111 | | } |
1112 | | b't' => { |
1113 | 0 | self.eat_char(); |
1114 | 0 | tri!(self.parse_ident(b"rue")); |
1115 | 0 | None |
1116 | | } |
1117 | | b'f' => { |
1118 | 0 | self.eat_char(); |
1119 | 0 | tri!(self.parse_ident(b"alse")); |
1120 | 0 | None |
1121 | | } |
1122 | | b'-' => { |
1123 | 0 | self.eat_char(); |
1124 | 0 | tri!(self.ignore_integer()); |
1125 | 0 | None |
1126 | | } |
1127 | 0 | b'0'..=b'9' => { |
1128 | 0 | tri!(self.ignore_integer()); |
1129 | 0 | None |
1130 | | } |
1131 | | b'"' => { |
1132 | 0 | self.eat_char(); |
1133 | 0 | tri!(self.read.ignore_str()); |
1134 | 0 | None |
1135 | | } |
1136 | 0 | frame @ (b'[' | b'{') => { |
1137 | 0 | self.scratch.extend(enclosing.take()); |
1138 | 0 | self.eat_char(); |
1139 | 0 | Some(frame) |
1140 | | } |
1141 | 0 | _ => return Err(self.peek_error(ErrorCode::ExpectedSomeValue)), |
1142 | | }; |
1143 | | |
1144 | 0 | let (mut accept_comma, mut frame) = match frame { |
1145 | 0 | Some(frame) => (false, frame), |
1146 | 0 | None => match enclosing.take() { |
1147 | 0 | Some(frame) => (true, frame), |
1148 | 0 | None => match self.scratch.pop() { |
1149 | 0 | Some(frame) => (true, frame), |
1150 | 0 | None => return Ok(()), |
1151 | | }, |
1152 | | }, |
1153 | | }; |
1154 | | |
1155 | | loop { |
1156 | 0 | match tri!(self.parse_whitespace()) { |
1157 | 0 | Some(b',') if accept_comma => { |
1158 | 0 | self.eat_char(); |
1159 | 0 | break; |
1160 | | } |
1161 | 0 | Some(b']') if frame == b'[' => {} |
1162 | 0 | Some(b'}') if frame == b'{' => {} |
1163 | | Some(_) => { |
1164 | 0 | if accept_comma { |
1165 | 0 | return Err(self.peek_error(match frame { |
1166 | 0 | b'[' => ErrorCode::ExpectedListCommaOrEnd, |
1167 | 0 | b'{' => ErrorCode::ExpectedObjectCommaOrEnd, |
1168 | 0 | _ => unreachable!(), |
1169 | | })); |
1170 | | } else { |
1171 | 0 | break; |
1172 | | } |
1173 | | } |
1174 | | None => { |
1175 | 0 | return Err(self.peek_error(match frame { |
1176 | 0 | b'[' => ErrorCode::EofWhileParsingList, |
1177 | 0 | b'{' => ErrorCode::EofWhileParsingObject, |
1178 | 0 | _ => unreachable!(), |
1179 | | })); |
1180 | | } |
1181 | | } |
1182 | | |
1183 | 0 | self.eat_char(); |
1184 | 0 | frame = match self.scratch.pop() { |
1185 | 0 | Some(frame) => frame, |
1186 | 0 | None => return Ok(()), |
1187 | | }; |
1188 | 0 | accept_comma = true; |
1189 | | } |
1190 | | |
1191 | 0 | if frame == b'{' { |
1192 | 0 | match tri!(self.parse_whitespace()) { |
1193 | 0 | Some(b'"') => self.eat_char(), |
1194 | 0 | Some(_) => return Err(self.peek_error(ErrorCode::KeyMustBeAString)), |
1195 | 0 | None => return Err(self.peek_error(ErrorCode::EofWhileParsingObject)), |
1196 | | } |
1197 | 0 | tri!(self.read.ignore_str()); |
1198 | 0 | match tri!(self.parse_whitespace()) { |
1199 | 0 | Some(b':') => self.eat_char(), |
1200 | 0 | Some(_) => return Err(self.peek_error(ErrorCode::ExpectedColon)), |
1201 | 0 | None => return Err(self.peek_error(ErrorCode::EofWhileParsingObject)), |
1202 | | } |
1203 | 0 | } |
1204 | | |
1205 | 0 | enclosing = Some(frame); |
1206 | | } |
1207 | 0 | } |
1208 | | |
1209 | 0 | fn ignore_integer(&mut self) -> Result<()> { |
1210 | 0 | match tri!(self.next_char_or_null()) { |
1211 | | b'0' => { |
1212 | | // There can be only one leading '0'. |
1213 | 0 | if let b'0'..=b'9' = tri!(self.peek_or_null()) { |
1214 | 0 | return Err(self.peek_error(ErrorCode::InvalidNumber)); |
1215 | 0 | } |
1216 | | } |
1217 | 0 | b'1'..=b'9' => { |
1218 | 0 | while let b'0'..=b'9' = tri!(self.peek_or_null()) { |
1219 | 0 | self.eat_char(); |
1220 | 0 | } |
1221 | | } |
1222 | | _ => { |
1223 | 0 | return Err(self.error(ErrorCode::InvalidNumber)); |
1224 | | } |
1225 | | } |
1226 | | |
1227 | 0 | match tri!(self.peek_or_null()) { |
1228 | 0 | b'.' => self.ignore_decimal(), |
1229 | 0 | b'e' | b'E' => self.ignore_exponent(), |
1230 | 0 | _ => Ok(()), |
1231 | | } |
1232 | 0 | } |
1233 | | |
1234 | 0 | fn ignore_decimal(&mut self) -> Result<()> { |
1235 | 0 | self.eat_char(); |
1236 | 0 |
|
1237 | 0 | let mut at_least_one_digit = false; |
1238 | 0 | while let b'0'..=b'9' = tri!(self.peek_or_null()) { |
1239 | 0 | self.eat_char(); |
1240 | 0 | at_least_one_digit = true; |
1241 | 0 | } |
1242 | | |
1243 | 0 | if !at_least_one_digit { |
1244 | 0 | return Err(self.peek_error(ErrorCode::InvalidNumber)); |
1245 | 0 | } |
1246 | | |
1247 | 0 | match tri!(self.peek_or_null()) { |
1248 | 0 | b'e' | b'E' => self.ignore_exponent(), |
1249 | 0 | _ => Ok(()), |
1250 | | } |
1251 | 0 | } |
1252 | | |
1253 | 0 | fn ignore_exponent(&mut self) -> Result<()> { |
1254 | 0 | self.eat_char(); |
1255 | | |
1256 | 0 | match tri!(self.peek_or_null()) { |
1257 | 0 | b'+' | b'-' => self.eat_char(), |
1258 | 0 | _ => {} |
1259 | | } |
1260 | | |
1261 | | // Make sure a digit follows the exponent place. |
1262 | 0 | match tri!(self.next_char_or_null()) { |
1263 | 0 | b'0'..=b'9' => {} |
1264 | | _ => { |
1265 | 0 | return Err(self.error(ErrorCode::InvalidNumber)); |
1266 | | } |
1267 | | } |
1268 | | |
1269 | 0 | while let b'0'..=b'9' = tri!(self.peek_or_null()) { |
1270 | 0 | self.eat_char(); |
1271 | 0 | } |
1272 | | |
1273 | 0 | Ok(()) |
1274 | 0 | } |
1275 | | |
1276 | | #[cfg(feature = "raw_value")] |
1277 | | fn deserialize_raw_value<V>(&mut self, visitor: V) -> Result<V::Value> |
1278 | | where |
1279 | | V: de::Visitor<'de>, |
1280 | | { |
1281 | | tri!(self.parse_whitespace()); |
1282 | | self.read.begin_raw_buffering(); |
1283 | | tri!(self.ignore_value()); |
1284 | | self.read.end_raw_buffering(visitor) |
1285 | | } |
1286 | | } |
1287 | | |
1288 | | impl FromStr for Number { |
1289 | | type Err = Error; |
1290 | | |
1291 | 0 | fn from_str(s: &str) -> result::Result<Self, Self::Err> { |
1292 | 0 | Deserializer::from_str(s) |
1293 | 0 | .parse_any_signed_number() |
1294 | 0 | .map(Into::into) |
1295 | 0 | } |
1296 | | } |
1297 | | |
1298 | | #[cfg(not(feature = "float_roundtrip"))] |
1299 | | static POW10: [f64; 309] = [ |
1300 | | 1e000, 1e001, 1e002, 1e003, 1e004, 1e005, 1e006, 1e007, 1e008, 1e009, // |
1301 | | 1e010, 1e011, 1e012, 1e013, 1e014, 1e015, 1e016, 1e017, 1e018, 1e019, // |
1302 | | 1e020, 1e021, 1e022, 1e023, 1e024, 1e025, 1e026, 1e027, 1e028, 1e029, // |
1303 | | 1e030, 1e031, 1e032, 1e033, 1e034, 1e035, 1e036, 1e037, 1e038, 1e039, // |
1304 | | 1e040, 1e041, 1e042, 1e043, 1e044, 1e045, 1e046, 1e047, 1e048, 1e049, // |
1305 | | 1e050, 1e051, 1e052, 1e053, 1e054, 1e055, 1e056, 1e057, 1e058, 1e059, // |
1306 | | 1e060, 1e061, 1e062, 1e063, 1e064, 1e065, 1e066, 1e067, 1e068, 1e069, // |
1307 | | 1e070, 1e071, 1e072, 1e073, 1e074, 1e075, 1e076, 1e077, 1e078, 1e079, // |
1308 | | 1e080, 1e081, 1e082, 1e083, 1e084, 1e085, 1e086, 1e087, 1e088, 1e089, // |
1309 | | 1e090, 1e091, 1e092, 1e093, 1e094, 1e095, 1e096, 1e097, 1e098, 1e099, // |
1310 | | 1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109, // |
1311 | | 1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119, // |
1312 | | 1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129, // |
1313 | | 1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139, // |
1314 | | 1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149, // |
1315 | | 1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159, // |
1316 | | 1e160, 1e161, 1e162, 1e163, 1e164, 1e165, 1e166, 1e167, 1e168, 1e169, // |
1317 | | 1e170, 1e171, 1e172, 1e173, 1e174, 1e175, 1e176, 1e177, 1e178, 1e179, // |
1318 | | 1e180, 1e181, 1e182, 1e183, 1e184, 1e185, 1e186, 1e187, 1e188, 1e189, // |
1319 | | 1e190, 1e191, 1e192, 1e193, 1e194, 1e195, 1e196, 1e197, 1e198, 1e199, // |
1320 | | 1e200, 1e201, 1e202, 1e203, 1e204, 1e205, 1e206, 1e207, 1e208, 1e209, // |
1321 | | 1e210, 1e211, 1e212, 1e213, 1e214, 1e215, 1e216, 1e217, 1e218, 1e219, // |
1322 | | 1e220, 1e221, 1e222, 1e223, 1e224, 1e225, 1e226, 1e227, 1e228, 1e229, // |
1323 | | 1e230, 1e231, 1e232, 1e233, 1e234, 1e235, 1e236, 1e237, 1e238, 1e239, // |
1324 | | 1e240, 1e241, 1e242, 1e243, 1e244, 1e245, 1e246, 1e247, 1e248, 1e249, // |
1325 | | 1e250, 1e251, 1e252, 1e253, 1e254, 1e255, 1e256, 1e257, 1e258, 1e259, // |
1326 | | 1e260, 1e261, 1e262, 1e263, 1e264, 1e265, 1e266, 1e267, 1e268, 1e269, // |
1327 | | 1e270, 1e271, 1e272, 1e273, 1e274, 1e275, 1e276, 1e277, 1e278, 1e279, // |
1328 | | 1e280, 1e281, 1e282, 1e283, 1e284, 1e285, 1e286, 1e287, 1e288, 1e289, // |
1329 | | 1e290, 1e291, 1e292, 1e293, 1e294, 1e295, 1e296, 1e297, 1e298, 1e299, // |
1330 | | 1e300, 1e301, 1e302, 1e303, 1e304, 1e305, 1e306, 1e307, 1e308, |
1331 | | ]; |
1332 | | |
1333 | | macro_rules! deserialize_number { |
1334 | | ($method:ident) => { |
1335 | | deserialize_number!($method, deserialize_number); |
1336 | | }; |
1337 | | |
1338 | | ($method:ident, $using:ident) => { |
1339 | 0 | fn $method<V>(self, visitor: V) -> Result<V::Value> |
1340 | 0 | where |
1341 | 0 | V: de::Visitor<'de>, |
1342 | 0 | { |
1343 | 0 | self.$using(visitor) |
1344 | 0 | } Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde::de::Deserializer>::deserialize_i8::<_> Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde::de::Deserializer>::deserialize_i16::<_> Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde::de::Deserializer>::deserialize_i32::<_> Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde::de::Deserializer>::deserialize_i64::<_> Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde::de::Deserializer>::deserialize_u8::<_> Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde::de::Deserializer>::deserialize_u16::<_> Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde::de::Deserializer>::deserialize_u32::<_> Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde::de::Deserializer>::deserialize_u64::<_> Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde::de::Deserializer>::deserialize_f32::<_> Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde::de::Deserializer>::deserialize_f64::<_> Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde::de::Deserializer>::deserialize_i128::<_> Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde::de::Deserializer>::deserialize_u128::<_> |
1345 | | }; |
1346 | | } |
1347 | | |
1348 | | #[cfg(not(feature = "unbounded_depth"))] |
1349 | | macro_rules! if_checking_recursion_limit { |
1350 | | ($($body:tt)*) => { |
1351 | | $($body)* |
1352 | | }; |
1353 | | } |
1354 | | |
1355 | | #[cfg(feature = "unbounded_depth")] |
1356 | | macro_rules! if_checking_recursion_limit { |
1357 | | ($this:ident $($body:tt)*) => { |
1358 | | if !$this.disable_recursion_limit { |
1359 | | $this $($body)* |
1360 | | } |
1361 | | }; |
1362 | | } |
1363 | | |
1364 | | macro_rules! check_recursion { |
1365 | | ($this:ident $($body:tt)*) => { |
1366 | | if_checking_recursion_limit! { |
1367 | | $this.remaining_depth -= 1; |
1368 | | if $this.remaining_depth == 0 { |
1369 | | return Err($this.peek_error(ErrorCode::RecursionLimitExceeded)); |
1370 | | } |
1371 | | } |
1372 | | |
1373 | | $this $($body)* |
1374 | | |
1375 | | if_checking_recursion_limit! { |
1376 | | $this.remaining_depth += 1; |
1377 | | } |
1378 | | }; |
1379 | | } |
1380 | | |
1381 | | impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> { |
1382 | | type Error = Error; |
1383 | | |
1384 | | #[inline] |
1385 | 24.4k | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
1386 | 24.4k | where |
1387 | 24.4k | V: de::Visitor<'de>, |
1388 | 24.4k | { |
1389 | 24.4k | let peek = match tri!(self.parse_whitespace()) { |
1390 | 24.2k | Some(b) => b, |
1391 | | None => { |
1392 | 202 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1393 | | } |
1394 | | }; |
1395 | | |
1396 | 24.2k | let value = match peek { |
1397 | | b'n' => { |
1398 | 108 | self.eat_char(); |
1399 | 108 | tri!(self.parse_ident(b"ull")); |
1400 | 0 | visitor.visit_unit() |
1401 | | } |
1402 | | b't' => { |
1403 | 766 | self.eat_char(); |
1404 | 766 | tri!(self.parse_ident(b"rue")); |
1405 | 133 | visitor.visit_bool(true) |
1406 | | } |
1407 | | b'f' => { |
1408 | 257 | self.eat_char(); |
1409 | 257 | tri!(self.parse_ident(b"alse")); |
1410 | 0 | visitor.visit_bool(false) |
1411 | | } |
1412 | | b'-' => { |
1413 | 701 | self.eat_char(); |
1414 | 701 | tri!(self.parse_any_number(false)).visit(visitor) |
1415 | | } |
1416 | 13.6k | b'0'..=b'9' => tri!(self.parse_any_number(true)).visit(visitor), |
1417 | | b'"' => { |
1418 | 1.09k | self.eat_char(); |
1419 | 1.09k | self.scratch.clear(); |
1420 | 1.09k | match tri!(self.read.parse_str(&mut self.scratch)) { |
1421 | 54 | Reference::Borrowed(s) => visitor.visit_borrowed_str(s), |
1422 | 2 | Reference::Copied(s) => visitor.visit_str(s), |
1423 | | } |
1424 | | } |
1425 | | b'[' => { |
1426 | 5.99k | check_recursion! { |
1427 | | self.eat_char(); |
1428 | 5.99k | let ret = visitor.visit_seq(SeqAccess::new(self)); |
1429 | 5.99k | } |
1430 | 5.99k | |
1431 | 5.99k | match (ret, self.end_seq()) { |
1432 | 48 | (Ok(ret), Ok(())) => Ok(ret), |
1433 | 5.95k | (Err(err), _) | (_, Err(err)) => Err(err), |
1434 | | } |
1435 | | } |
1436 | | b'{' => { |
1437 | 593 | check_recursion! { |
1438 | | self.eat_char(); |
1439 | 593 | let ret = visitor.visit_map(MapAccess::new(self)); |
1440 | 593 | } |
1441 | 593 | |
1442 | 593 | match (ret, self.end_map()) { |
1443 | 68 | (Ok(ret), Ok(())) => Ok(ret), |
1444 | 525 | (Err(err), _) | (_, Err(err)) => Err(err), |
1445 | | } |
1446 | | } |
1447 | 1.24k | _ => Err(self.peek_error(ErrorCode::ExpectedSomeValue)), |
1448 | | }; |
1449 | | |
1450 | 17.5k | match value { |
1451 | 9.78k | Ok(value) => Ok(value), |
1452 | | // The de::Error impl creates errors with unknown line and column. |
1453 | | // Fill in the position here by looking at the current index in the |
1454 | | // input. There is no way to tell whether this should call `error` |
1455 | | // or `peek_error` so pick the one that seems correct more often. |
1456 | | // Worst case, the position is off by one character. |
1457 | 7.71k | Err(err) => Err(self.fix_position(err)), |
1458 | | } |
1459 | 24.4k | } <&mut serde_json::de::Deserializer<serde_json::read::StrRead> as serde::de::Deserializer>::deserialize_any::<rhai::serde::deserialize::DynamicVisitor> Line | Count | Source | 1385 | 24.4k | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> | 1386 | 24.4k | where | 1387 | 24.4k | V: de::Visitor<'de>, | 1388 | 24.4k | { | 1389 | 24.4k | let peek = match tri!(self.parse_whitespace()) { | 1390 | 24.2k | Some(b) => b, | 1391 | | None => { | 1392 | 202 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); | 1393 | | } | 1394 | | }; | 1395 | | | 1396 | 24.2k | let value = match peek { | 1397 | | b'n' => { | 1398 | 108 | self.eat_char(); | 1399 | 108 | tri!(self.parse_ident(b"ull")); | 1400 | 0 | visitor.visit_unit() | 1401 | | } | 1402 | | b't' => { | 1403 | 766 | self.eat_char(); | 1404 | 766 | tri!(self.parse_ident(b"rue")); | 1405 | 133 | visitor.visit_bool(true) | 1406 | | } | 1407 | | b'f' => { | 1408 | 257 | self.eat_char(); | 1409 | 257 | tri!(self.parse_ident(b"alse")); | 1410 | 0 | visitor.visit_bool(false) | 1411 | | } | 1412 | | b'-' => { | 1413 | 701 | self.eat_char(); | 1414 | 701 | tri!(self.parse_any_number(false)).visit(visitor) | 1415 | | } | 1416 | 13.6k | b'0'..=b'9' => tri!(self.parse_any_number(true)).visit(visitor), | 1417 | | b'"' => { | 1418 | 1.09k | self.eat_char(); | 1419 | 1.09k | self.scratch.clear(); | 1420 | 1.09k | match tri!(self.read.parse_str(&mut self.scratch)) { | 1421 | 54 | Reference::Borrowed(s) => visitor.visit_borrowed_str(s), | 1422 | 2 | Reference::Copied(s) => visitor.visit_str(s), | 1423 | | } | 1424 | | } | 1425 | | b'[' => { | 1426 | 5.99k | check_recursion! { | 1427 | | self.eat_char(); | 1428 | 5.99k | let ret = visitor.visit_seq(SeqAccess::new(self)); | 1429 | 5.99k | } | 1430 | 5.99k | | 1431 | 5.99k | match (ret, self.end_seq()) { | 1432 | 48 | (Ok(ret), Ok(())) => Ok(ret), | 1433 | 5.95k | (Err(err), _) | (_, Err(err)) => Err(err), | 1434 | | } | 1435 | | } | 1436 | | b'{' => { | 1437 | 593 | check_recursion! { | 1438 | | self.eat_char(); | 1439 | 593 | let ret = visitor.visit_map(MapAccess::new(self)); | 1440 | 593 | } | 1441 | 593 | | 1442 | 593 | match (ret, self.end_map()) { | 1443 | 68 | (Ok(ret), Ok(())) => Ok(ret), | 1444 | 525 | (Err(err), _) | (_, Err(err)) => Err(err), | 1445 | | } | 1446 | | } | 1447 | 1.24k | _ => Err(self.peek_error(ErrorCode::ExpectedSomeValue)), | 1448 | | }; | 1449 | | | 1450 | 17.5k | match value { | 1451 | 9.78k | Ok(value) => Ok(value), | 1452 | | // The de::Error impl creates errors with unknown line and column. | 1453 | | // Fill in the position here by looking at the current index in the | 1454 | | // input. There is no way to tell whether this should call `error` | 1455 | | // or `peek_error` so pick the one that seems correct more often. | 1456 | | // Worst case, the position is off by one character. | 1457 | 7.71k | Err(err) => Err(self.fix_position(err)), | 1458 | | } | 1459 | 24.4k | } |
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::StrRead> as serde::de::Deserializer>::deserialize_any::<<serde_json::value::Value as serde::de::Deserialize>::deserialize::ValueVisitor> |
1460 | | |
1461 | 0 | fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value> |
1462 | 0 | where |
1463 | 0 | V: de::Visitor<'de>, |
1464 | 0 | { |
1465 | 0 | let peek = match tri!(self.parse_whitespace()) { |
1466 | 0 | Some(b) => b, |
1467 | | None => { |
1468 | 0 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1469 | | } |
1470 | | }; |
1471 | | |
1472 | 0 | let value = match peek { |
1473 | | b't' => { |
1474 | 0 | self.eat_char(); |
1475 | 0 | tri!(self.parse_ident(b"rue")); |
1476 | 0 | visitor.visit_bool(true) |
1477 | | } |
1478 | | b'f' => { |
1479 | 0 | self.eat_char(); |
1480 | 0 | tri!(self.parse_ident(b"alse")); |
1481 | 0 | visitor.visit_bool(false) |
1482 | | } |
1483 | 0 | _ => Err(self.peek_invalid_type(&visitor)), |
1484 | | }; |
1485 | | |
1486 | 0 | match value { |
1487 | 0 | Ok(value) => Ok(value), |
1488 | 0 | Err(err) => Err(self.fix_position(err)), |
1489 | | } |
1490 | 0 | } |
1491 | | |
1492 | | deserialize_number!(deserialize_i8); |
1493 | | deserialize_number!(deserialize_i16); |
1494 | | deserialize_number!(deserialize_i32); |
1495 | | deserialize_number!(deserialize_i64); |
1496 | | deserialize_number!(deserialize_u8); |
1497 | | deserialize_number!(deserialize_u16); |
1498 | | deserialize_number!(deserialize_u32); |
1499 | | deserialize_number!(deserialize_u64); |
1500 | | #[cfg(not(feature = "float_roundtrip"))] |
1501 | | deserialize_number!(deserialize_f32); |
1502 | | deserialize_number!(deserialize_f64); |
1503 | | |
1504 | | #[cfg(feature = "float_roundtrip")] |
1505 | | deserialize_number!(deserialize_f32, do_deserialize_f32); |
1506 | | deserialize_number!(deserialize_i128, do_deserialize_i128); |
1507 | | deserialize_number!(deserialize_u128, do_deserialize_u128); |
1508 | | |
1509 | 0 | fn deserialize_char<V>(self, visitor: V) -> Result<V::Value> |
1510 | 0 | where |
1511 | 0 | V: de::Visitor<'de>, |
1512 | 0 | { |
1513 | 0 | self.deserialize_str(visitor) |
1514 | 0 | } |
1515 | | |
1516 | 0 | fn deserialize_str<V>(self, visitor: V) -> Result<V::Value> |
1517 | 0 | where |
1518 | 0 | V: de::Visitor<'de>, |
1519 | 0 | { |
1520 | 0 | let peek = match tri!(self.parse_whitespace()) { |
1521 | 0 | Some(b) => b, |
1522 | | None => { |
1523 | 0 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1524 | | } |
1525 | | }; |
1526 | | |
1527 | 0 | let value = match peek { |
1528 | | b'"' => { |
1529 | 0 | self.eat_char(); |
1530 | 0 | self.scratch.clear(); |
1531 | 0 | match tri!(self.read.parse_str(&mut self.scratch)) { |
1532 | 0 | Reference::Borrowed(s) => visitor.visit_borrowed_str(s), |
1533 | 0 | Reference::Copied(s) => visitor.visit_str(s), |
1534 | | } |
1535 | | } |
1536 | 0 | _ => Err(self.peek_invalid_type(&visitor)), |
1537 | | }; |
1538 | | |
1539 | 0 | match value { |
1540 | 0 | Ok(value) => Ok(value), |
1541 | 0 | Err(err) => Err(self.fix_position(err)), |
1542 | | } |
1543 | 0 | } |
1544 | | |
1545 | 0 | fn deserialize_string<V>(self, visitor: V) -> Result<V::Value> |
1546 | 0 | where |
1547 | 0 | V: de::Visitor<'de>, |
1548 | 0 | { |
1549 | 0 | self.deserialize_str(visitor) |
1550 | 0 | } |
1551 | | |
1552 | | /// Parses a JSON string as bytes. Note that this function does not check |
1553 | | /// whether the bytes represent a valid UTF-8 string. |
1554 | | /// |
1555 | | /// The relevant part of the JSON specification is Section 8.2 of [RFC |
1556 | | /// 7159]: |
1557 | | /// |
1558 | | /// > When all the strings represented in a JSON text are composed entirely |
1559 | | /// > of Unicode characters (however escaped), then that JSON text is |
1560 | | /// > interoperable in the sense that all software implementations that |
1561 | | /// > parse it will agree on the contents of names and of string values in |
1562 | | /// > objects and arrays. |
1563 | | /// > |
1564 | | /// > However, the ABNF in this specification allows member names and string |
1565 | | /// > values to contain bit sequences that cannot encode Unicode characters; |
1566 | | /// > for example, "\uDEAD" (a single unpaired UTF-16 surrogate). Instances |
1567 | | /// > of this have been observed, for example, when a library truncates a |
1568 | | /// > UTF-16 string without checking whether the truncation split a |
1569 | | /// > surrogate pair. The behavior of software that receives JSON texts |
1570 | | /// > containing such values is unpredictable; for example, implementations |
1571 | | /// > might return different values for the length of a string value or even |
1572 | | /// > suffer fatal runtime exceptions. |
1573 | | /// |
1574 | | /// [RFC 7159]: https://tools.ietf.org/html/rfc7159 |
1575 | | /// |
1576 | | /// The behavior of serde_json is specified to fail on non-UTF-8 strings |
1577 | | /// when deserializing into Rust UTF-8 string types such as String, and |
1578 | | /// succeed with non-UTF-8 bytes when deserializing using this method. |
1579 | | /// |
1580 | | /// Escape sequences are processed as usual, and for `\uXXXX` escapes it is |
1581 | | /// still checked if the hex number represents a valid Unicode code point. |
1582 | | /// |
1583 | | /// # Examples |
1584 | | /// |
1585 | | /// You can use this to parse JSON strings containing invalid UTF-8 bytes, |
1586 | | /// or unpaired surrogates. |
1587 | | /// |
1588 | | /// ``` |
1589 | | /// use serde_bytes::ByteBuf; |
1590 | | /// |
1591 | | /// fn look_at_bytes() -> Result<(), serde_json::Error> { |
1592 | | /// let json_data = b"\"some bytes: \xe5\x00\xe5\""; |
1593 | | /// let bytes: ByteBuf = serde_json::from_slice(json_data)?; |
1594 | | /// |
1595 | | /// assert_eq!(b'\xe5', bytes[12]); |
1596 | | /// assert_eq!(b'\0', bytes[13]); |
1597 | | /// assert_eq!(b'\xe5', bytes[14]); |
1598 | | /// |
1599 | | /// Ok(()) |
1600 | | /// } |
1601 | | /// # |
1602 | | /// # look_at_bytes().unwrap(); |
1603 | | /// ``` |
1604 | | /// |
1605 | | /// Backslash escape sequences like `\n` are still interpreted and required |
1606 | | /// to be valid. `\u` escape sequences are required to represent a valid |
1607 | | /// Unicode code point or lone surrogate. |
1608 | | /// |
1609 | | /// ``` |
1610 | | /// use serde_bytes::ByteBuf; |
1611 | | /// |
1612 | | /// fn look_at_bytes() -> Result<(), serde_json::Error> { |
1613 | | /// let json_data = b"\"lone surrogate: \\uD801\""; |
1614 | | /// let bytes: ByteBuf = serde_json::from_slice(json_data)?; |
1615 | | /// let expected = b"lone surrogate: \xED\xA0\x81"; |
1616 | | /// assert_eq!(expected, bytes.as_slice()); |
1617 | | /// Ok(()) |
1618 | | /// } |
1619 | | /// # |
1620 | | /// # look_at_bytes(); |
1621 | | /// ``` |
1622 | 0 | fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value> |
1623 | 0 | where |
1624 | 0 | V: de::Visitor<'de>, |
1625 | 0 | { |
1626 | 0 | let peek = match tri!(self.parse_whitespace()) { |
1627 | 0 | Some(b) => b, |
1628 | | None => { |
1629 | 0 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1630 | | } |
1631 | | }; |
1632 | | |
1633 | 0 | let value = match peek { |
1634 | | b'"' => { |
1635 | 0 | self.eat_char(); |
1636 | 0 | self.scratch.clear(); |
1637 | 0 | match tri!(self.read.parse_str_raw(&mut self.scratch)) { |
1638 | 0 | Reference::Borrowed(b) => visitor.visit_borrowed_bytes(b), |
1639 | 0 | Reference::Copied(b) => visitor.visit_bytes(b), |
1640 | | } |
1641 | | } |
1642 | 0 | b'[' => self.deserialize_seq(visitor), |
1643 | 0 | _ => Err(self.peek_invalid_type(&visitor)), |
1644 | | }; |
1645 | | |
1646 | 0 | match value { |
1647 | 0 | Ok(value) => Ok(value), |
1648 | 0 | Err(err) => Err(self.fix_position(err)), |
1649 | | } |
1650 | 0 | } |
1651 | | |
1652 | | #[inline] |
1653 | 0 | fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value> |
1654 | 0 | where |
1655 | 0 | V: de::Visitor<'de>, |
1656 | 0 | { |
1657 | 0 | self.deserialize_bytes(visitor) |
1658 | 0 | } |
1659 | | |
1660 | | /// Parses a `null` as a None, and any other values as a `Some(...)`. |
1661 | | #[inline] |
1662 | 0 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value> |
1663 | 0 | where |
1664 | 0 | V: de::Visitor<'de>, |
1665 | 0 | { |
1666 | 0 | match tri!(self.parse_whitespace()) { |
1667 | | Some(b'n') => { |
1668 | 0 | self.eat_char(); |
1669 | 0 | tri!(self.parse_ident(b"ull")); |
1670 | 0 | visitor.visit_none() |
1671 | | } |
1672 | 0 | _ => visitor.visit_some(self), |
1673 | | } |
1674 | 0 | } |
1675 | | |
1676 | 0 | fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value> |
1677 | 0 | where |
1678 | 0 | V: de::Visitor<'de>, |
1679 | 0 | { |
1680 | 0 | let peek = match tri!(self.parse_whitespace()) { |
1681 | 0 | Some(b) => b, |
1682 | | None => { |
1683 | 0 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1684 | | } |
1685 | | }; |
1686 | | |
1687 | 0 | let value = match peek { |
1688 | | b'n' => { |
1689 | 0 | self.eat_char(); |
1690 | 0 | tri!(self.parse_ident(b"ull")); |
1691 | 0 | visitor.visit_unit() |
1692 | | } |
1693 | 0 | _ => Err(self.peek_invalid_type(&visitor)), |
1694 | | }; |
1695 | | |
1696 | 0 | match value { |
1697 | 0 | Ok(value) => Ok(value), |
1698 | 0 | Err(err) => Err(self.fix_position(err)), |
1699 | | } |
1700 | 0 | } |
1701 | | |
1702 | 0 | fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value> |
1703 | 0 | where |
1704 | 0 | V: de::Visitor<'de>, |
1705 | 0 | { |
1706 | 0 | self.deserialize_unit(visitor) |
1707 | 0 | } |
1708 | | |
1709 | | /// Parses a newtype struct as the underlying value. |
1710 | | #[inline] |
1711 | 0 | fn deserialize_newtype_struct<V>(self, name: &str, visitor: V) -> Result<V::Value> |
1712 | 0 | where |
1713 | 0 | V: de::Visitor<'de>, |
1714 | 0 | { |
1715 | 0 | #[cfg(feature = "raw_value")] |
1716 | 0 | { |
1717 | 0 | if name == crate::raw::TOKEN { |
1718 | 0 | return self.deserialize_raw_value(visitor); |
1719 | 0 | } |
1720 | 0 | } |
1721 | 0 |
|
1722 | 0 | let _ = name; |
1723 | 0 | visitor.visit_newtype_struct(self) |
1724 | 0 | } |
1725 | | |
1726 | 0 | fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value> |
1727 | 0 | where |
1728 | 0 | V: de::Visitor<'de>, |
1729 | 0 | { |
1730 | 0 | let peek = match tri!(self.parse_whitespace()) { |
1731 | 0 | Some(b) => b, |
1732 | | None => { |
1733 | 0 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1734 | | } |
1735 | | }; |
1736 | | |
1737 | 0 | let value = match peek { |
1738 | | b'[' => { |
1739 | 0 | check_recursion! { |
1740 | | self.eat_char(); |
1741 | 0 | let ret = visitor.visit_seq(SeqAccess::new(self)); |
1742 | 0 | } |
1743 | 0 |
|
1744 | 0 | match (ret, self.end_seq()) { |
1745 | 0 | (Ok(ret), Ok(())) => Ok(ret), |
1746 | 0 | (Err(err), _) | (_, Err(err)) => Err(err), |
1747 | | } |
1748 | | } |
1749 | 0 | _ => Err(self.peek_invalid_type(&visitor)), |
1750 | | }; |
1751 | | |
1752 | 0 | match value { |
1753 | 0 | Ok(value) => Ok(value), |
1754 | 0 | Err(err) => Err(self.fix_position(err)), |
1755 | | } |
1756 | 0 | } |
1757 | | |
1758 | 0 | fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value> |
1759 | 0 | where |
1760 | 0 | V: de::Visitor<'de>, |
1761 | 0 | { |
1762 | 0 | self.deserialize_seq(visitor) |
1763 | 0 | } |
1764 | | |
1765 | 0 | fn deserialize_tuple_struct<V>( |
1766 | 0 | self, |
1767 | 0 | _name: &'static str, |
1768 | 0 | _len: usize, |
1769 | 0 | visitor: V, |
1770 | 0 | ) -> Result<V::Value> |
1771 | 0 | where |
1772 | 0 | V: de::Visitor<'de>, |
1773 | 0 | { |
1774 | 0 | self.deserialize_seq(visitor) |
1775 | 0 | } |
1776 | | |
1777 | 0 | fn deserialize_map<V>(self, visitor: V) -> Result<V::Value> |
1778 | 0 | where |
1779 | 0 | V: de::Visitor<'de>, |
1780 | 0 | { |
1781 | 0 | let peek = match tri!(self.parse_whitespace()) { |
1782 | 0 | Some(b) => b, |
1783 | | None => { |
1784 | 0 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1785 | | } |
1786 | | }; |
1787 | | |
1788 | 0 | let value = match peek { |
1789 | | b'{' => { |
1790 | 0 | check_recursion! { |
1791 | | self.eat_char(); |
1792 | 0 | let ret = visitor.visit_map(MapAccess::new(self)); |
1793 | 0 | } |
1794 | 0 |
|
1795 | 0 | match (ret, self.end_map()) { |
1796 | 0 | (Ok(ret), Ok(())) => Ok(ret), |
1797 | 0 | (Err(err), _) | (_, Err(err)) => Err(err), |
1798 | | } |
1799 | | } |
1800 | 0 | _ => Err(self.peek_invalid_type(&visitor)), |
1801 | | }; |
1802 | | |
1803 | 0 | match value { |
1804 | 0 | Ok(value) => Ok(value), |
1805 | 0 | Err(err) => Err(self.fix_position(err)), |
1806 | | } |
1807 | 0 | } |
1808 | | |
1809 | 0 | fn deserialize_struct<V>( |
1810 | 0 | self, |
1811 | 0 | _name: &'static str, |
1812 | 0 | _fields: &'static [&'static str], |
1813 | 0 | visitor: V, |
1814 | 0 | ) -> Result<V::Value> |
1815 | 0 | where |
1816 | 0 | V: de::Visitor<'de>, |
1817 | 0 | { |
1818 | 0 | let peek = match tri!(self.parse_whitespace()) { |
1819 | 0 | Some(b) => b, |
1820 | | None => { |
1821 | 0 | return Err(self.peek_error(ErrorCode::EofWhileParsingValue)); |
1822 | | } |
1823 | | }; |
1824 | | |
1825 | 0 | let value = match peek { |
1826 | | b'[' => { |
1827 | 0 | check_recursion! { |
1828 | | self.eat_char(); |
1829 | 0 | let ret = visitor.visit_seq(SeqAccess::new(self)); |
1830 | 0 | } |
1831 | 0 |
|
1832 | 0 | match (ret, self.end_seq()) { |
1833 | 0 | (Ok(ret), Ok(())) => Ok(ret), |
1834 | 0 | (Err(err), _) | (_, Err(err)) => Err(err), |
1835 | | } |
1836 | | } |
1837 | | b'{' => { |
1838 | 0 | check_recursion! { |
1839 | | self.eat_char(); |
1840 | 0 | let ret = visitor.visit_map(MapAccess::new(self)); |
1841 | 0 | } |
1842 | 0 |
|
1843 | 0 | match (ret, self.end_map()) { |
1844 | 0 | (Ok(ret), Ok(())) => Ok(ret), |
1845 | 0 | (Err(err), _) | (_, Err(err)) => Err(err), |
1846 | | } |
1847 | | } |
1848 | 0 | _ => Err(self.peek_invalid_type(&visitor)), |
1849 | | }; |
1850 | | |
1851 | 0 | match value { |
1852 | 0 | Ok(value) => Ok(value), |
1853 | 0 | Err(err) => Err(self.fix_position(err)), |
1854 | | } |
1855 | 0 | } |
1856 | | |
1857 | | /// Parses an enum as an object like `{"$KEY":$VALUE}`, where $VALUE is either a straight |
1858 | | /// value, a `[..]`, or a `{..}`. |
1859 | | #[inline] |
1860 | 0 | fn deserialize_enum<V>( |
1861 | 0 | self, |
1862 | 0 | _name: &str, |
1863 | 0 | _variants: &'static [&'static str], |
1864 | 0 | visitor: V, |
1865 | 0 | ) -> Result<V::Value> |
1866 | 0 | where |
1867 | 0 | V: de::Visitor<'de>, |
1868 | 0 | { |
1869 | 0 | match tri!(self.parse_whitespace()) { |
1870 | | Some(b'{') => { |
1871 | 0 | check_recursion! { |
1872 | | self.eat_char(); |
1873 | 0 | let value = tri!(visitor.visit_enum(VariantAccess::new(self))); |
1874 | | } |
1875 | | |
1876 | 0 | match tri!(self.parse_whitespace()) { |
1877 | | Some(b'}') => { |
1878 | 0 | self.eat_char(); |
1879 | 0 | Ok(value) |
1880 | | } |
1881 | 0 | Some(_) => Err(self.error(ErrorCode::ExpectedSomeValue)), |
1882 | 0 | None => Err(self.error(ErrorCode::EofWhileParsingObject)), |
1883 | | } |
1884 | | } |
1885 | 0 | Some(b'"') => visitor.visit_enum(UnitVariantAccess::new(self)), |
1886 | 0 | Some(_) => Err(self.peek_error(ErrorCode::ExpectedSomeValue)), |
1887 | 0 | None => Err(self.peek_error(ErrorCode::EofWhileParsingValue)), |
1888 | | } |
1889 | 0 | } |
1890 | | |
1891 | 0 | fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value> |
1892 | 0 | where |
1893 | 0 | V: de::Visitor<'de>, |
1894 | 0 | { |
1895 | 0 | self.deserialize_str(visitor) |
1896 | 0 | } |
1897 | | |
1898 | 0 | fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value> |
1899 | 0 | where |
1900 | 0 | V: de::Visitor<'de>, |
1901 | 0 | { |
1902 | 0 | tri!(self.ignore_value()); |
1903 | 0 | visitor.visit_unit() |
1904 | 0 | } |
1905 | | } |
1906 | | |
1907 | | struct SeqAccess<'a, R: 'a> { |
1908 | | de: &'a mut Deserializer<R>, |
1909 | | first: bool, |
1910 | | } |
1911 | | |
1912 | | impl<'a, R: 'a> SeqAccess<'a, R> { |
1913 | 5.99k | fn new(de: &'a mut Deserializer<R>) -> Self { |
1914 | 5.99k | SeqAccess { de, first: true } |
1915 | 5.99k | } |
1916 | | } |
1917 | | |
1918 | | impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> { |
1919 | | type Error = Error; |
1920 | | |
1921 | 10.4k | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>> |
1922 | 10.4k | where |
1923 | 10.4k | T: de::DeserializeSeed<'de>, |
1924 | 10.4k | { |
1925 | 10.4k | let peek = match tri!(self.de.parse_whitespace()) { |
1926 | | Some(b']') => { |
1927 | 48 | return Ok(None); |
1928 | | } |
1929 | 2.65k | Some(b',') if !self.first => { |
1930 | 2.65k | self.de.eat_char(); |
1931 | 2.65k | tri!(self.de.parse_whitespace()) |
1932 | | } |
1933 | 6.81k | Some(b) => { |
1934 | 6.81k | if self.first { |
1935 | 5.99k | self.first = false; |
1936 | 5.99k | Some(b) |
1937 | | } else { |
1938 | 824 | return Err(self.de.peek_error(ErrorCode::ExpectedListCommaOrEnd)); |
1939 | | } |
1940 | | } |
1941 | | None => { |
1942 | 891 | return Err(self.de.peek_error(ErrorCode::EofWhileParsingList)); |
1943 | | } |
1944 | | }; |
1945 | | |
1946 | 8.65k | match peek { |
1947 | 44 | Some(b']') => Err(self.de.peek_error(ErrorCode::TrailingComma)), |
1948 | 8.40k | Some(_) => Ok(Some(tri!(seed.deserialize(&mut *self.de)))), |
1949 | 204 | None => Err(self.de.peek_error(ErrorCode::EofWhileParsingValue)), |
1950 | | } |
1951 | 10.4k | } <serde_json::de::SeqAccess<serde_json::read::StrRead> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<rhai::types::dynamic::Dynamic>> Line | Count | Source | 1921 | 10.4k | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>> | 1922 | 10.4k | where | 1923 | 10.4k | T: de::DeserializeSeed<'de>, | 1924 | 10.4k | { | 1925 | 10.4k | let peek = match tri!(self.de.parse_whitespace()) { | 1926 | | Some(b']') => { | 1927 | 48 | return Ok(None); | 1928 | | } | 1929 | 2.65k | Some(b',') if !self.first => { | 1930 | 2.65k | self.de.eat_char(); | 1931 | 2.65k | tri!(self.de.parse_whitespace()) | 1932 | | } | 1933 | 6.81k | Some(b) => { | 1934 | 6.81k | if self.first { | 1935 | 5.99k | self.first = false; | 1936 | 5.99k | Some(b) | 1937 | | } else { | 1938 | 824 | return Err(self.de.peek_error(ErrorCode::ExpectedListCommaOrEnd)); | 1939 | | } | 1940 | | } | 1941 | | None => { | 1942 | 891 | return Err(self.de.peek_error(ErrorCode::EofWhileParsingList)); | 1943 | | } | 1944 | | }; | 1945 | | | 1946 | 8.65k | match peek { | 1947 | 44 | Some(b']') => Err(self.de.peek_error(ErrorCode::TrailingComma)), | 1948 | 8.40k | Some(_) => Ok(Some(tri!(seed.deserialize(&mut *self.de)))), | 1949 | 204 | None => Err(self.de.peek_error(ErrorCode::EofWhileParsingValue)), | 1950 | | } | 1951 | 10.4k | } |
Unexecuted instantiation: <serde_json::de::SeqAccess<serde_json::read::StrRead> as serde::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<serde_json::value::Value>> |
1952 | | } |
1953 | | |
1954 | | struct MapAccess<'a, R: 'a> { |
1955 | | de: &'a mut Deserializer<R>, |
1956 | | first: bool, |
1957 | | } |
1958 | | |
1959 | | impl<'a, R: 'a> MapAccess<'a, R> { |
1960 | 593 | fn new(de: &'a mut Deserializer<R>) -> Self { |
1961 | 593 | MapAccess { de, first: true } |
1962 | 593 | } |
1963 | | } |
1964 | | |
1965 | | impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> { |
1966 | | type Error = Error; |
1967 | | |
1968 | 593 | fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> |
1969 | 593 | where |
1970 | 593 | K: de::DeserializeSeed<'de>, |
1971 | 593 | { |
1972 | 593 | let peek = match tri!(self.de.parse_whitespace()) { |
1973 | | Some(b'}') => { |
1974 | 68 | return Ok(None); |
1975 | | } |
1976 | 0 | Some(b',') if !self.first => { |
1977 | 0 | self.de.eat_char(); |
1978 | 0 | tri!(self.de.parse_whitespace()) |
1979 | | } |
1980 | 390 | Some(b) => { |
1981 | 390 | if self.first { |
1982 | 390 | self.first = false; |
1983 | 390 | Some(b) |
1984 | | } else { |
1985 | 0 | return Err(self.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd)); |
1986 | | } |
1987 | | } |
1988 | | None => { |
1989 | 135 | return Err(self.de.peek_error(ErrorCode::EofWhileParsingObject)); |
1990 | | } |
1991 | | }; |
1992 | | |
1993 | 390 | match peek { |
1994 | 105 | Some(b'"') => seed.deserialize(MapKey { de: &mut *self.de }).map(Some), |
1995 | 0 | Some(b'}') => Err(self.de.peek_error(ErrorCode::TrailingComma)), |
1996 | 285 | Some(_) => Err(self.de.peek_error(ErrorCode::KeyMustBeAString)), |
1997 | 0 | None => Err(self.de.peek_error(ErrorCode::EofWhileParsingValue)), |
1998 | | } |
1999 | 593 | } <serde_json::de::MapAccess<serde_json::read::StrRead> as serde::de::MapAccess>::next_key_seed::<core::marker::PhantomData<smartstring::SmartString<smartstring::config::LazyCompact>>> Line | Count | Source | 1968 | 593 | fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>> | 1969 | 593 | where | 1970 | 593 | K: de::DeserializeSeed<'de>, | 1971 | 593 | { | 1972 | 593 | let peek = match tri!(self.de.parse_whitespace()) { | 1973 | | Some(b'}') => { | 1974 | 68 | return Ok(None); | 1975 | | } | 1976 | 0 | Some(b',') if !self.first => { | 1977 | 0 | self.de.eat_char(); | 1978 | 0 | tri!(self.de.parse_whitespace()) | 1979 | | } | 1980 | 390 | Some(b) => { | 1981 | 390 | if self.first { | 1982 | 390 | self.first = false; | 1983 | 390 | Some(b) | 1984 | | } else { | 1985 | 0 | return Err(self.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd)); | 1986 | | } | 1987 | | } | 1988 | | None => { | 1989 | 135 | return Err(self.de.peek_error(ErrorCode::EofWhileParsingObject)); | 1990 | | } | 1991 | | }; | 1992 | | | 1993 | 390 | match peek { | 1994 | 105 | Some(b'"') => seed.deserialize(MapKey { de: &mut *self.de }).map(Some), | 1995 | 0 | Some(b'}') => Err(self.de.peek_error(ErrorCode::TrailingComma)), | 1996 | 285 | Some(_) => Err(self.de.peek_error(ErrorCode::KeyMustBeAString)), | 1997 | 0 | None => Err(self.de.peek_error(ErrorCode::EofWhileParsingValue)), | 1998 | | } | 1999 | 593 | } |
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::StrRead> as serde::de::MapAccess>::next_key_seed::<core::marker::PhantomData<alloc::string::String>> Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::StrRead> as serde::de::MapAccess>::next_key_seed::<serde_json::value::de::KeyClassifier> |
2000 | | |
2001 | 40 | fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value> |
2002 | 40 | where |
2003 | 40 | V: de::DeserializeSeed<'de>, |
2004 | 40 | { |
2005 | 40 | tri!(self.de.parse_object_colon()); |
2006 | | |
2007 | 0 | seed.deserialize(&mut *self.de) |
2008 | 40 | } <serde_json::de::MapAccess<serde_json::read::StrRead> as serde::de::MapAccess>::next_value_seed::<core::marker::PhantomData<rhai::types::dynamic::Dynamic>> Line | Count | Source | 2001 | 40 | fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value> | 2002 | 40 | where | 2003 | 40 | V: de::DeserializeSeed<'de>, | 2004 | 40 | { | 2005 | 40 | tri!(self.de.parse_object_colon()); | 2006 | | | 2007 | 0 | seed.deserialize(&mut *self.de) | 2008 | 40 | } |
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::StrRead> as serde::de::MapAccess>::next_value_seed::<core::marker::PhantomData<serde_json::value::Value>> |
2009 | | } |
2010 | | |
2011 | | struct VariantAccess<'a, R: 'a> { |
2012 | | de: &'a mut Deserializer<R>, |
2013 | | } |
2014 | | |
2015 | | impl<'a, R: 'a> VariantAccess<'a, R> { |
2016 | 0 | fn new(de: &'a mut Deserializer<R>) -> Self { |
2017 | 0 | VariantAccess { de } |
2018 | 0 | } |
2019 | | } |
2020 | | |
2021 | | impl<'de, 'a, R: Read<'de> + 'a> de::EnumAccess<'de> for VariantAccess<'a, R> { |
2022 | | type Error = Error; |
2023 | | type Variant = Self; |
2024 | | |
2025 | 0 | fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)> |
2026 | 0 | where |
2027 | 0 | V: de::DeserializeSeed<'de>, |
2028 | 0 | { |
2029 | 0 | let val = tri!(seed.deserialize(&mut *self.de)); |
2030 | 0 | tri!(self.de.parse_object_colon()); |
2031 | 0 | Ok((val, self)) |
2032 | 0 | } |
2033 | | } |
2034 | | |
2035 | | impl<'de, 'a, R: Read<'de> + 'a> de::VariantAccess<'de> for VariantAccess<'a, R> { |
2036 | | type Error = Error; |
2037 | | |
2038 | 0 | fn unit_variant(self) -> Result<()> { |
2039 | 0 | de::Deserialize::deserialize(self.de) |
2040 | 0 | } |
2041 | | |
2042 | 0 | fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value> |
2043 | 0 | where |
2044 | 0 | T: de::DeserializeSeed<'de>, |
2045 | 0 | { |
2046 | 0 | seed.deserialize(self.de) |
2047 | 0 | } |
2048 | | |
2049 | 0 | fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value> |
2050 | 0 | where |
2051 | 0 | V: de::Visitor<'de>, |
2052 | 0 | { |
2053 | 0 | de::Deserializer::deserialize_seq(self.de, visitor) |
2054 | 0 | } |
2055 | | |
2056 | 0 | fn struct_variant<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value> |
2057 | 0 | where |
2058 | 0 | V: de::Visitor<'de>, |
2059 | 0 | { |
2060 | 0 | de::Deserializer::deserialize_struct(self.de, "", fields, visitor) |
2061 | 0 | } |
2062 | | } |
2063 | | |
2064 | | struct UnitVariantAccess<'a, R: 'a> { |
2065 | | de: &'a mut Deserializer<R>, |
2066 | | } |
2067 | | |
2068 | | impl<'a, R: 'a> UnitVariantAccess<'a, R> { |
2069 | 0 | fn new(de: &'a mut Deserializer<R>) -> Self { |
2070 | 0 | UnitVariantAccess { de } |
2071 | 0 | } |
2072 | | } |
2073 | | |
2074 | | impl<'de, 'a, R: Read<'de> + 'a> de::EnumAccess<'de> for UnitVariantAccess<'a, R> { |
2075 | | type Error = Error; |
2076 | | type Variant = Self; |
2077 | | |
2078 | 0 | fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)> |
2079 | 0 | where |
2080 | 0 | V: de::DeserializeSeed<'de>, |
2081 | 0 | { |
2082 | 0 | let variant = tri!(seed.deserialize(&mut *self.de)); |
2083 | 0 | Ok((variant, self)) |
2084 | 0 | } |
2085 | | } |
2086 | | |
2087 | | impl<'de, 'a, R: Read<'de> + 'a> de::VariantAccess<'de> for UnitVariantAccess<'a, R> { |
2088 | | type Error = Error; |
2089 | | |
2090 | 0 | fn unit_variant(self) -> Result<()> { |
2091 | 0 | Ok(()) |
2092 | 0 | } |
2093 | | |
2094 | 0 | fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value> |
2095 | 0 | where |
2096 | 0 | T: de::DeserializeSeed<'de>, |
2097 | 0 | { |
2098 | 0 | Err(de::Error::invalid_type( |
2099 | 0 | Unexpected::UnitVariant, |
2100 | 0 | &"newtype variant", |
2101 | 0 | )) |
2102 | 0 | } |
2103 | | |
2104 | 0 | fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value> |
2105 | 0 | where |
2106 | 0 | V: de::Visitor<'de>, |
2107 | 0 | { |
2108 | 0 | Err(de::Error::invalid_type( |
2109 | 0 | Unexpected::UnitVariant, |
2110 | 0 | &"tuple variant", |
2111 | 0 | )) |
2112 | 0 | } |
2113 | | |
2114 | 0 | fn struct_variant<V>(self, _fields: &'static [&'static str], _visitor: V) -> Result<V::Value> |
2115 | 0 | where |
2116 | 0 | V: de::Visitor<'de>, |
2117 | 0 | { |
2118 | 0 | Err(de::Error::invalid_type( |
2119 | 0 | Unexpected::UnitVariant, |
2120 | 0 | &"struct variant", |
2121 | 0 | )) |
2122 | 0 | } |
2123 | | } |
2124 | | |
2125 | | /// Only deserialize from this after peeking a '"' byte! Otherwise it may |
2126 | | /// deserialize invalid JSON successfully. |
2127 | | struct MapKey<'a, R: 'a> { |
2128 | | de: &'a mut Deserializer<R>, |
2129 | | } |
2130 | | |
2131 | | macro_rules! deserialize_numeric_key { |
2132 | | ($method:ident) => { |
2133 | 0 | fn $method<V>(self, visitor: V) -> Result<V::Value> |
2134 | 0 | where |
2135 | 0 | V: de::Visitor<'de>, |
2136 | 0 | { |
2137 | 0 | self.deserialize_number(visitor) |
2138 | 0 | } Unexecuted instantiation: <serde_json::de::MapKey<_> as serde::de::Deserializer>::deserialize_i8::<_> Unexecuted instantiation: <serde_json::de::MapKey<_> as serde::de::Deserializer>::deserialize_i16::<_> Unexecuted instantiation: <serde_json::de::MapKey<_> as serde::de::Deserializer>::deserialize_i32::<_> Unexecuted instantiation: <serde_json::de::MapKey<_> as serde::de::Deserializer>::deserialize_i64::<_> Unexecuted instantiation: <serde_json::de::MapKey<_> as serde::de::Deserializer>::deserialize_u8::<_> Unexecuted instantiation: <serde_json::de::MapKey<_> as serde::de::Deserializer>::deserialize_u16::<_> Unexecuted instantiation: <serde_json::de::MapKey<_> as serde::de::Deserializer>::deserialize_u32::<_> Unexecuted instantiation: <serde_json::de::MapKey<_> as serde::de::Deserializer>::deserialize_u64::<_> Unexecuted instantiation: <serde_json::de::MapKey<_> as serde::de::Deserializer>::deserialize_f32::<_> Unexecuted instantiation: <serde_json::de::MapKey<_> as serde::de::Deserializer>::deserialize_f64::<_> |
2139 | | }; |
2140 | | |
2141 | | ($method:ident, $delegate:ident) => { |
2142 | 0 | fn $method<V>(self, visitor: V) -> Result<V::Value> |
2143 | 0 | where |
2144 | 0 | V: de::Visitor<'de>, |
2145 | 0 | { |
2146 | 0 | self.de.eat_char(); |
2147 | | |
2148 | 0 | match tri!(self.de.peek()) { |
2149 | 0 | Some(b'0'..=b'9' | b'-') => {} |
2150 | 0 | _ => return Err(self.de.error(ErrorCode::ExpectedNumericKey)), |
2151 | | } |
2152 | | |
2153 | 0 | let value = tri!(self.de.$delegate(visitor)); |
2154 | | |
2155 | 0 | match tri!(self.de.peek()) { |
2156 | 0 | Some(b'"') => self.de.eat_char(), |
2157 | 0 | _ => return Err(self.de.peek_error(ErrorCode::ExpectedDoubleQuote)), |
2158 | | } |
2159 | | |
2160 | 0 | Ok(value) |
2161 | 0 | } Unexecuted instantiation: <serde_json::de::MapKey<_>>::deserialize_number::<_> Unexecuted instantiation: <serde_json::de::MapKey<_> as serde::de::Deserializer>::deserialize_i128::<_> Unexecuted instantiation: <serde_json::de::MapKey<_> as serde::de::Deserializer>::deserialize_u128::<_> |
2162 | | }; |
2163 | | } |
2164 | | |
2165 | | impl<'de, 'a, R> MapKey<'a, R> |
2166 | | where |
2167 | | R: Read<'de>, |
2168 | | { |
2169 | | deserialize_numeric_key!(deserialize_number, deserialize_number); |
2170 | | } |
2171 | | |
2172 | | impl<'de, 'a, R> de::Deserializer<'de> for MapKey<'a, R> |
2173 | | where |
2174 | | R: Read<'de>, |
2175 | | { |
2176 | | type Error = Error; |
2177 | | |
2178 | | #[inline] |
2179 | 105 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> |
2180 | 105 | where |
2181 | 105 | V: de::Visitor<'de>, |
2182 | 105 | { |
2183 | 105 | self.de.eat_char(); |
2184 | 105 | self.de.scratch.clear(); |
2185 | 105 | match tri!(self.de.read.parse_str(&mut self.de.scratch)) { |
2186 | 40 | Reference::Borrowed(s) => visitor.visit_borrowed_str(s), |
2187 | 0 | Reference::Copied(s) => visitor.visit_str(s), |
2188 | | } |
2189 | 105 | } <serde_json::de::MapKey<serde_json::read::StrRead> as serde::de::Deserializer>::deserialize_any::<smartstring::serde::SmartStringVisitor<smartstring::config::LazyCompact>> Line | Count | Source | 2179 | 105 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> | 2180 | 105 | where | 2181 | 105 | V: de::Visitor<'de>, | 2182 | 105 | { | 2183 | 105 | self.de.eat_char(); | 2184 | 105 | self.de.scratch.clear(); | 2185 | 105 | match tri!(self.de.read.parse_str(&mut self.de.scratch)) { | 2186 | 40 | Reference::Borrowed(s) => visitor.visit_borrowed_str(s), | 2187 | 0 | Reference::Copied(s) => visitor.visit_str(s), | 2188 | | } | 2189 | 105 | } |
Unexecuted instantiation: <serde_json::de::MapKey<serde_json::read::StrRead> as serde::de::Deserializer>::deserialize_any::<serde::de::impls::StringVisitor> Unexecuted instantiation: <serde_json::de::MapKey<serde_json::read::StrRead> as serde::de::Deserializer>::deserialize_any::<serde_json::value::de::KeyClassifier> |
2190 | | |
2191 | | deserialize_numeric_key!(deserialize_i8); |
2192 | | deserialize_numeric_key!(deserialize_i16); |
2193 | | deserialize_numeric_key!(deserialize_i32); |
2194 | | deserialize_numeric_key!(deserialize_i64); |
2195 | | deserialize_numeric_key!(deserialize_i128, deserialize_i128); |
2196 | | deserialize_numeric_key!(deserialize_u8); |
2197 | | deserialize_numeric_key!(deserialize_u16); |
2198 | | deserialize_numeric_key!(deserialize_u32); |
2199 | | deserialize_numeric_key!(deserialize_u64); |
2200 | | deserialize_numeric_key!(deserialize_u128, deserialize_u128); |
2201 | | #[cfg(not(feature = "float_roundtrip"))] |
2202 | | deserialize_numeric_key!(deserialize_f32); |
2203 | | #[cfg(feature = "float_roundtrip")] |
2204 | | deserialize_numeric_key!(deserialize_f32, deserialize_f32); |
2205 | | deserialize_numeric_key!(deserialize_f64); |
2206 | | |
2207 | 0 | fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value> |
2208 | 0 | where |
2209 | 0 | V: de::Visitor<'de>, |
2210 | 0 | { |
2211 | 0 | self.de.eat_char(); |
2212 | | |
2213 | 0 | let peek = match tri!(self.de.next_char()) { |
2214 | 0 | Some(b) => b, |
2215 | | None => { |
2216 | 0 | return Err(self.de.peek_error(ErrorCode::EofWhileParsingValue)); |
2217 | | } |
2218 | | }; |
2219 | | |
2220 | 0 | let value = match peek { |
2221 | | b't' => { |
2222 | 0 | tri!(self.de.parse_ident(b"rue\"")); |
2223 | 0 | visitor.visit_bool(true) |
2224 | | } |
2225 | | b'f' => { |
2226 | 0 | tri!(self.de.parse_ident(b"alse\"")); |
2227 | 0 | visitor.visit_bool(false) |
2228 | | } |
2229 | | _ => { |
2230 | 0 | self.de.scratch.clear(); |
2231 | 0 | let s = tri!(self.de.read.parse_str(&mut self.de.scratch)); |
2232 | 0 | Err(de::Error::invalid_type(Unexpected::Str(&s), &visitor)) |
2233 | | } |
2234 | | }; |
2235 | | |
2236 | 0 | match value { |
2237 | 0 | Ok(value) => Ok(value), |
2238 | 0 | Err(err) => Err(self.de.fix_position(err)), |
2239 | | } |
2240 | 0 | } |
2241 | | |
2242 | | #[inline] |
2243 | 0 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value> |
2244 | 0 | where |
2245 | 0 | V: de::Visitor<'de>, |
2246 | 0 | { |
2247 | 0 | // Map keys cannot be null. |
2248 | 0 | visitor.visit_some(self) |
2249 | 0 | } |
2250 | | |
2251 | | #[inline] |
2252 | 0 | fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value> |
2253 | 0 | where |
2254 | 0 | V: de::Visitor<'de>, |
2255 | 0 | { |
2256 | 0 | #[cfg(feature = "raw_value")] |
2257 | 0 | { |
2258 | 0 | if name == crate::raw::TOKEN { |
2259 | 0 | return self.de.deserialize_raw_value(visitor); |
2260 | 0 | } |
2261 | 0 | } |
2262 | 0 |
|
2263 | 0 | let _ = name; |
2264 | 0 | visitor.visit_newtype_struct(self) |
2265 | 0 | } |
2266 | | |
2267 | | #[inline] |
2268 | 0 | fn deserialize_enum<V>( |
2269 | 0 | self, |
2270 | 0 | name: &'static str, |
2271 | 0 | variants: &'static [&'static str], |
2272 | 0 | visitor: V, |
2273 | 0 | ) -> Result<V::Value> |
2274 | 0 | where |
2275 | 0 | V: de::Visitor<'de>, |
2276 | 0 | { |
2277 | 0 | self.de.deserialize_enum(name, variants, visitor) |
2278 | 0 | } |
2279 | | |
2280 | | #[inline] |
2281 | 0 | fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value> |
2282 | 0 | where |
2283 | 0 | V: de::Visitor<'de>, |
2284 | 0 | { |
2285 | 0 | self.de.deserialize_bytes(visitor) |
2286 | 0 | } |
2287 | | |
2288 | | #[inline] |
2289 | 0 | fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value> |
2290 | 0 | where |
2291 | 0 | V: de::Visitor<'de>, |
2292 | 0 | { |
2293 | 0 | self.de.deserialize_bytes(visitor) |
2294 | 0 | } |
2295 | | |
2296 | | forward_to_deserialize_any! { |
2297 | | char str string unit unit_struct seq tuple tuple_struct map struct |
2298 | | identifier ignored_any |
2299 | | } |
2300 | | } |
2301 | | |
2302 | | ////////////////////////////////////////////////////////////////////////////// |
2303 | | |
2304 | | /// Iterator that deserializes a stream into multiple JSON values. |
2305 | | /// |
2306 | | /// A stream deserializer can be created from any JSON deserializer using the |
2307 | | /// `Deserializer::into_iter` method. |
2308 | | /// |
2309 | | /// The data can consist of any JSON value. Values need to be a self-delineating value e.g. |
2310 | | /// arrays, objects, or strings, or be followed by whitespace or a self-delineating value. |
2311 | | /// |
2312 | | /// ``` |
2313 | | /// use serde_json::{Deserializer, Value}; |
2314 | | /// |
2315 | | /// fn main() { |
2316 | | /// let data = "{\"k\": 3}1\"cool\"\"stuff\" 3{} [0, 1, 2]"; |
2317 | | /// |
2318 | | /// let stream = Deserializer::from_str(data).into_iter::<Value>(); |
2319 | | /// |
2320 | | /// for value in stream { |
2321 | | /// println!("{}", value.unwrap()); |
2322 | | /// } |
2323 | | /// } |
2324 | | /// ``` |
2325 | | pub struct StreamDeserializer<'de, R, T> { |
2326 | | de: Deserializer<R>, |
2327 | | offset: usize, |
2328 | | failed: bool, |
2329 | | output: PhantomData<T>, |
2330 | | lifetime: PhantomData<&'de ()>, |
2331 | | } |
2332 | | |
2333 | | impl<'de, R, T> StreamDeserializer<'de, R, T> |
2334 | | where |
2335 | | R: read::Read<'de>, |
2336 | | T: de::Deserialize<'de>, |
2337 | | { |
2338 | | /// Create a JSON stream deserializer from one of the possible serde_json |
2339 | | /// input sources. |
2340 | | /// |
2341 | | /// Typically it is more convenient to use one of these methods instead: |
2342 | | /// |
2343 | | /// - Deserializer::from_str(...).into_iter() |
2344 | | /// - Deserializer::from_slice(...).into_iter() |
2345 | | /// - Deserializer::from_reader(...).into_iter() |
2346 | 0 | pub fn new(read: R) -> Self { |
2347 | 0 | let offset = read.byte_offset(); |
2348 | 0 | StreamDeserializer { |
2349 | 0 | de: Deserializer::new(read), |
2350 | 0 | offset, |
2351 | 0 | failed: false, |
2352 | 0 | output: PhantomData, |
2353 | 0 | lifetime: PhantomData, |
2354 | 0 | } |
2355 | 0 | } |
2356 | | |
2357 | | /// Returns the number of bytes so far deserialized into a successful `T`. |
2358 | | /// |
2359 | | /// If a stream deserializer returns an EOF error, new data can be joined to |
2360 | | /// `old_data[stream.byte_offset()..]` to try again. |
2361 | | /// |
2362 | | /// ``` |
2363 | | /// let data = b"[0] [1] ["; |
2364 | | /// |
2365 | | /// let de = serde_json::Deserializer::from_slice(data); |
2366 | | /// let mut stream = de.into_iter::<Vec<i32>>(); |
2367 | | /// assert_eq!(0, stream.byte_offset()); |
2368 | | /// |
2369 | | /// println!("{:?}", stream.next()); // [0] |
2370 | | /// assert_eq!(3, stream.byte_offset()); |
2371 | | /// |
2372 | | /// println!("{:?}", stream.next()); // [1] |
2373 | | /// assert_eq!(7, stream.byte_offset()); |
2374 | | /// |
2375 | | /// println!("{:?}", stream.next()); // error |
2376 | | /// assert_eq!(8, stream.byte_offset()); |
2377 | | /// |
2378 | | /// // If err.is_eof(), can join the remaining data to new data and continue. |
2379 | | /// let remaining = &data[stream.byte_offset()..]; |
2380 | | /// ``` |
2381 | | /// |
2382 | | /// *Note:* In the future this method may be changed to return the number of |
2383 | | /// bytes so far deserialized into a successful T *or* syntactically valid |
2384 | | /// JSON skipped over due to a type error. See [serde-rs/json#70] for an |
2385 | | /// example illustrating this. |
2386 | | /// |
2387 | | /// [serde-rs/json#70]: https://github.com/serde-rs/json/issues/70 |
2388 | 0 | pub fn byte_offset(&self) -> usize { |
2389 | 0 | self.offset |
2390 | 0 | } |
2391 | | |
2392 | 0 | fn peek_end_of_value(&mut self) -> Result<()> { |
2393 | 0 | match tri!(self.de.peek()) { |
2394 | | Some(b' ' | b'\n' | b'\t' | b'\r' | b'"' | b'[' | b']' | b'{' | b'}' | b',' | b':') |
2395 | 0 | | None => Ok(()), |
2396 | | Some(_) => { |
2397 | 0 | let position = self.de.read.peek_position(); |
2398 | 0 | Err(Error::syntax( |
2399 | 0 | ErrorCode::TrailingCharacters, |
2400 | 0 | position.line, |
2401 | 0 | position.column, |
2402 | 0 | )) |
2403 | | } |
2404 | | } |
2405 | 0 | } |
2406 | | } |
2407 | | |
2408 | | impl<'de, R, T> Iterator for StreamDeserializer<'de, R, T> |
2409 | | where |
2410 | | R: Read<'de>, |
2411 | | T: de::Deserialize<'de>, |
2412 | | { |
2413 | | type Item = Result<T>; |
2414 | | |
2415 | 0 | fn next(&mut self) -> Option<Result<T>> { |
2416 | 0 | if R::should_early_return_if_failed && self.failed { |
2417 | 0 | return None; |
2418 | 0 | } |
2419 | 0 |
|
2420 | 0 | // skip whitespaces, if any |
2421 | 0 | // this helps with trailing whitespaces, since whitespaces between |
2422 | 0 | // values are handled for us. |
2423 | 0 | match self.de.parse_whitespace() { |
2424 | | Ok(None) => { |
2425 | 0 | self.offset = self.de.read.byte_offset(); |
2426 | 0 | None |
2427 | | } |
2428 | 0 | Ok(Some(b)) => { |
2429 | | // If the value does not have a clear way to show the end of the value |
2430 | | // (like numbers, null, true etc.) we have to look for whitespace or |
2431 | | // the beginning of a self-delineated value. |
2432 | 0 | let self_delineated_value = match b { |
2433 | 0 | b'[' | b'"' | b'{' => true, |
2434 | 0 | _ => false, |
2435 | | }; |
2436 | 0 | self.offset = self.de.read.byte_offset(); |
2437 | 0 | let result = de::Deserialize::deserialize(&mut self.de); |
2438 | 0 |
|
2439 | 0 | Some(match result { |
2440 | 0 | Ok(value) => { |
2441 | 0 | self.offset = self.de.read.byte_offset(); |
2442 | 0 | if self_delineated_value { |
2443 | 0 | Ok(value) |
2444 | | } else { |
2445 | 0 | self.peek_end_of_value().map(|()| value) |
2446 | | } |
2447 | | } |
2448 | 0 | Err(e) => { |
2449 | 0 | self.de.read.set_failed(&mut self.failed); |
2450 | 0 | Err(e) |
2451 | | } |
2452 | | }) |
2453 | | } |
2454 | 0 | Err(e) => { |
2455 | 0 | self.de.read.set_failed(&mut self.failed); |
2456 | 0 | Some(Err(e)) |
2457 | | } |
2458 | | } |
2459 | 0 | } |
2460 | | } |
2461 | | |
2462 | | impl<'de, R, T> FusedIterator for StreamDeserializer<'de, R, T> |
2463 | | where |
2464 | | R: Read<'de> + Fused, |
2465 | | T: de::Deserialize<'de>, |
2466 | | { |
2467 | | } |
2468 | | |
2469 | | ////////////////////////////////////////////////////////////////////////////// |
2470 | | |
2471 | 16.0k | fn from_trait<'de, R, T>(read: R) -> Result<T> |
2472 | 16.0k | where |
2473 | 16.0k | R: Read<'de>, |
2474 | 16.0k | T: de::Deserialize<'de>, |
2475 | 16.0k | { |
2476 | 16.0k | let mut de = Deserializer::new(read); |
2477 | 16.0k | let value = tri!(de::Deserialize::deserialize(&mut de)); |
2478 | | |
2479 | | // Make sure the whole stream has been consumed. |
2480 | 5.37k | tri!(de.end()); |
2481 | 128 | Ok(value) |
2482 | 16.0k | } serde_json::de::from_trait::<serde_json::read::StrRead, rhai::types::dynamic::Dynamic> Line | Count | Source | 2471 | 16.0k | fn from_trait<'de, R, T>(read: R) -> Result<T> | 2472 | 16.0k | where | 2473 | 16.0k | R: Read<'de>, | 2474 | 16.0k | T: de::Deserialize<'de>, | 2475 | 16.0k | { | 2476 | 16.0k | let mut de = Deserializer::new(read); | 2477 | 16.0k | let value = tri!(de::Deserialize::deserialize(&mut de)); | 2478 | | | 2479 | | // Make sure the whole stream has been consumed. | 2480 | 5.37k | tri!(de.end()); | 2481 | 128 | Ok(value) | 2482 | 16.0k | } |
Unexecuted instantiation: serde_json::de::from_trait::<serde_json::read::StrRead, serde_json::value::Value> |
2483 | | |
2484 | | /// Deserialize an instance of type `T` from an I/O stream of JSON. |
2485 | | /// |
2486 | | /// The content of the I/O stream is deserialized directly from the stream |
2487 | | /// without being buffered in memory by serde_json. |
2488 | | /// |
2489 | | /// When reading from a source against which short reads are not efficient, such |
2490 | | /// as a [`File`], you will want to apply your own buffering because serde_json |
2491 | | /// will not buffer the input. See [`std::io::BufReader`]. |
2492 | | /// |
2493 | | /// It is expected that the input stream ends after the deserialized object. |
2494 | | /// If the stream does not end, such as in the case of a persistent socket connection, |
2495 | | /// this function will not return. It is possible instead to deserialize from a prefix of an input |
2496 | | /// stream without looking for EOF by managing your own [`Deserializer`]. |
2497 | | /// |
2498 | | /// Note that counter to intuition, this function is usually slower than |
2499 | | /// reading a file completely into memory and then applying [`from_str`] |
2500 | | /// or [`from_slice`] on it. See [issue #160]. |
2501 | | /// |
2502 | | /// [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html |
2503 | | /// [`std::io::BufReader`]: https://doc.rust-lang.org/std/io/struct.BufReader.html |
2504 | | /// [`from_str`]: ./fn.from_str.html |
2505 | | /// [`from_slice`]: ./fn.from_slice.html |
2506 | | /// [issue #160]: https://github.com/serde-rs/json/issues/160 |
2507 | | /// |
2508 | | /// # Example |
2509 | | /// |
2510 | | /// Reading the contents of a file. |
2511 | | /// |
2512 | | /// ``` |
2513 | | /// use serde::Deserialize; |
2514 | | /// |
2515 | | /// use std::error::Error; |
2516 | | /// use std::fs::File; |
2517 | | /// use std::io::BufReader; |
2518 | | /// use std::path::Path; |
2519 | | /// |
2520 | | /// #[derive(Deserialize, Debug)] |
2521 | | /// struct User { |
2522 | | /// fingerprint: String, |
2523 | | /// location: String, |
2524 | | /// } |
2525 | | /// |
2526 | | /// fn read_user_from_file<P: AsRef<Path>>(path: P) -> Result<User, Box<dyn Error>> { |
2527 | | /// // Open the file in read-only mode with buffer. |
2528 | | /// let file = File::open(path)?; |
2529 | | /// let reader = BufReader::new(file); |
2530 | | /// |
2531 | | /// // Read the JSON contents of the file as an instance of `User`. |
2532 | | /// let u = serde_json::from_reader(reader)?; |
2533 | | /// |
2534 | | /// // Return the `User`. |
2535 | | /// Ok(u) |
2536 | | /// } |
2537 | | /// |
2538 | | /// fn main() { |
2539 | | /// # } |
2540 | | /// # fn fake_main() { |
2541 | | /// let u = read_user_from_file("test.json").unwrap(); |
2542 | | /// println!("{:#?}", u); |
2543 | | /// } |
2544 | | /// ``` |
2545 | | /// |
2546 | | /// Reading from a persistent socket connection. |
2547 | | /// |
2548 | | /// ``` |
2549 | | /// use serde::Deserialize; |
2550 | | /// |
2551 | | /// use std::error::Error; |
2552 | | /// use std::net::{TcpListener, TcpStream}; |
2553 | | /// |
2554 | | /// #[derive(Deserialize, Debug)] |
2555 | | /// struct User { |
2556 | | /// fingerprint: String, |
2557 | | /// location: String, |
2558 | | /// } |
2559 | | /// |
2560 | | /// fn read_user_from_stream(tcp_stream: TcpStream) -> Result<User, Box<dyn Error>> { |
2561 | | /// let mut de = serde_json::Deserializer::from_reader(tcp_stream); |
2562 | | /// let u = User::deserialize(&mut de)?; |
2563 | | /// |
2564 | | /// Ok(u) |
2565 | | /// } |
2566 | | /// |
2567 | | /// fn main() { |
2568 | | /// # } |
2569 | | /// # fn fake_main() { |
2570 | | /// let listener = TcpListener::bind("127.0.0.1:4000").unwrap(); |
2571 | | /// |
2572 | | /// for stream in listener.incoming() { |
2573 | | /// println!("{:#?}", read_user_from_stream(stream.unwrap())); |
2574 | | /// } |
2575 | | /// } |
2576 | | /// ``` |
2577 | | /// |
2578 | | /// # Errors |
2579 | | /// |
2580 | | /// This conversion can fail if the structure of the input does not match the |
2581 | | /// structure expected by `T`, for example if `T` is a struct type but the input |
2582 | | /// contains something other than a JSON map. It can also fail if the structure |
2583 | | /// is correct but `T`'s implementation of `Deserialize` decides that something |
2584 | | /// is wrong with the data, for example required struct fields are missing from |
2585 | | /// the JSON map or some number is too big to fit in the expected primitive |
2586 | | /// type. |
2587 | | #[cfg(feature = "std")] |
2588 | | #[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
2589 | | pub fn from_reader<R, T>(rdr: R) -> Result<T> |
2590 | | where |
2591 | | R: crate::io::Read, |
2592 | | T: de::DeserializeOwned, |
2593 | | { |
2594 | | from_trait(read::IoRead::new(rdr)) |
2595 | | } |
2596 | | |
2597 | | /// Deserialize an instance of type `T` from bytes of JSON text. |
2598 | | /// |
2599 | | /// # Example |
2600 | | /// |
2601 | | /// ``` |
2602 | | /// use serde::Deserialize; |
2603 | | /// |
2604 | | /// #[derive(Deserialize, Debug)] |
2605 | | /// struct User { |
2606 | | /// fingerprint: String, |
2607 | | /// location: String, |
2608 | | /// } |
2609 | | /// |
2610 | | /// fn main() { |
2611 | | /// // The type of `j` is `&[u8]` |
2612 | | /// let j = b" |
2613 | | /// { |
2614 | | /// \"fingerprint\": \"0xF9BA143B95FF6D82\", |
2615 | | /// \"location\": \"Menlo Park, CA\" |
2616 | | /// }"; |
2617 | | /// |
2618 | | /// let u: User = serde_json::from_slice(j).unwrap(); |
2619 | | /// println!("{:#?}", u); |
2620 | | /// } |
2621 | | /// ``` |
2622 | | /// |
2623 | | /// # Errors |
2624 | | /// |
2625 | | /// This conversion can fail if the structure of the input does not match the |
2626 | | /// structure expected by `T`, for example if `T` is a struct type but the input |
2627 | | /// contains something other than a JSON map. It can also fail if the structure |
2628 | | /// is correct but `T`'s implementation of `Deserialize` decides that something |
2629 | | /// is wrong with the data, for example required struct fields are missing from |
2630 | | /// the JSON map or some number is too big to fit in the expected primitive |
2631 | | /// type. |
2632 | 0 | pub fn from_slice<'a, T>(v: &'a [u8]) -> Result<T> |
2633 | 0 | where |
2634 | 0 | T: de::Deserialize<'a>, |
2635 | 0 | { |
2636 | 0 | from_trait(read::SliceRead::new(v)) |
2637 | 0 | } |
2638 | | |
2639 | | /// Deserialize an instance of type `T` from a string of JSON text. |
2640 | | /// |
2641 | | /// # Example |
2642 | | /// |
2643 | | /// ``` |
2644 | | /// use serde::Deserialize; |
2645 | | /// |
2646 | | /// #[derive(Deserialize, Debug)] |
2647 | | /// struct User { |
2648 | | /// fingerprint: String, |
2649 | | /// location: String, |
2650 | | /// } |
2651 | | /// |
2652 | | /// fn main() { |
2653 | | /// // The type of `j` is `&str` |
2654 | | /// let j = " |
2655 | | /// { |
2656 | | /// \"fingerprint\": \"0xF9BA143B95FF6D82\", |
2657 | | /// \"location\": \"Menlo Park, CA\" |
2658 | | /// }"; |
2659 | | /// |
2660 | | /// let u: User = serde_json::from_str(j).unwrap(); |
2661 | | /// println!("{:#?}", u); |
2662 | | /// } |
2663 | | /// ``` |
2664 | | /// |
2665 | | /// # Errors |
2666 | | /// |
2667 | | /// This conversion can fail if the structure of the input does not match the |
2668 | | /// structure expected by `T`, for example if `T` is a struct type but the input |
2669 | | /// contains something other than a JSON map. It can also fail if the structure |
2670 | | /// is correct but `T`'s implementation of `Deserialize` decides that something |
2671 | | /// is wrong with the data, for example required struct fields are missing from |
2672 | | /// the JSON map or some number is too big to fit in the expected primitive |
2673 | | /// type. |
2674 | 16.0k | pub fn from_str<'a, T>(s: &'a str) -> Result<T> |
2675 | 16.0k | where |
2676 | 16.0k | T: de::Deserialize<'a>, |
2677 | 16.0k | { |
2678 | 16.0k | from_trait(read::StrRead::new(s)) |
2679 | 16.0k | } serde_json::de::from_str::<rhai::types::dynamic::Dynamic> Line | Count | Source | 2674 | 16.0k | pub fn from_str<'a, T>(s: &'a str) -> Result<T> | 2675 | 16.0k | where | 2676 | 16.0k | T: de::Deserialize<'a>, | 2677 | 16.0k | { | 2678 | 16.0k | from_trait(read::StrRead::new(s)) | 2679 | 16.0k | } |
Unexecuted instantiation: serde_json::de::from_str::<serde_json::value::Value> |