serde_json/
de.rs

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