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