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