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, R: Read<'de>> de::Deserializer<'de> for &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        fn has_next_element<'de, 'a, R: Read<'de> + 'a>(
1930            seq: &mut SeqAccess<'a, R>,
1931        ) -> Result<bool> {
1932            let peek = match tri!(seq.de.parse_whitespace()) {
1933                Some(b) => b,
1934                None => {
1935                    return Err(seq.de.peek_error(ErrorCode::EofWhileParsingList));
1936                }
1937            };
1938
1939            if peek == b']' {
1940                Ok(false)
1941            } else if seq.first {
1942                seq.first = false;
1943                Ok(true)
1944            } else if peek == b',' {
1945                seq.de.eat_char();
1946                match tri!(seq.de.parse_whitespace()) {
1947                    Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)),
1948                    Some(_) => Ok(true),
1949                    None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)),
1950                }
1951            } else {
1952                Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd))
1953            }
1954        }
1955
1956        if tri!(has_next_element(self)) {
1957            Ok(Some(tri!(seed.deserialize(&mut *self.de))))
1958        } else {
1959            Ok(None)
1960        }
1961    }
1962}
1963
1964struct MapAccess<'a, R: 'a> {
1965    de: &'a mut Deserializer<R>,
1966    first: bool,
1967}
1968
1969impl<'a, R: 'a> MapAccess<'a, R> {
1970    fn new(de: &'a mut Deserializer<R>) -> Self {
1971        MapAccess { de, first: true }
1972    }
1973}
1974
1975impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
1976    type Error = Error;
1977
1978    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
1979    where
1980        K: de::DeserializeSeed<'de>,
1981    {
1982        fn has_next_key<'de, 'a, R: Read<'de> + 'a>(map: &mut MapAccess<'a, R>) -> Result<bool> {
1983            let peek = match tri!(map.de.parse_whitespace()) {
1984                Some(b) => b,
1985                None => {
1986                    return Err(map.de.peek_error(ErrorCode::EofWhileParsingObject));
1987                }
1988            };
1989
1990            if peek == b'}' {
1991                Ok(false)
1992            } else if map.first {
1993                map.first = false;
1994                if peek == b'"' {
1995                    Ok(true)
1996                } else {
1997                    Err(map.de.peek_error(ErrorCode::KeyMustBeAString))
1998                }
1999            } else if peek == b',' {
2000                map.de.eat_char();
2001                match tri!(map.de.parse_whitespace()) {
2002                    Some(b'"') => Ok(true),
2003                    Some(b'}') => Err(map.de.peek_error(ErrorCode::TrailingComma)),
2004                    Some(_) => Err(map.de.peek_error(ErrorCode::KeyMustBeAString)),
2005                    None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
2006                }
2007            } else {
2008                Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd))
2009            }
2010        }
2011
2012        if tri!(has_next_key(self)) {
2013            Ok(Some(tri!(seed.deserialize(MapKey { de: &mut *self.de }))))
2014        } else {
2015            Ok(None)
2016        }
2017    }
2018
2019    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
2020    where
2021        V: de::DeserializeSeed<'de>,
2022    {
2023        tri!(self.de.parse_object_colon());
2024
2025        seed.deserialize(&mut *self.de)
2026    }
2027}
2028
2029struct VariantAccess<'a, R: 'a> {
2030    de: &'a mut Deserializer<R>,
2031}
2032
2033impl<'a, R: 'a> VariantAccess<'a, R> {
2034    fn new(de: &'a mut Deserializer<R>) -> Self {
2035        VariantAccess { de }
2036    }
2037}
2038
2039impl<'de, 'a, R: Read<'de> + 'a> de::EnumAccess<'de> for VariantAccess<'a, R> {
2040    type Error = Error;
2041    type Variant = Self;
2042
2043    fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)>
2044    where
2045        V: de::DeserializeSeed<'de>,
2046    {
2047        let val = tri!(seed.deserialize(&mut *self.de));
2048        tri!(self.de.parse_object_colon());
2049        Ok((val, self))
2050    }
2051}
2052
2053impl<'de, 'a, R: Read<'de> + 'a> de::VariantAccess<'de> for VariantAccess<'a, R> {
2054    type Error = Error;
2055
2056    fn unit_variant(self) -> Result<()> {
2057        de::Deserialize::deserialize(self.de)
2058    }
2059
2060    fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
2061    where
2062        T: de::DeserializeSeed<'de>,
2063    {
2064        seed.deserialize(self.de)
2065    }
2066
2067    fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
2068    where
2069        V: de::Visitor<'de>,
2070    {
2071        de::Deserializer::deserialize_seq(self.de, visitor)
2072    }
2073
2074    fn struct_variant<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value>
2075    where
2076        V: de::Visitor<'de>,
2077    {
2078        de::Deserializer::deserialize_struct(self.de, "", fields, visitor)
2079    }
2080}
2081
2082struct UnitVariantAccess<'a, R: 'a> {
2083    de: &'a mut Deserializer<R>,
2084}
2085
2086impl<'a, R: 'a> UnitVariantAccess<'a, R> {
2087    fn new(de: &'a mut Deserializer<R>) -> Self {
2088        UnitVariantAccess { de }
2089    }
2090}
2091
2092impl<'de, 'a, R: Read<'de> + 'a> de::EnumAccess<'de> for UnitVariantAccess<'a, R> {
2093    type Error = Error;
2094    type Variant = Self;
2095
2096    fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)>
2097    where
2098        V: de::DeserializeSeed<'de>,
2099    {
2100        let variant = tri!(seed.deserialize(&mut *self.de));
2101        Ok((variant, self))
2102    }
2103}
2104
2105impl<'de, 'a, R: Read<'de> + 'a> de::VariantAccess<'de> for UnitVariantAccess<'a, R> {
2106    type Error = Error;
2107
2108    fn unit_variant(self) -> Result<()> {
2109        Ok(())
2110    }
2111
2112    fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value>
2113    where
2114        T: de::DeserializeSeed<'de>,
2115    {
2116        Err(de::Error::invalid_type(
2117            Unexpected::UnitVariant,
2118            &"newtype variant",
2119        ))
2120    }
2121
2122    fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value>
2123    where
2124        V: de::Visitor<'de>,
2125    {
2126        Err(de::Error::invalid_type(
2127            Unexpected::UnitVariant,
2128            &"tuple variant",
2129        ))
2130    }
2131
2132    fn struct_variant<V>(self, _fields: &'static [&'static str], _visitor: V) -> Result<V::Value>
2133    where
2134        V: de::Visitor<'de>,
2135    {
2136        Err(de::Error::invalid_type(
2137            Unexpected::UnitVariant,
2138            &"struct variant",
2139        ))
2140    }
2141}
2142
2143/// Only deserialize from this after peeking a '"' byte! Otherwise it may
2144/// deserialize invalid JSON successfully.
2145struct MapKey<'a, R: 'a> {
2146    de: &'a mut Deserializer<R>,
2147}
2148
2149macro_rules! deserialize_numeric_key {
2150    ($method:ident) => {
2151        fn $method<V>(self, visitor: V) -> Result<V::Value>
2152        where
2153            V: de::Visitor<'de>,
2154        {
2155            self.deserialize_number(visitor)
2156        }
2157    };
2158
2159    ($method:ident, $delegate:ident) => {
2160        fn $method<V>(self, visitor: V) -> Result<V::Value>
2161        where
2162            V: de::Visitor<'de>,
2163        {
2164            self.de.eat_char();
2165
2166            match tri!(self.de.peek()) {
2167                Some(b'0'..=b'9' | b'-') => {}
2168                _ => return Err(self.de.error(ErrorCode::ExpectedNumericKey)),
2169            }
2170
2171            let value = tri!(self.de.$delegate(visitor));
2172
2173            match tri!(self.de.peek()) {
2174                Some(b'"') => self.de.eat_char(),
2175                _ => return Err(self.de.peek_error(ErrorCode::ExpectedDoubleQuote)),
2176            }
2177
2178            Ok(value)
2179        }
2180    };
2181}
2182
2183impl<'de, 'a, R> MapKey<'a, R>
2184where
2185    R: Read<'de>,
2186{
2187    deserialize_numeric_key!(deserialize_number, deserialize_number);
2188}
2189
2190impl<'de, 'a, R> de::Deserializer<'de> for MapKey<'a, R>
2191where
2192    R: Read<'de>,
2193{
2194    type Error = Error;
2195
2196    #[inline]
2197    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
2198    where
2199        V: de::Visitor<'de>,
2200    {
2201        self.de.eat_char();
2202        self.de.scratch.clear();
2203        match tri!(self.de.read.parse_str(&mut self.de.scratch)) {
2204            Reference::Borrowed(s) => visitor.visit_borrowed_str(s),
2205            Reference::Copied(s) => visitor.visit_str(s),
2206        }
2207    }
2208
2209    deserialize_numeric_key!(deserialize_i8);
2210    deserialize_numeric_key!(deserialize_i16);
2211    deserialize_numeric_key!(deserialize_i32);
2212    deserialize_numeric_key!(deserialize_i64);
2213    deserialize_numeric_key!(deserialize_i128, deserialize_i128);
2214    deserialize_numeric_key!(deserialize_u8);
2215    deserialize_numeric_key!(deserialize_u16);
2216    deserialize_numeric_key!(deserialize_u32);
2217    deserialize_numeric_key!(deserialize_u64);
2218    deserialize_numeric_key!(deserialize_u128, deserialize_u128);
2219    #[cfg(not(feature = "float_roundtrip"))]
2220    deserialize_numeric_key!(deserialize_f32);
2221    #[cfg(feature = "float_roundtrip")]
2222    deserialize_numeric_key!(deserialize_f32, deserialize_f32);
2223    deserialize_numeric_key!(deserialize_f64);
2224
2225    fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
2226    where
2227        V: de::Visitor<'de>,
2228    {
2229        self.de.eat_char();
2230
2231        let peek = match tri!(self.de.next_char()) {
2232            Some(b) => b,
2233            None => {
2234                return Err(self.de.peek_error(ErrorCode::EofWhileParsingValue));
2235            }
2236        };
2237
2238        let value = match peek {
2239            b't' => {
2240                tri!(self.de.parse_ident(b"rue\""));
2241                visitor.visit_bool(true)
2242            }
2243            b'f' => {
2244                tri!(self.de.parse_ident(b"alse\""));
2245                visitor.visit_bool(false)
2246            }
2247            _ => {
2248                self.de.scratch.clear();
2249                let s = tri!(self.de.read.parse_str(&mut self.de.scratch));
2250                Err(de::Error::invalid_type(Unexpected::Str(&s), &visitor))
2251            }
2252        };
2253
2254        match value {
2255            Ok(value) => Ok(value),
2256            Err(err) => Err(self.de.fix_position(err)),
2257        }
2258    }
2259
2260    #[inline]
2261    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
2262    where
2263        V: de::Visitor<'de>,
2264    {
2265        // Map keys cannot be null.
2266        visitor.visit_some(self)
2267    }
2268
2269    #[inline]
2270    fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value>
2271    where
2272        V: de::Visitor<'de>,
2273    {
2274        #[cfg(feature = "raw_value")]
2275        {
2276            if name == crate::raw::TOKEN {
2277                return self.de.deserialize_raw_value(visitor);
2278            }
2279        }
2280
2281        let _ = name;
2282        visitor.visit_newtype_struct(self)
2283    }
2284
2285    #[inline]
2286    fn deserialize_enum<V>(
2287        self,
2288        name: &'static str,
2289        variants: &'static [&'static str],
2290        visitor: V,
2291    ) -> Result<V::Value>
2292    where
2293        V: de::Visitor<'de>,
2294    {
2295        self.de.deserialize_enum(name, variants, visitor)
2296    }
2297
2298    #[inline]
2299    fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
2300    where
2301        V: de::Visitor<'de>,
2302    {
2303        self.de.deserialize_bytes(visitor)
2304    }
2305
2306    #[inline]
2307    fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
2308    where
2309        V: de::Visitor<'de>,
2310    {
2311        self.de.deserialize_bytes(visitor)
2312    }
2313
2314    forward_to_deserialize_any! {
2315        char str string unit unit_struct seq tuple tuple_struct map struct
2316        identifier ignored_any
2317    }
2318}
2319
2320//////////////////////////////////////////////////////////////////////////////
2321
2322/// Iterator that deserializes a stream into multiple JSON values.
2323///
2324/// A stream deserializer can be created from any JSON deserializer using the
2325/// `Deserializer::into_iter` method.
2326///
2327/// The data can consist of any JSON value. Values need to be a self-delineating value e.g.
2328/// arrays, objects, or strings, or be followed by whitespace or a self-delineating value.
2329///
2330/// ```
2331/// use serde_json::{Deserializer, Value};
2332///
2333/// fn main() {
2334///     let data = "{\"k\": 3}1\"cool\"\"stuff\" 3{}  [0, 1, 2]";
2335///
2336///     let stream = Deserializer::from_str(data).into_iter::<Value>();
2337///
2338///     for value in stream {
2339///         println!("{}", value.unwrap());
2340///     }
2341/// }
2342/// ```
2343pub struct StreamDeserializer<'de, R, T> {
2344    de: Deserializer<R>,
2345    offset: usize,
2346    failed: bool,
2347    output: PhantomData<T>,
2348    lifetime: PhantomData<&'de ()>,
2349}
2350
2351impl<'de, R, T> StreamDeserializer<'de, R, T>
2352where
2353    R: read::Read<'de>,
2354    T: de::Deserialize<'de>,
2355{
2356    /// Create a JSON stream deserializer from one of the possible serde_json
2357    /// input sources.
2358    ///
2359    /// Typically it is more convenient to use one of these methods instead:
2360    ///
2361    ///   - Deserializer::from_str(...).into_iter()
2362    ///   - Deserializer::from_slice(...).into_iter()
2363    ///   - Deserializer::from_reader(...).into_iter()
2364    pub fn new(read: R) -> Self {
2365        let offset = read.byte_offset();
2366        StreamDeserializer {
2367            de: Deserializer::new(read),
2368            offset,
2369            failed: false,
2370            output: PhantomData,
2371            lifetime: PhantomData,
2372        }
2373    }
2374
2375    /// Returns the number of bytes so far deserialized into a successful `T`.
2376    ///
2377    /// If a stream deserializer returns an EOF error, new data can be joined to
2378    /// `old_data[stream.byte_offset()..]` to try again.
2379    ///
2380    /// ```
2381    /// let data = b"[0] [1] [";
2382    ///
2383    /// let de = serde_json::Deserializer::from_slice(data);
2384    /// let mut stream = de.into_iter::<Vec<i32>>();
2385    /// assert_eq!(0, stream.byte_offset());
2386    ///
2387    /// println!("{:?}", stream.next()); // [0]
2388    /// assert_eq!(3, stream.byte_offset());
2389    ///
2390    /// println!("{:?}", stream.next()); // [1]
2391    /// assert_eq!(7, stream.byte_offset());
2392    ///
2393    /// println!("{:?}", stream.next()); // error
2394    /// assert_eq!(8, stream.byte_offset());
2395    ///
2396    /// // If err.is_eof(), can join the remaining data to new data and continue.
2397    /// let remaining = &data[stream.byte_offset()..];
2398    /// ```
2399    ///
2400    /// *Note:* In the future this method may be changed to return the number of
2401    /// bytes so far deserialized into a successful T *or* syntactically valid
2402    /// JSON skipped over due to a type error. See [serde-rs/json#70] for an
2403    /// example illustrating this.
2404    ///
2405    /// [serde-rs/json#70]: https://github.com/serde-rs/json/issues/70
2406    pub fn byte_offset(&self) -> usize {
2407        self.offset
2408    }
2409
2410    fn peek_end_of_value(&mut self) -> Result<()> {
2411        match tri!(self.de.peek()) {
2412            Some(b' ' | b'\n' | b'\t' | b'\r' | b'"' | b'[' | b']' | b'{' | b'}' | b',' | b':')
2413            | None => Ok(()),
2414            Some(_) => {
2415                let position = self.de.read.peek_position();
2416                Err(Error::syntax(
2417                    ErrorCode::TrailingCharacters,
2418                    position.line,
2419                    position.column,
2420                ))
2421            }
2422        }
2423    }
2424}
2425
2426impl<'de, R, T> Iterator for StreamDeserializer<'de, R, T>
2427where
2428    R: Read<'de>,
2429    T: de::Deserialize<'de>,
2430{
2431    type Item = Result<T>;
2432
2433    fn next(&mut self) -> Option<Result<T>> {
2434        if R::should_early_return_if_failed && self.failed {
2435            return None;
2436        }
2437
2438        // skip whitespaces, if any
2439        // this helps with trailing whitespaces, since whitespaces between
2440        // values are handled for us.
2441        match self.de.parse_whitespace() {
2442            Ok(None) => {
2443                self.offset = self.de.read.byte_offset();
2444                None
2445            }
2446            Ok(Some(b)) => {
2447                // If the value does not have a clear way to show the end of the value
2448                // (like numbers, null, true etc.) we have to look for whitespace or
2449                // the beginning of a self-delineated value.
2450                let self_delineated_value = match b {
2451                    b'[' | b'"' | b'{' => true,
2452                    _ => false,
2453                };
2454                self.offset = self.de.read.byte_offset();
2455                let result = de::Deserialize::deserialize(&mut self.de);
2456
2457                Some(match result {
2458                    Ok(value) => {
2459                        self.offset = self.de.read.byte_offset();
2460                        if self_delineated_value {
2461                            Ok(value)
2462                        } else {
2463                            self.peek_end_of_value().map(|()| value)
2464                        }
2465                    }
2466                    Err(e) => {
2467                        self.de.read.set_failed(&mut self.failed);
2468                        Err(e)
2469                    }
2470                })
2471            }
2472            Err(e) => {
2473                self.de.read.set_failed(&mut self.failed);
2474                Some(Err(e))
2475            }
2476        }
2477    }
2478}
2479
2480impl<'de, R, T> FusedIterator for StreamDeserializer<'de, R, T>
2481where
2482    R: Read<'de> + Fused,
2483    T: de::Deserialize<'de>,
2484{
2485}
2486
2487//////////////////////////////////////////////////////////////////////////////
2488
2489fn from_trait<'de, R, T>(read: R) -> Result<T>
2490where
2491    R: Read<'de>,
2492    T: de::Deserialize<'de>,
2493{
2494    let mut de = Deserializer::new(read);
2495    let value = tri!(de::Deserialize::deserialize(&mut de));
2496
2497    // Make sure the whole stream has been consumed.
2498    tri!(de.end());
2499    Ok(value)
2500}
2501
2502/// Deserialize an instance of type `T` from an I/O stream of JSON.
2503///
2504/// The content of the I/O stream is deserialized directly from the stream
2505/// without being buffered in memory by serde_json.
2506///
2507/// When reading from a source against which short reads are not efficient, such
2508/// as a [`File`], you will want to apply your own buffering because serde_json
2509/// will not buffer the input. See [`std::io::BufReader`].
2510///
2511/// It is expected that the input stream ends after the deserialized object.
2512/// If the stream does not end, such as in the case of a persistent socket connection,
2513/// this function will not return. It is possible instead to deserialize from a prefix of an input
2514/// stream without looking for EOF by managing your own [`Deserializer`].
2515///
2516/// Note that counter to intuition, this function is usually slower than
2517/// reading a file completely into memory and then applying [`from_str`]
2518/// or [`from_slice`] on it. See [issue #160].
2519///
2520/// [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html
2521/// [`std::io::BufReader`]: https://doc.rust-lang.org/std/io/struct.BufReader.html
2522/// [`from_str`]: ./fn.from_str.html
2523/// [`from_slice`]: ./fn.from_slice.html
2524/// [issue #160]: https://github.com/serde-rs/json/issues/160
2525///
2526/// # Example
2527///
2528/// Reading the contents of a file.
2529///
2530/// ```
2531/// use serde::Deserialize;
2532///
2533/// use std::error::Error;
2534/// use std::fs::File;
2535/// use std::io::BufReader;
2536/// use std::path::Path;
2537///
2538/// #[derive(Deserialize, Debug)]
2539/// struct User {
2540///     fingerprint: String,
2541///     location: String,
2542/// }
2543///
2544/// fn read_user_from_file<P: AsRef<Path>>(path: P) -> Result<User, Box<dyn Error>> {
2545///     // Open the file in read-only mode with buffer.
2546///     let file = File::open(path)?;
2547///     let reader = BufReader::new(file);
2548///
2549///     // Read the JSON contents of the file as an instance of `User`.
2550///     let u = serde_json::from_reader(reader)?;
2551///
2552///     // Return the `User`.
2553///     Ok(u)
2554/// }
2555///
2556/// fn main() {
2557/// # }
2558/// # fn fake_main() {
2559///     let u = read_user_from_file("test.json").unwrap();
2560///     println!("{:#?}", u);
2561/// }
2562/// ```
2563///
2564/// Reading from a persistent socket connection.
2565///
2566/// ```
2567/// use serde::Deserialize;
2568///
2569/// use std::error::Error;
2570/// use std::net::{TcpListener, TcpStream};
2571///
2572/// #[derive(Deserialize, Debug)]
2573/// struct User {
2574///     fingerprint: String,
2575///     location: String,
2576/// }
2577///
2578/// fn read_user_from_stream(tcp_stream: TcpStream) -> Result<User, Box<dyn Error>> {
2579///     let mut de = serde_json::Deserializer::from_reader(tcp_stream);
2580///     let u = User::deserialize(&mut de)?;
2581///
2582///     Ok(u)
2583/// }
2584///
2585/// fn main() {
2586/// # }
2587/// # fn fake_main() {
2588///     let listener = TcpListener::bind("127.0.0.1:4000").unwrap();
2589///
2590///     for stream in listener.incoming() {
2591///         println!("{:#?}", read_user_from_stream(stream.unwrap()));
2592///     }
2593/// }
2594/// ```
2595///
2596/// # Errors
2597///
2598/// This conversion can fail if the structure of the input does not match the
2599/// structure expected by `T`, for example if `T` is a struct type but the input
2600/// contains something other than a JSON map. It can also fail if the structure
2601/// is correct but `T`'s implementation of `Deserialize` decides that something
2602/// is wrong with the data, for example required struct fields are missing from
2603/// the JSON map or some number is too big to fit in the expected primitive
2604/// type.
2605#[cfg(feature = "std")]
2606#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
2607pub fn from_reader<R, T>(rdr: R) -> Result<T>
2608where
2609    R: crate::io::Read,
2610    T: de::DeserializeOwned,
2611{
2612    from_trait(read::IoRead::new(rdr))
2613}
2614
2615/// Deserialize an instance of type `T` from bytes of JSON text.
2616///
2617/// # Example
2618///
2619/// ```
2620/// use serde::Deserialize;
2621///
2622/// #[derive(Deserialize, Debug)]
2623/// struct User {
2624///     fingerprint: String,
2625///     location: String,
2626/// }
2627///
2628/// fn main() {
2629///     // The type of `j` is `&[u8]`
2630///     let j = b"
2631///         {
2632///             \"fingerprint\": \"0xF9BA143B95FF6D82\",
2633///             \"location\": \"Menlo Park, CA\"
2634///         }";
2635///
2636///     let u: User = serde_json::from_slice(j).unwrap();
2637///     println!("{:#?}", u);
2638/// }
2639/// ```
2640///
2641/// # Errors
2642///
2643/// This conversion can fail if the structure of the input does not match the
2644/// structure expected by `T`, for example if `T` is a struct type but the input
2645/// contains something other than a JSON map. It can also fail if the structure
2646/// is correct but `T`'s implementation of `Deserialize` decides that something
2647/// is wrong with the data, for example required struct fields are missing from
2648/// the JSON map or some number is too big to fit in the expected primitive
2649/// type.
2650pub fn from_slice<'a, T>(v: &'a [u8]) -> Result<T>
2651where
2652    T: de::Deserialize<'a>,
2653{
2654    from_trait(read::SliceRead::new(v))
2655}
2656
2657/// Deserialize an instance of type `T` from a string of JSON text.
2658///
2659/// # Example
2660///
2661/// ```
2662/// use serde::Deserialize;
2663///
2664/// #[derive(Deserialize, Debug)]
2665/// struct User {
2666///     fingerprint: String,
2667///     location: String,
2668/// }
2669///
2670/// fn main() {
2671///     // The type of `j` is `&str`
2672///     let j = "
2673///         {
2674///             \"fingerprint\": \"0xF9BA143B95FF6D82\",
2675///             \"location\": \"Menlo Park, CA\"
2676///         }";
2677///
2678///     let u: User = serde_json::from_str(j).unwrap();
2679///     println!("{:#?}", u);
2680/// }
2681/// ```
2682///
2683/// # Errors
2684///
2685/// This conversion can fail if the structure of the input does not match the
2686/// structure expected by `T`, for example if `T` is a struct type but the input
2687/// contains something other than a JSON map. It can also fail if the structure
2688/// is correct but `T`'s implementation of `Deserialize` decides that something
2689/// is wrong with the data, for example required struct fields are missing from
2690/// the JSON map or some number is too big to fit in the expected primitive
2691/// type.
2692pub fn from_str<'a, T>(s: &'a str) -> Result<T>
2693where
2694    T: de::Deserialize<'a>,
2695{
2696    from_trait(read::StrRead::new(s))
2697}