Coverage Report

Created: 2026-03-11 07:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/serde_json-1.0.149/src/de.rs
Line
Count
Source
1
//! Deserialize JSON data to a Rust data structure.
2
3
use crate::error::{Error, ErrorCode, Result};
4
#[cfg(feature = "float_roundtrip")]
5
use crate::lexical;
6
use crate::number::Number;
7
use crate::read::{self, Fused, Reference};
8
use alloc::string::String;
9
use alloc::vec::Vec;
10
#[cfg(feature = "float_roundtrip")]
11
use core::iter;
12
use core::iter::FusedIterator;
13
use core::marker::PhantomData;
14
use core::result;
15
use core::str::FromStr;
16
use serde::de::{self, Expected, Unexpected};
17
use serde::forward_to_deserialize_any;
18
19
#[cfg(feature = "arbitrary_precision")]
20
use crate::number::NumberDeserializer;
21
22
pub use crate::read::{Read, SliceRead, StrRead};
23
24
#[cfg(feature = "std")]
25
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
26
pub use crate::read::IoRead;
27
28
//////////////////////////////////////////////////////////////////////////////
29
30
/// A structure that deserializes JSON into Rust values.
31
pub struct Deserializer<R> {
32
    read: R,
33
    scratch: Vec<u8>,
34
    remaining_depth: u8,
35
    #[cfg(feature = "float_roundtrip")]
36
    single_precision: bool,
37
    #[cfg(feature = "unbounded_depth")]
38
    disable_recursion_limit: bool,
39
}
40
41
impl<'de, R> Deserializer<R>
42
where
43
    R: read::Read<'de>,
44
{
45
    /// Create a JSON deserializer from one of the possible serde_json input
46
    /// sources.
47
    ///
48
    /// 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
0
    pub fn new(read: R) -> Self {
60
0
        Deserializer {
61
0
            read,
62
0
            scratch: Vec::new(),
63
0
            remaining_depth: 128,
64
0
            #[cfg(feature = "float_roundtrip")]
65
0
            single_precision: false,
66
0
            #[cfg(feature = "unbounded_depth")]
67
0
            disable_recursion_limit: false,
68
0
        }
69
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::new
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::new
70
}
71
72
#[cfg(feature = "std")]
73
impl<R> Deserializer<read::IoRead<R>>
74
where
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
0
    pub fn from_reader(reader: R) -> Self {
83
0
        Deserializer::new(read::IoRead::new(reader))
84
0
    }
85
}
86
87
impl<'a> Deserializer<read::SliceRead<'a>> {
88
    /// Creates a JSON deserializer from a `&[u8]`.
89
0
    pub fn from_slice(bytes: &'a [u8]) -> Self {
90
0
        Deserializer::new(read::SliceRead::new(bytes))
91
0
    }
92
}
93
94
impl<'a> Deserializer<read::StrRead<'a>> {
95
    /// Creates a JSON deserializer from a `&str`.
96
0
    pub fn from_str(s: &'a str) -> Self {
97
0
        Deserializer::new(read::StrRead::new(s))
98
0
    }
99
}
100
101
macro_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
109
pub(crate) enum ParserNumber {
110
    F64(f64),
111
    U64(u64),
112
    I64(i64),
113
    #[cfg(feature = "arbitrary_precision")]
114
    String(String),
115
}
116
117
impl ParserNumber {
118
0
    fn visit<'de, V>(self, visitor: V) -> Result<V::Value>
119
0
    where
120
0
        V: de::Visitor<'de>,
121
    {
122
0
        match self {
123
0
            ParserNumber::F64(x) => visitor.visit_f64(x),
124
0
            ParserNumber::U64(x) => visitor.visit_u64(x),
125
0
            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
0
    }
Unexecuted instantiation: <serde_json::de::ParserNumber>::visit::<jsonwebtoken::validation::numeric_type::NumericType>
Unexecuted instantiation: <serde_json::de::ParserNumber>::visit::<<i64 as serde_core::de::Deserialize>::deserialize::PrimitiveVisitor>
Unexecuted instantiation: <serde_json::de::ParserNumber>::visit::<serde::private::de::content::ContentVisitor>
Unexecuted instantiation: <serde_json::de::ParserNumber>::visit::<<serde_json::value::Value as serde_core::de::Deserialize>::deserialize::ValueVisitor>
130
131
0
    fn invalid_type(self, exp: &dyn Expected) -> Error {
132
0
        match self {
133
0
            ParserNumber::F64(x) => de::Error::invalid_type(Unexpected::Float(x), exp),
134
0
            ParserNumber::U64(x) => de::Error::invalid_type(Unexpected::Unsigned(x), exp),
135
0
            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
0
    }
140
}
141
142
impl<'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
0
    pub fn end(&mut self) -> Result<()> {
147
0
        match tri!(self.parse_whitespace()) {
148
0
            Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)),
149
0
            None => Ok(()),
150
        }
151
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::end
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::end
152
153
    /// Turn a JSON deserializer into an iterator over values of type T.
154
0
    pub fn into_iter<T>(self) -> StreamDeserializer<'de, R, T>
155
0
    where
156
0
        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
0
        let offset = self.read.byte_offset();
161
0
        StreamDeserializer {
162
0
            de: self,
163
0
            offset,
164
0
            failed: false,
165
0
            output: PhantomData,
166
0
            lifetime: PhantomData,
167
0
        }
168
0
    }
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
0
    pub(crate) fn peek(&mut self) -> Result<Option<u8>> {
220
0
        self.read.peek()
221
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::peek
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::peek
222
223
0
    fn peek_or_null(&mut self) -> Result<u8> {
224
0
        Ok(tri!(self.peek()).unwrap_or(b'\x00'))
225
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::peek_or_null
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::peek_or_null
226
227
0
    fn eat_char(&mut self) {
228
0
        self.read.discard();
229
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::eat_char
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::eat_char
230
231
0
    fn next_char(&mut self) -> Result<Option<u8>> {
232
0
        self.read.next()
233
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::next_char
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::next_char
234
235
0
    fn next_char_or_null(&mut self) -> Result<u8> {
236
0
        Ok(tri!(self.next_char()).unwrap_or(b'\x00'))
237
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::next_char_or_null
Unexecuted instantiation: <serde_json::de::Deserializer<_>>::next_char_or_null
238
239
    /// Error caused by a byte from next_char().
240
    #[cold]
241
0
    fn error(&self, reason: ErrorCode) -> Error {
242
0
        let position = self.read.position();
243
0
        Error::syntax(reason, position.line, position.column)
244
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::error
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::error
245
246
    /// Error caused by a byte from peek().
247
    #[cold]
248
0
    fn peek_error(&self, reason: ErrorCode) -> Error {
249
0
        let position = self.read.peek_position();
250
0
        Error::syntax(reason, position.line, position.column)
251
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::peek_error
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::peek_error
252
253
    /// Returns the first non-whitespace byte without consuming it, or `None` if
254
    /// EOF is encountered.
255
0
    fn parse_whitespace(&mut self) -> Result<Option<u8>> {
256
        loop {
257
0
            match tri!(self.peek()) {
258
0
                Some(b' ' | b'\n' | b'\t' | b'\r') => {
259
0
                    self.eat_char();
260
0
                }
261
0
                other => {
262
0
                    return Ok(other);
263
                }
264
            }
265
        }
266
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::parse_whitespace
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::parse_whitespace
267
268
    #[cold]
269
0
    fn peek_invalid_type(&mut self, exp: &dyn Expected) -> Error {
270
0
        let err = match self.peek_or_null().unwrap_or(b'\x00') {
271
            b'n' => {
272
0
                self.eat_char();
273
0
                if let Err(err) = self.parse_ident(b"ull") {
274
0
                    return err;
275
0
                }
276
0
                de::Error::invalid_type(Unexpected::Unit, exp)
277
            }
278
            b't' => {
279
0
                self.eat_char();
280
0
                if let Err(err) = self.parse_ident(b"rue") {
281
0
                    return err;
282
0
                }
283
0
                de::Error::invalid_type(Unexpected::Bool(true), exp)
284
            }
285
            b'f' => {
286
0
                self.eat_char();
287
0
                if let Err(err) = self.parse_ident(b"alse") {
288
0
                    return err;
289
0
                }
290
0
                de::Error::invalid_type(Unexpected::Bool(false), exp)
291
            }
292
            b'-' => {
293
0
                self.eat_char();
294
0
                match self.parse_any_number(false) {
295
0
                    Ok(n) => n.invalid_type(exp),
296
0
                    Err(err) => return err,
297
                }
298
            }
299
0
            b'0'..=b'9' => match self.parse_any_number(true) {
300
0
                Ok(n) => n.invalid_type(exp),
301
0
                Err(err) => return err,
302
            },
303
            b'"' => {
304
0
                self.eat_char();
305
0
                self.scratch.clear();
306
0
                match self.read.parse_str(&mut self.scratch) {
307
0
                    Ok(s) => de::Error::invalid_type(Unexpected::Str(&s), exp),
308
0
                    Err(err) => return err,
309
                }
310
            }
311
0
            b'[' => de::Error::invalid_type(Unexpected::Seq, exp),
312
0
            b'{' => de::Error::invalid_type(Unexpected::Map, exp),
313
0
            _ => self.peek_error(ErrorCode::ExpectedSomeValue),
314
        };
315
316
0
        self.fix_position(err)
317
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::peek_invalid_type
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::peek_invalid_type
318
319
0
    pub(crate) fn deserialize_number<'any, V>(&mut self, visitor: V) -> Result<V::Value>
320
0
    where
321
0
        V: de::Visitor<'any>,
322
    {
323
0
        let peek = match tri!(self.parse_whitespace()) {
324
0
            Some(b) => b,
325
            None => {
326
0
                return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
327
            }
328
        };
329
330
0
        let value = match peek {
331
            b'-' => {
332
0
                self.eat_char();
333
0
                tri!(self.parse_integer(false)).visit(visitor)
334
            }
335
0
            b'0'..=b'9' => tri!(self.parse_integer(true)).visit(visitor),
336
0
            _ => Err(self.peek_invalid_type(&visitor)),
337
        };
338
339
0
        match value {
340
0
            Ok(value) => Ok(value),
341
0
            Err(err) => Err(self.fix_position(err)),
342
        }
343
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::deserialize_number::<<i64 as serde_core::de::Deserialize>::deserialize::PrimitiveVisitor>
Unexecuted instantiation: <serde_json::de::Deserializer<_>>::deserialize_number::<_>
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
0
    pub(crate) fn do_deserialize_i128<'any, V>(&mut self, visitor: V) -> Result<V::Value>
357
0
    where
358
0
        V: de::Visitor<'any>,
359
    {
360
0
        let mut buf = String::new();
361
362
0
        match tri!(self.parse_whitespace()) {
363
0
            Some(b'-') => {
364
0
                self.eat_char();
365
0
                buf.push('-');
366
0
            }
367
0
            Some(_) => {}
368
            None => {
369
0
                return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
370
            }
371
        }
372
373
0
        tri!(self.scan_integer128(&mut buf));
374
375
0
        let value = match buf.parse() {
376
0
            Ok(int) => visitor.visit_i128(int),
377
            Err(_) => {
378
0
                return Err(self.error(ErrorCode::NumberOutOfRange));
379
            }
380
        };
381
382
0
        match value {
383
0
            Ok(value) => Ok(value),
384
0
            Err(err) => Err(self.fix_position(err)),
385
        }
386
0
    }
387
388
0
    pub(crate) fn do_deserialize_u128<'any, V>(&mut self, visitor: V) -> Result<V::Value>
389
0
    where
390
0
        V: de::Visitor<'any>,
391
    {
392
0
        match tri!(self.parse_whitespace()) {
393
            Some(b'-') => {
394
0
                return Err(self.peek_error(ErrorCode::NumberOutOfRange));
395
            }
396
0
            Some(_) => {}
397
            None => {
398
0
                return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
399
            }
400
        }
401
402
0
        let mut buf = String::new();
403
0
        tri!(self.scan_integer128(&mut buf));
404
405
0
        let value = match buf.parse() {
406
0
            Ok(int) => visitor.visit_u128(int),
407
            Err(_) => {
408
0
                return Err(self.error(ErrorCode::NumberOutOfRange));
409
            }
410
        };
411
412
0
        match value {
413
0
            Ok(value) => Ok(value),
414
0
            Err(err) => Err(self.fix_position(err)),
415
        }
416
0
    }
417
418
0
    fn scan_integer128(&mut self, buf: &mut String) -> Result<()> {
419
0
        match tri!(self.next_char_or_null()) {
420
            b'0' => {
421
0
                buf.push('0');
422
                // There can be only one leading '0'.
423
0
                match tri!(self.peek_or_null()) {
424
0
                    b'0'..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)),
425
0
                    _ => Ok(()),
426
                }
427
            }
428
0
            c @ b'1'..=b'9' => {
429
0
                buf.push(c as char);
430
0
                while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
431
0
                    self.eat_char();
432
0
                    buf.push(c as char);
433
0
                }
434
0
                Ok(())
435
            }
436
0
            _ => Err(self.error(ErrorCode::InvalidNumber)),
437
        }
438
0
    }
439
440
    #[cold]
441
0
    fn fix_position(&self, err: Error) -> Error {
442
0
        err.fix_position(move |code| self.error(code))
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::fix_position::{closure#0}
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::fix_position::{closure#0}
443
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::fix_position
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::fix_position
444
445
0
    fn parse_ident(&mut self, ident: &[u8]) -> Result<()> {
446
0
        for expected in ident {
447
0
            match tri!(self.next_char()) {
448
                None => {
449
0
                    return Err(self.error(ErrorCode::EofWhileParsingValue));
450
                }
451
0
                Some(next) => {
452
0
                    if next != *expected {
453
0
                        return Err(self.error(ErrorCode::ExpectedSomeIdent));
454
0
                    }
455
                }
456
            }
457
        }
458
459
0
        Ok(())
460
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::parse_ident
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::parse_ident
461
462
0
    fn parse_integer(&mut self, positive: bool) -> Result<ParserNumber> {
463
0
        let next = match tri!(self.next_char()) {
464
0
            Some(b) => b,
465
            None => {
466
0
                return Err(self.error(ErrorCode::EofWhileParsingValue));
467
            }
468
        };
469
470
0
        match next {
471
            b'0' => {
472
                // There can be only one leading '0'.
473
0
                match tri!(self.peek_or_null()) {
474
0
                    b'0'..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)),
475
0
                    _ => self.parse_number(positive, 0),
476
                }
477
            }
478
0
            c @ b'1'..=b'9' => {
479
0
                let mut significand = (c - b'0') as u64;
480
481
                loop {
482
0
                    match tri!(self.peek_or_null()) {
483
0
                        c @ b'0'..=b'9' => {
484
0
                            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
0
                            if overflow!(significand * 10 + digit, u64::MAX) {
491
0
                                return Ok(ParserNumber::F64(tri!(
492
0
                                    self.parse_long_integer(positive, significand),
493
                                )));
494
0
                            }
495
496
0
                            self.eat_char();
497
0
                            significand = significand * 10 + digit;
498
                        }
499
                        _ => {
500
0
                            return self.parse_number(positive, significand);
501
                        }
502
                    }
503
                }
504
            }
505
0
            _ => Err(self.error(ErrorCode::InvalidNumber)),
506
        }
507
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::parse_integer
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::parse_integer
508
509
0
    fn parse_number(&mut self, positive: bool, significand: u64) -> Result<ParserNumber> {
510
0
        Ok(match tri!(self.peek_or_null()) {
511
0
            b'.' => ParserNumber::F64(tri!(self.parse_decimal(positive, significand, 0))),
512
0
            b'e' | b'E' => ParserNumber::F64(tri!(self.parse_exponent(positive, significand, 0))),
513
            _ => {
514
0
                if positive {
515
0
                    ParserNumber::U64(significand)
516
                } else {
517
0
                    let neg = (significand as i64).wrapping_neg();
518
519
                    // Convert into a float if we underflow, or on `-0`.
520
0
                    if neg >= 0 {
521
0
                        ParserNumber::F64(-(significand as f64))
522
                    } else {
523
0
                        ParserNumber::I64(neg)
524
                    }
525
                }
526
            }
527
        })
528
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::parse_number
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::parse_number
529
530
0
    fn parse_decimal(
531
0
        &mut self,
532
0
        positive: bool,
533
0
        mut significand: u64,
534
0
        exponent_before_decimal_point: i32,
535
0
    ) -> Result<f64> {
536
0
        self.eat_char();
537
538
0
        let mut exponent_after_decimal_point = 0;
539
0
        while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
540
0
            let digit = (c - b'0') as u64;
541
542
0
            if overflow!(significand * 10 + digit, u64::MAX) {
543
0
                let exponent = exponent_before_decimal_point + exponent_after_decimal_point;
544
0
                return self.parse_decimal_overflow(positive, significand, exponent);
545
0
            }
546
547
0
            self.eat_char();
548
0
            significand = significand * 10 + digit;
549
0
            exponent_after_decimal_point -= 1;
550
        }
551
552
        // Error if there is not at least one digit after the decimal point.
553
0
        if exponent_after_decimal_point == 0 {
554
0
            match tri!(self.peek()) {
555
0
                Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)),
556
0
                None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
557
            }
558
0
        }
559
560
0
        let exponent = exponent_before_decimal_point + exponent_after_decimal_point;
561
0
        match tri!(self.peek_or_null()) {
562
0
            b'e' | b'E' => self.parse_exponent(positive, significand, exponent),
563
0
            _ => self.f64_from_parts(positive, significand, exponent),
564
        }
565
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::parse_decimal
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::parse_decimal
566
567
0
    fn parse_exponent(
568
0
        &mut self,
569
0
        positive: bool,
570
0
        significand: u64,
571
0
        starting_exp: i32,
572
0
    ) -> Result<f64> {
573
0
        self.eat_char();
574
575
0
        let positive_exp = match tri!(self.peek_or_null()) {
576
            b'+' => {
577
0
                self.eat_char();
578
0
                true
579
            }
580
            b'-' => {
581
0
                self.eat_char();
582
0
                false
583
            }
584
0
            _ => true,
585
        };
586
587
0
        let next = match tri!(self.next_char()) {
588
0
            Some(b) => b,
589
            None => {
590
0
                return Err(self.error(ErrorCode::EofWhileParsingValue));
591
            }
592
        };
593
594
        // Make sure a digit follows the exponent place.
595
0
        let mut exp = match next {
596
0
            c @ b'0'..=b'9' => (c - b'0') as i32,
597
            _ => {
598
0
                return Err(self.error(ErrorCode::InvalidNumber));
599
            }
600
        };
601
602
0
        while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
603
0
            self.eat_char();
604
0
            let digit = (c - b'0') as i32;
605
606
0
            if overflow!(exp * 10 + digit, i32::MAX) {
607
0
                let zero_significand = significand == 0;
608
0
                return self.parse_exponent_overflow(positive, zero_significand, positive_exp);
609
0
            }
610
611
0
            exp = exp * 10 + digit;
612
        }
613
614
0
        let final_exp = if positive_exp {
615
0
            starting_exp.saturating_add(exp)
616
        } else {
617
0
            starting_exp.saturating_sub(exp)
618
        };
619
620
0
        self.f64_from_parts(positive, significand, final_exp)
621
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::parse_exponent
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::parse_exponent
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
0
    fn f64_from_parts(
640
0
        &mut self,
641
0
        positive: bool,
642
0
        significand: u64,
643
0
        mut exponent: i32,
644
0
    ) -> Result<f64> {
645
0
        let mut f = significand as f64;
646
        loop {
647
0
            match POW10.get(exponent.wrapping_abs() as usize) {
648
0
                Some(&pow) => {
649
0
                    if exponent >= 0 {
650
0
                        f *= pow;
651
0
                        if f.is_infinite() {
652
0
                            return Err(self.error(ErrorCode::NumberOutOfRange));
653
0
                        }
654
0
                    } else {
655
0
                        f /= pow;
656
0
                    }
657
0
                    break;
658
                }
659
                None => {
660
0
                    if f == 0.0 {
661
0
                        break;
662
0
                    }
663
0
                    if exponent >= 0 {
664
0
                        return Err(self.error(ErrorCode::NumberOutOfRange));
665
0
                    }
666
0
                    f /= 1e308;
667
0
                    exponent += 308;
668
                }
669
            }
670
        }
671
0
        Ok(if positive { f } else { -f })
672
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::f64_from_parts
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::f64_from_parts
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
0
    fn parse_long_integer(&mut self, positive: bool, significand: u64) -> Result<f64> {
718
0
        let mut exponent = 0;
719
        loop {
720
0
            match tri!(self.peek_or_null()) {
721
0
                b'0'..=b'9' => {
722
0
                    self.eat_char();
723
0
                    // This could overflow... if your integer is gigabytes long.
724
0
                    // Ignore that possibility.
725
0
                    exponent += 1;
726
0
                }
727
                b'.' => {
728
0
                    return self.parse_decimal(positive, significand, exponent);
729
                }
730
                b'e' | b'E' => {
731
0
                    return self.parse_exponent(positive, significand, exponent);
732
                }
733
                _ => {
734
0
                    return self.f64_from_parts(positive, significand, exponent);
735
                }
736
            }
737
        }
738
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::parse_long_integer
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::parse_long_integer
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
0
    fn parse_decimal_overflow(
838
0
        &mut self,
839
0
        positive: bool,
840
0
        significand: u64,
841
0
        exponent: i32,
842
0
    ) -> Result<f64> {
843
        // The next multiply/add would overflow, so just ignore all further
844
        // digits.
845
0
        while let b'0'..=b'9' = tri!(self.peek_or_null()) {
846
0
            self.eat_char();
847
0
        }
848
849
0
        match tri!(self.peek_or_null()) {
850
0
            b'e' | b'E' => self.parse_exponent(positive, significand, exponent),
851
0
            _ => self.f64_from_parts(positive, significand, exponent),
852
        }
853
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::parse_decimal_overflow
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::parse_decimal_overflow
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
0
    fn parse_exponent_overflow(
860
0
        &mut self,
861
0
        positive: bool,
862
0
        zero_significand: bool,
863
0
        positive_exp: bool,
864
0
    ) -> Result<f64> {
865
        // Error instead of +/- infinity.
866
0
        if !zero_significand && positive_exp {
867
0
            return Err(self.error(ErrorCode::NumberOutOfRange));
868
0
        }
869
870
0
        while let b'0'..=b'9' = tri!(self.peek_or_null()) {
871
0
            self.eat_char();
872
0
        }
873
0
        Ok(if positive { 0.0 } else { -0.0 })
874
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::parse_exponent_overflow
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::parse_exponent_overflow
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
0
    fn parse_any_signed_number(&mut self) -> Result<ParserNumber> {
900
0
        let peek = match tri!(self.peek()) {
901
0
            Some(b) => b,
902
            None => {
903
0
                return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
904
            }
905
        };
906
907
0
        let value = match peek {
908
            b'-' => {
909
0
                self.eat_char();
910
0
                self.parse_any_number(false)
911
            }
912
0
            b'0'..=b'9' => self.parse_any_number(true),
913
0
            _ => Err(self.peek_error(ErrorCode::InvalidNumber)),
914
        };
915
916
0
        let value = match tri!(self.peek()) {
917
0
            Some(_) => Err(self.peek_error(ErrorCode::InvalidNumber)),
918
0
            None => value,
919
        };
920
921
0
        match value {
922
0
            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
0
            Err(err) => Err(self.fix_position(err)),
929
        }
930
0
    }
931
932
    #[cfg(not(feature = "arbitrary_precision"))]
933
0
    fn parse_any_number(&mut self, positive: bool) -> Result<ParserNumber> {
934
0
        self.parse_integer(positive)
935
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::parse_any_number
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::parse_any_number
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
0
    fn parse_object_colon(&mut self) -> Result<()> {
1062
0
        match tri!(self.parse_whitespace()) {
1063
            Some(b':') => {
1064
0
                self.eat_char();
1065
0
                Ok(())
1066
            }
1067
0
            Some(_) => Err(self.peek_error(ErrorCode::ExpectedColon)),
1068
0
            None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
1069
        }
1070
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::parse_object_colon
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::parse_object_colon
1071
1072
0
    fn end_seq(&mut self) -> Result<()> {
1073
0
        match tri!(self.parse_whitespace()) {
1074
            Some(b']') => {
1075
0
                self.eat_char();
1076
0
                Ok(())
1077
            }
1078
            Some(b',') => {
1079
0
                self.eat_char();
1080
0
                match self.parse_whitespace() {
1081
0
                    Ok(Some(b']')) => Err(self.peek_error(ErrorCode::TrailingComma)),
1082
0
                    _ => Err(self.peek_error(ErrorCode::TrailingCharacters)),
1083
                }
1084
            }
1085
0
            Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)),
1086
0
            None => Err(self.peek_error(ErrorCode::EofWhileParsingList)),
1087
        }
1088
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::end_seq
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::end_seq
1089
1090
0
    fn end_map(&mut self) -> Result<()> {
1091
0
        match tri!(self.parse_whitespace()) {
1092
            Some(b'}') => {
1093
0
                self.eat_char();
1094
0
                Ok(())
1095
            }
1096
0
            Some(b',') => Err(self.peek_error(ErrorCode::TrailingComma)),
1097
0
            Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)),
1098
0
            None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
1099
        }
1100
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::end_map
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::StrRead>>::end_map
1101
1102
0
    fn ignore_value(&mut self) -> Result<()> {
1103
0
        self.scratch.clear();
1104
0
        let mut enclosing = None;
1105
1106
        loop {
1107
0
            let peek = match tri!(self.parse_whitespace()) {
1108
0
                Some(b) => b,
1109
                None => {
1110
0
                    return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
1111
                }
1112
            };
1113
1114
0
            let frame = match peek {
1115
                b'n' => {
1116
0
                    self.eat_char();
1117
0
                    tri!(self.parse_ident(b"ull"));
1118
0
                    None
1119
                }
1120
                b't' => {
1121
0
                    self.eat_char();
1122
0
                    tri!(self.parse_ident(b"rue"));
1123
0
                    None
1124
                }
1125
                b'f' => {
1126
0
                    self.eat_char();
1127
0
                    tri!(self.parse_ident(b"alse"));
1128
0
                    None
1129
                }
1130
                b'-' => {
1131
0
                    self.eat_char();
1132
0
                    tri!(self.ignore_integer());
1133
0
                    None
1134
                }
1135
0
                b'0'..=b'9' => {
1136
0
                    tri!(self.ignore_integer());
1137
0
                    None
1138
                }
1139
                b'"' => {
1140
0
                    self.eat_char();
1141
0
                    tri!(self.read.ignore_str());
1142
0
                    None
1143
                }
1144
0
                frame @ (b'[' | b'{') => {
1145
0
                    self.scratch.extend(enclosing.take());
1146
0
                    self.eat_char();
1147
0
                    Some(frame)
1148
                }
1149
0
                _ => return Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
1150
            };
1151
1152
0
            let (mut accept_comma, mut frame) = match frame {
1153
0
                Some(frame) => (false, frame),
1154
0
                None => match enclosing.take() {
1155
0
                    Some(frame) => (true, frame),
1156
0
                    None => match self.scratch.pop() {
1157
0
                        Some(frame) => (true, frame),
1158
0
                        None => return Ok(()),
1159
                    },
1160
                },
1161
            };
1162
1163
            loop {
1164
0
                match tri!(self.parse_whitespace()) {
1165
0
                    Some(b',') if accept_comma => {
1166
0
                        self.eat_char();
1167
0
                        break;
1168
                    }
1169
0
                    Some(b']') if frame == b'[' => {}
1170
0
                    Some(b'}') if frame == b'{' => {}
1171
                    Some(_) => {
1172
0
                        if accept_comma {
1173
0
                            return Err(self.peek_error(match frame {
1174
0
                                b'[' => ErrorCode::ExpectedListCommaOrEnd,
1175
0
                                b'{' => ErrorCode::ExpectedObjectCommaOrEnd,
1176
0
                                _ => unreachable!(),
1177
                            }));
1178
                        } else {
1179
0
                            break;
1180
                        }
1181
                    }
1182
                    None => {
1183
0
                        return Err(self.peek_error(match frame {
1184
0
                            b'[' => ErrorCode::EofWhileParsingList,
1185
0
                            b'{' => ErrorCode::EofWhileParsingObject,
1186
0
                            _ => unreachable!(),
1187
                        }));
1188
                    }
1189
                }
1190
1191
0
                self.eat_char();
1192
0
                frame = match self.scratch.pop() {
1193
0
                    Some(frame) => frame,
1194
0
                    None => return Ok(()),
1195
                };
1196
0
                accept_comma = true;
1197
            }
1198
1199
0
            if frame == b'{' {
1200
0
                match tri!(self.parse_whitespace()) {
1201
0
                    Some(b'"') => self.eat_char(),
1202
0
                    Some(_) => return Err(self.peek_error(ErrorCode::KeyMustBeAString)),
1203
0
                    None => return Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
1204
                }
1205
0
                tri!(self.read.ignore_str());
1206
0
                match tri!(self.parse_whitespace()) {
1207
0
                    Some(b':') => self.eat_char(),
1208
0
                    Some(_) => return Err(self.peek_error(ErrorCode::ExpectedColon)),
1209
0
                    None => return Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
1210
                }
1211
0
            }
1212
1213
0
            enclosing = Some(frame);
1214
        }
1215
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::ignore_value
Unexecuted instantiation: <serde_json::de::Deserializer<_>>::ignore_value
1216
1217
0
    fn ignore_integer(&mut self) -> Result<()> {
1218
0
        match tri!(self.next_char_or_null()) {
1219
            b'0' => {
1220
                // There can be only one leading '0'.
1221
0
                if let b'0'..=b'9' = tri!(self.peek_or_null()) {
1222
0
                    return Err(self.peek_error(ErrorCode::InvalidNumber));
1223
0
                }
1224
            }
1225
0
            b'1'..=b'9' => {
1226
0
                while let b'0'..=b'9' = tri!(self.peek_or_null()) {
1227
0
                    self.eat_char();
1228
0
                }
1229
            }
1230
            _ => {
1231
0
                return Err(self.error(ErrorCode::InvalidNumber));
1232
            }
1233
        }
1234
1235
0
        match tri!(self.peek_or_null()) {
1236
0
            b'.' => self.ignore_decimal(),
1237
0
            b'e' | b'E' => self.ignore_exponent(),
1238
0
            _ => Ok(()),
1239
        }
1240
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::ignore_integer
Unexecuted instantiation: <serde_json::de::Deserializer<_>>::ignore_integer
1241
1242
0
    fn ignore_decimal(&mut self) -> Result<()> {
1243
0
        self.eat_char();
1244
1245
0
        let mut at_least_one_digit = false;
1246
0
        while let b'0'..=b'9' = tri!(self.peek_or_null()) {
1247
0
            self.eat_char();
1248
0
            at_least_one_digit = true;
1249
0
        }
1250
1251
0
        if !at_least_one_digit {
1252
0
            return Err(self.peek_error(ErrorCode::InvalidNumber));
1253
0
        }
1254
1255
0
        match tri!(self.peek_or_null()) {
1256
0
            b'e' | b'E' => self.ignore_exponent(),
1257
0
            _ => Ok(()),
1258
        }
1259
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::ignore_decimal
Unexecuted instantiation: <serde_json::de::Deserializer<_>>::ignore_decimal
1260
1261
0
    fn ignore_exponent(&mut self) -> Result<()> {
1262
0
        self.eat_char();
1263
1264
0
        match tri!(self.peek_or_null()) {
1265
0
            b'+' | b'-' => self.eat_char(),
1266
0
            _ => {}
1267
        }
1268
1269
        // Make sure a digit follows the exponent place.
1270
0
        match tri!(self.next_char_or_null()) {
1271
0
            b'0'..=b'9' => {}
1272
            _ => {
1273
0
                return Err(self.error(ErrorCode::InvalidNumber));
1274
            }
1275
        }
1276
1277
0
        while let b'0'..=b'9' = tri!(self.peek_or_null()) {
1278
0
            self.eat_char();
1279
0
        }
1280
1281
0
        Ok(())
1282
0
    }
Unexecuted instantiation: <serde_json::de::Deserializer<serde_json::read::SliceRead>>::ignore_exponent
Unexecuted instantiation: <serde_json::de::Deserializer<_>>::ignore_exponent
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
1296
impl FromStr for Number {
1297
    type Err = Error;
1298
1299
0
    fn from_str(s: &str) -> result::Result<Self, Self::Err> {
1300
0
        Deserializer::from_str(s)
1301
0
            .parse_any_signed_number()
1302
0
            .map(Into::into)
1303
0
    }
1304
}
1305
1306
#[cfg(not(feature = "float_roundtrip"))]
1307
static 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
1341
macro_rules! deserialize_number {
1342
    ($method:ident) => {
1343
        deserialize_number!($method, deserialize_number);
1344
    };
1345
1346
    ($method:ident, $using:ident) => {
1347
0
        fn $method<V>(self, visitor: V) -> Result<V::Value>
1348
0
        where
1349
0
            V: de::Visitor<'de>,
1350
        {
1351
0
            self.$using(visitor)
1352
0
        }
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_i64::<<i64 as serde_core::de::Deserialize>::deserialize::PrimitiveVisitor>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_i8::<_>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_u8::<_>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_f32::<_>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_f64::<_>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_i16::<_>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_i32::<_>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_i64::<_>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_u16::<_>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_u32::<_>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_u64::<_>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_i128::<_>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_u128::<_>
1353
    };
1354
}
1355
1356
#[cfg(not(feature = "unbounded_depth"))]
1357
macro_rules! if_checking_recursion_limit {
1358
    ($($body:tt)*) => {
1359
        $($body)*
1360
    };
1361
}
1362
1363
#[cfg(feature = "unbounded_depth")]
1364
macro_rules! if_checking_recursion_limit {
1365
    ($this:ident $($body:tt)*) => {
1366
        if !$this.disable_recursion_limit {
1367
            $this $($body)*
1368
        }
1369
    };
1370
}
1371
1372
macro_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
1389
impl<'de, R: Read<'de>> de::Deserializer<'de> for &mut Deserializer<R> {
1390
    type Error = Error;
1391
1392
    #[inline]
1393
0
    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
1394
0
    where
1395
0
        V: de::Visitor<'de>,
1396
    {
1397
0
        let peek = match tri!(self.parse_whitespace()) {
1398
0
            Some(b) => b,
1399
            None => {
1400
0
                return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
1401
            }
1402
        };
1403
1404
0
        let value = match peek {
1405
            b'n' => {
1406
0
                self.eat_char();
1407
0
                tri!(self.parse_ident(b"ull"));
1408
0
                visitor.visit_unit()
1409
            }
1410
            b't' => {
1411
0
                self.eat_char();
1412
0
                tri!(self.parse_ident(b"rue"));
1413
0
                visitor.visit_bool(true)
1414
            }
1415
            b'f' => {
1416
0
                self.eat_char();
1417
0
                tri!(self.parse_ident(b"alse"));
1418
0
                visitor.visit_bool(false)
1419
            }
1420
            b'-' => {
1421
0
                self.eat_char();
1422
0
                tri!(self.parse_any_number(false)).visit(visitor)
1423
            }
1424
0
            b'0'..=b'9' => tri!(self.parse_any_number(true)).visit(visitor),
1425
            b'"' => {
1426
0
                self.eat_char();
1427
0
                self.scratch.clear();
1428
0
                match tri!(self.read.parse_str(&mut self.scratch)) {
1429
0
                    Reference::Borrowed(s) => visitor.visit_borrowed_str(s),
1430
0
                    Reference::Copied(s) => visitor.visit_str(s),
1431
                }
1432
            }
1433
            b'[' => {
1434
0
                check_recursion! {
1435
                    self.eat_char();
1436
0
                    let ret = visitor.visit_seq(SeqAccess::new(self));
1437
                }
1438
1439
0
                match (ret, self.end_seq()) {
1440
0
                    (Ok(ret), Ok(())) => Ok(ret),
1441
0
                    (Err(err), _) | (_, Err(err)) => Err(err),
1442
                }
1443
            }
1444
            b'{' => {
1445
0
                check_recursion! {
1446
                    self.eat_char();
1447
0
                    let ret = visitor.visit_map(MapAccess::new(self));
1448
                }
1449
1450
0
                match (ret, self.end_map()) {
1451
0
                    (Ok(ret), Ok(())) => Ok(ret),
1452
0
                    (Err(err), _) | (_, Err(err)) => Err(err),
1453
                }
1454
            }
1455
0
            _ => Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
1456
        };
1457
1458
0
        match value {
1459
0
            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
0
            Err(err) => Err(self.fix_position(err)),
1466
        }
1467
0
    }
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_any::<jsonwebtoken::validation::numeric_type::NumericType>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_any::<serde::private::de::content::ContentVisitor>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::StrRead> as serde_core::de::Deserializer>::deserialize_any::<<serde_json::value::Value as serde_core::de::Deserialize>::deserialize::ValueVisitor>
1468
1469
0
    fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
1470
0
    where
1471
0
        V: de::Visitor<'de>,
1472
    {
1473
0
        let peek = match tri!(self.parse_whitespace()) {
1474
0
            Some(b) => b,
1475
            None => {
1476
0
                return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
1477
            }
1478
        };
1479
1480
0
        let value = match peek {
1481
            b't' => {
1482
0
                self.eat_char();
1483
0
                tri!(self.parse_ident(b"rue"));
1484
0
                visitor.visit_bool(true)
1485
            }
1486
            b'f' => {
1487
0
                self.eat_char();
1488
0
                tri!(self.parse_ident(b"alse"));
1489
0
                visitor.visit_bool(false)
1490
            }
1491
0
            _ => Err(self.peek_invalid_type(&visitor)),
1492
        };
1493
1494
0
        match value {
1495
0
            Ok(value) => Ok(value),
1496
0
            Err(err) => Err(self.fix_position(err)),
1497
        }
1498
0
    }
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
0
    fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>
1518
0
    where
1519
0
        V: de::Visitor<'de>,
1520
    {
1521
0
        self.deserialize_str(visitor)
1522
0
    }
1523
1524
0
    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
1525
0
    where
1526
0
        V: de::Visitor<'de>,
1527
    {
1528
0
        let peek = match tri!(self.parse_whitespace()) {
1529
0
            Some(b) => b,
1530
            None => {
1531
0
                return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
1532
            }
1533
        };
1534
1535
0
        let value = match peek {
1536
            b'"' => {
1537
0
                self.eat_char();
1538
0
                self.scratch.clear();
1539
0
                match tri!(self.read.parse_str(&mut self.scratch)) {
1540
0
                    Reference::Borrowed(s) => visitor.visit_borrowed_str(s),
1541
0
                    Reference::Copied(s) => visitor.visit_str(s),
1542
                }
1543
            }
1544
0
            _ => Err(self.peek_invalid_type(&visitor)),
1545
        };
1546
1547
0
        match value {
1548
0
            Ok(value) => Ok(value),
1549
0
            Err(err) => Err(self.fix_position(err)),
1550
        }
1551
0
    }
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_str::<serde_core::de::impls::StringVisitor>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_str::<<jsonwebtoken::algorithms::Algorithm as serde_core::de::Deserialize>::deserialize::__FieldVisitor>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_str::<_>
1552
1553
0
    fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
1554
0
    where
1555
0
        V: de::Visitor<'de>,
1556
    {
1557
0
        self.deserialize_str(visitor)
1558
0
    }
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_string::<serde_core::de::impls::StringVisitor>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_string::<_>
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
0
    fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
1634
0
    where
1635
0
        V: de::Visitor<'de>,
1636
    {
1637
0
        let peek = match tri!(self.parse_whitespace()) {
1638
0
            Some(b) => b,
1639
            None => {
1640
0
                return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
1641
            }
1642
        };
1643
1644
0
        let value = match peek {
1645
            b'"' => {
1646
0
                self.eat_char();
1647
0
                self.scratch.clear();
1648
0
                match tri!(self.read.parse_str_raw(&mut self.scratch)) {
1649
0
                    Reference::Borrowed(b) => visitor.visit_borrowed_bytes(b),
1650
0
                    Reference::Copied(b) => visitor.visit_bytes(b),
1651
                }
1652
            }
1653
0
            b'[' => self.deserialize_seq(visitor),
1654
0
            _ => Err(self.peek_invalid_type(&visitor)),
1655
        };
1656
1657
0
        match value {
1658
0
            Ok(value) => Ok(value),
1659
0
            Err(err) => Err(self.fix_position(err)),
1660
        }
1661
0
    }
1662
1663
    #[inline]
1664
0
    fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
1665
0
    where
1666
0
        V: de::Visitor<'de>,
1667
    {
1668
0
        self.deserialize_bytes(visitor)
1669
0
    }
1670
1671
    /// Parses a `null` as a None, and any other values as a `Some(...)`.
1672
    #[inline]
1673
0
    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
1674
0
    where
1675
0
        V: de::Visitor<'de>,
1676
    {
1677
0
        match tri!(self.parse_whitespace()) {
1678
            Some(b'n') => {
1679
0
                self.eat_char();
1680
0
                tri!(self.parse_ident(b"ull"));
1681
0
                visitor.visit_none()
1682
            }
1683
0
            _ => visitor.visit_some(self),
1684
        }
1685
0
    }
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_option::<serde_core::de::impls::OptionVisitor<alloc::borrow::Cow<str>>>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_option::<serde_core::de::impls::OptionVisitor<jsonwebtoken::validation::Issuer>>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_option::<serde_core::de::impls::OptionVisitor<jsonwebtoken::validation::Audience>>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_option::<serde_core::de::impls::OptionVisitor<surrealdb_core::iam::token::Audience>>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_option::<serde_core::de::impls::OptionVisitor<i64>>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_option::<serde_core::de::impls::OptionVisitor<alloc::vec::Vec<alloc::string::String>>>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_option::<serde_core::de::impls::OptionVisitor<jsonwebtoken::jwk::Jwk>>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_option::<serde_core::de::impls::OptionVisitor<jsonwebtoken::header::Enc>>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_option::<serde_core::de::impls::OptionVisitor<jsonwebtoken::header::Zip>>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_option::<serde_core::de::impls::OptionVisitor<alloc::string::String>>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_option::<_>
1686
1687
0
    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
1688
0
    where
1689
0
        V: de::Visitor<'de>,
1690
    {
1691
0
        let peek = match tri!(self.parse_whitespace()) {
1692
0
            Some(b) => b,
1693
            None => {
1694
0
                return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
1695
            }
1696
        };
1697
1698
0
        let value = match peek {
1699
            b'n' => {
1700
0
                self.eat_char();
1701
0
                tri!(self.parse_ident(b"ull"));
1702
0
                visitor.visit_unit()
1703
            }
1704
0
            _ => Err(self.peek_invalid_type(&visitor)),
1705
        };
1706
1707
0
        match value {
1708
0
            Ok(value) => Ok(value),
1709
0
            Err(err) => Err(self.fix_position(err)),
1710
        }
1711
0
    }
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_unit::<serde_core::de::impls::UnitVisitor>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_unit::<_>
1712
1713
0
    fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
1714
0
    where
1715
0
        V: de::Visitor<'de>,
1716
    {
1717
0
        self.deserialize_unit(visitor)
1718
0
    }
1719
1720
    /// Parses a newtype struct as the underlying value.
1721
    #[inline]
1722
0
    fn deserialize_newtype_struct<V>(self, name: &str, visitor: V) -> Result<V::Value>
1723
0
    where
1724
0
        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
0
        let _ = name;
1734
0
        visitor.visit_newtype_struct(self)
1735
0
    }
1736
1737
0
    fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
1738
0
    where
1739
0
        V: de::Visitor<'de>,
1740
    {
1741
0
        let peek = match tri!(self.parse_whitespace()) {
1742
0
            Some(b) => b,
1743
            None => {
1744
0
                return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
1745
            }
1746
        };
1747
1748
0
        let value = match peek {
1749
            b'[' => {
1750
0
                check_recursion! {
1751
                    self.eat_char();
1752
0
                    let ret = visitor.visit_seq(SeqAccess::new(self));
1753
                }
1754
1755
0
                match (ret, self.end_seq()) {
1756
0
                    (Ok(ret), Ok(())) => Ok(ret),
1757
0
                    (Err(err), _) | (_, Err(err)) => Err(err),
1758
                }
1759
            }
1760
0
            _ => Err(self.peek_invalid_type(&visitor)),
1761
        };
1762
1763
0
        match value {
1764
0
            Ok(value) => Ok(value),
1765
0
            Err(err) => Err(self.fix_position(err)),
1766
        }
1767
0
    }
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_seq::<<alloc::vec::Vec<_> as serde_core::de::Deserialize>::deserialize::VecVisitor<alloc::string::String>>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_seq::<_>
1768
1769
0
    fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
1770
0
    where
1771
0
        V: de::Visitor<'de>,
1772
    {
1773
0
        self.deserialize_seq(visitor)
1774
0
    }
1775
1776
0
    fn deserialize_tuple_struct<V>(
1777
0
        self,
1778
0
        _name: &'static str,
1779
0
        _len: usize,
1780
0
        visitor: V,
1781
0
    ) -> Result<V::Value>
1782
0
    where
1783
0
        V: de::Visitor<'de>,
1784
    {
1785
0
        self.deserialize_seq(visitor)
1786
0
    }
1787
1788
0
    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
1789
0
    where
1790
0
        V: de::Visitor<'de>,
1791
    {
1792
0
        let peek = match tri!(self.parse_whitespace()) {
1793
0
            Some(b) => b,
1794
            None => {
1795
0
                return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
1796
            }
1797
        };
1798
1799
0
        let value = match peek {
1800
            b'{' => {
1801
0
                check_recursion! {
1802
                    self.eat_char();
1803
0
                    let ret = visitor.visit_map(MapAccess::new(self));
1804
                }
1805
1806
0
                match (ret, self.end_map()) {
1807
0
                    (Ok(ret), Ok(())) => Ok(ret),
1808
0
                    (Err(err), _) | (_, Err(err)) => Err(err),
1809
                }
1810
            }
1811
0
            _ => Err(self.peek_invalid_type(&visitor)),
1812
        };
1813
1814
0
        match value {
1815
0
            Ok(value) => Ok(value),
1816
0
            Err(err) => Err(self.fix_position(err)),
1817
        }
1818
0
    }
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_map::<<surrealdb_core::iam::token::Claims as serde_core::de::Deserialize>::deserialize::__Visitor>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_map::<<jsonwebtoken::jwk::Jwk as serde_core::de::Deserialize>::deserialize::__Visitor>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_map::<<jsonwebtoken::header::Header as serde_core::de::Deserialize>::deserialize::__Visitor>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::StrRead> as serde_core::de::Deserializer>::deserialize_map::<<serde_json::map::Map<alloc::string::String, serde_json::value::Value> as serde_core::de::Deserialize>::deserialize::Visitor>
1819
1820
0
    fn deserialize_struct<V>(
1821
0
        self,
1822
0
        _name: &'static str,
1823
0
        _fields: &'static [&'static str],
1824
0
        visitor: V,
1825
0
    ) -> Result<V::Value>
1826
0
    where
1827
0
        V: de::Visitor<'de>,
1828
    {
1829
0
        let peek = match tri!(self.parse_whitespace()) {
1830
0
            Some(b) => b,
1831
            None => {
1832
0
                return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
1833
            }
1834
        };
1835
1836
0
        let value = match peek {
1837
            b'[' => {
1838
0
                check_recursion! {
1839
                    self.eat_char();
1840
0
                    let ret = visitor.visit_seq(SeqAccess::new(self));
1841
                }
1842
1843
0
                match (ret, self.end_seq()) {
1844
0
                    (Ok(ret), Ok(())) => Ok(ret),
1845
0
                    (Err(err), _) | (_, Err(err)) => Err(err),
1846
                }
1847
            }
1848
            b'{' => {
1849
0
                check_recursion! {
1850
                    self.eat_char();
1851
0
                    let ret = visitor.visit_map(MapAccess::new(self));
1852
                }
1853
1854
0
                match (ret, self.end_map()) {
1855
0
                    (Ok(ret), Ok(())) => Ok(ret),
1856
0
                    (Err(err), _) | (_, Err(err)) => Err(err),
1857
                }
1858
            }
1859
0
            _ => Err(self.peek_invalid_type(&visitor)),
1860
        };
1861
1862
0
        match value {
1863
0
            Ok(value) => Ok(value),
1864
0
            Err(err) => Err(self.fix_position(err)),
1865
        }
1866
0
    }
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_struct::<<jsonwebtoken::validation::ClaimsForValidation as serde_core::de::Deserialize>::deserialize::__Visitor>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_struct::<_>
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
0
    fn deserialize_enum<V>(
1872
0
        self,
1873
0
        _name: &str,
1874
0
        _variants: &'static [&'static str],
1875
0
        visitor: V,
1876
0
    ) -> Result<V::Value>
1877
0
    where
1878
0
        V: de::Visitor<'de>,
1879
    {
1880
0
        match tri!(self.parse_whitespace()) {
1881
            Some(b'{') => {
1882
0
                check_recursion! {
1883
                    self.eat_char();
1884
0
                    let ret = visitor.visit_enum(VariantAccess::new(self));
1885
                }
1886
0
                let value = tri!(ret);
1887
1888
0
                match tri!(self.parse_whitespace()) {
1889
                    Some(b'}') => {
1890
0
                        self.eat_char();
1891
0
                        Ok(value)
1892
                    }
1893
0
                    Some(_) => Err(self.error(ErrorCode::ExpectedSomeValue)),
1894
0
                    None => Err(self.error(ErrorCode::EofWhileParsingObject)),
1895
                }
1896
            }
1897
0
            Some(b'"') => visitor.visit_enum(UnitVariantAccess::new(self)),
1898
0
            Some(_) => Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
1899
0
            None => Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
1900
        }
1901
0
    }
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_enum::<<jsonwebtoken::algorithms::Algorithm as serde_core::de::Deserialize>::deserialize::__Visitor>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_enum::<_>
1902
1903
0
    fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>
1904
0
    where
1905
0
        V: de::Visitor<'de>,
1906
    {
1907
0
        self.deserialize_str(visitor)
1908
0
    }
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_identifier::<<jsonwebtoken::algorithms::Algorithm as serde_core::de::Deserialize>::deserialize::__FieldVisitor>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_identifier::<_>
1909
1910
0
    fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
1911
0
    where
1912
0
        V: de::Visitor<'de>,
1913
    {
1914
0
        tri!(self.ignore_value());
1915
0
        visitor.visit_unit()
1916
0
    }
Unexecuted instantiation: <&mut serde_json::de::Deserializer<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_ignored_any::<serde_core::de::ignored_any::IgnoredAny>
Unexecuted instantiation: <&mut serde_json::de::Deserializer<_> as serde_core::de::Deserializer>::deserialize_ignored_any::<_>
1917
}
1918
1919
struct SeqAccess<'a, R: 'a> {
1920
    de: &'a mut Deserializer<R>,
1921
    first: bool,
1922
}
1923
1924
impl<'a, R: 'a> SeqAccess<'a, R> {
1925
0
    fn new(de: &'a mut Deserializer<R>) -> Self {
1926
0
        SeqAccess { de, first: true }
1927
0
    }
Unexecuted instantiation: <serde_json::de::SeqAccess<serde_json::read::SliceRead>>::new
Unexecuted instantiation: <serde_json::de::SeqAccess<serde_json::read::StrRead>>::new
1928
}
1929
1930
impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
1931
    type Error = Error;
1932
1933
0
    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
1934
0
    where
1935
0
        T: de::DeserializeSeed<'de>,
1936
    {
1937
0
        fn has_next_element<'de, 'a, R: Read<'de> + 'a>(
1938
0
            seq: &mut SeqAccess<'a, R>,
1939
0
        ) -> Result<bool> {
1940
0
            let peek = match tri!(seq.de.parse_whitespace()) {
1941
0
                Some(b) => b,
1942
                None => {
1943
0
                    return Err(seq.de.peek_error(ErrorCode::EofWhileParsingList));
1944
                }
1945
            };
1946
1947
0
            if peek == b']' {
1948
0
                Ok(false)
1949
0
            } else if seq.first {
1950
0
                seq.first = false;
1951
0
                Ok(true)
1952
0
            } else if peek == b',' {
1953
0
                seq.de.eat_char();
1954
0
                match tri!(seq.de.parse_whitespace()) {
1955
0
                    Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)),
1956
0
                    Some(_) => Ok(true),
1957
0
                    None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)),
1958
                }
1959
            } else {
1960
0
                Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd))
1961
            }
1962
0
        }
Unexecuted instantiation: <serde_json::de::SeqAccess<_> as serde_core::de::SeqAccess>::next_element_seed::has_next_element::<serde_json::read::SliceRead>
Unexecuted instantiation: <serde_json::de::SeqAccess<_> as serde_core::de::SeqAccess>::next_element_seed::has_next_element::<serde_json::read::StrRead>
1963
1964
0
        if tri!(has_next_element(self)) {
1965
0
            Ok(Some(tri!(seed.deserialize(&mut *self.de))))
1966
        } else {
1967
0
            Ok(None)
1968
        }
1969
0
    }
Unexecuted instantiation: <serde_json::de::SeqAccess<serde_json::read::SliceRead> as serde_core::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<jsonwebtoken::validation::TryParse<alloc::borrow::Cow<str>>>>
Unexecuted instantiation: <serde_json::de::SeqAccess<serde_json::read::SliceRead> as serde_core::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<jsonwebtoken::validation::TryParse<jsonwebtoken::validation::Issuer>>>
Unexecuted instantiation: <serde_json::de::SeqAccess<serde_json::read::SliceRead> as serde_core::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<jsonwebtoken::validation::TryParse<jsonwebtoken::validation::Audience>>>
Unexecuted instantiation: <serde_json::de::SeqAccess<serde_json::read::SliceRead> as serde_core::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<<<jsonwebtoken::validation::ClaimsForValidation as serde_core::de::Deserialize>::deserialize::__Visitor as serde_core::de::Visitor>::visit_seq::__DeserializeWith>>
Unexecuted instantiation: <serde_json::de::SeqAccess<serde_json::read::SliceRead> as serde_core::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<<<jsonwebtoken::validation::ClaimsForValidation as serde_core::de::Deserialize>::deserialize::__Visitor as serde_core::de::Visitor>::visit_seq::__DeserializeWith>>
Unexecuted instantiation: <serde_json::de::SeqAccess<serde_json::read::SliceRead> as serde_core::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<alloc::string::String>>
Unexecuted instantiation: <serde_json::de::SeqAccess<serde_json::read::SliceRead> as serde_core::de::SeqAccess>::next_element_seed::<serde::private::de::content::ContentVisitor>
Unexecuted instantiation: <serde_json::de::SeqAccess<serde_json::read::StrRead> as serde_core::de::SeqAccess>::next_element_seed::<core::marker::PhantomData<serde_json::value::Value>>
1970
}
1971
1972
struct MapAccess<'a, R: 'a> {
1973
    de: &'a mut Deserializer<R>,
1974
    first: bool,
1975
}
1976
1977
impl<'a, R: 'a> MapAccess<'a, R> {
1978
0
    fn new(de: &'a mut Deserializer<R>) -> Self {
1979
0
        MapAccess { de, first: true }
1980
0
    }
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead>>::new
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::StrRead>>::new
1981
}
1982
1983
impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
1984
    type Error = Error;
1985
1986
0
    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
1987
0
    where
1988
0
        K: de::DeserializeSeed<'de>,
1989
    {
1990
0
        fn has_next_key<'de, 'a, R: Read<'de> + 'a>(map: &mut MapAccess<'a, R>) -> Result<bool> {
1991
0
            let peek = match tri!(map.de.parse_whitespace()) {
1992
0
                Some(b) => b,
1993
                None => {
1994
0
                    return Err(map.de.peek_error(ErrorCode::EofWhileParsingObject));
1995
                }
1996
            };
1997
1998
0
            if peek == b'}' {
1999
0
                Ok(false)
2000
0
            } else if map.first {
2001
0
                map.first = false;
2002
0
                if peek == b'"' {
2003
0
                    Ok(true)
2004
                } else {
2005
0
                    Err(map.de.peek_error(ErrorCode::KeyMustBeAString))
2006
                }
2007
0
            } else if peek == b',' {
2008
0
                map.de.eat_char();
2009
0
                match tri!(map.de.parse_whitespace()) {
2010
0
                    Some(b'"') => Ok(true),
2011
0
                    Some(b'}') => Err(map.de.peek_error(ErrorCode::TrailingComma)),
2012
0
                    Some(_) => Err(map.de.peek_error(ErrorCode::KeyMustBeAString)),
2013
0
                    None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
2014
                }
2015
            } else {
2016
0
                Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd))
2017
            }
2018
0
        }
Unexecuted instantiation: <serde_json::de::MapAccess<_> as serde_core::de::MapAccess>::next_key_seed::has_next_key::<serde_json::read::SliceRead>
Unexecuted instantiation: <serde_json::de::MapAccess<_> as serde_core::de::MapAccess>::next_key_seed::has_next_key::<serde_json::read::StrRead>
2019
2020
0
        if tri!(has_next_key(self)) {
2021
0
            Ok(Some(tri!(seed.deserialize(MapKey { de: &mut *self.de }))))
2022
        } else {
2023
0
            Ok(None)
2024
        }
2025
0
    }
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_key_seed::<core::marker::PhantomData<<jsonwebtoken::validation::ClaimsForValidation as serde_core::de::Deserialize>::deserialize::__Field>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_key_seed::<core::marker::PhantomData<<surrealdb_core::iam::token::Claims as serde_core::de::Deserialize>::deserialize::__Field>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_key_seed::<core::marker::PhantomData<<jsonwebtoken::jwk::Jwk as serde_core::de::Deserialize>::deserialize::__Field>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_key_seed::<core::marker::PhantomData<<jsonwebtoken::header::Header as serde_core::de::Deserialize>::deserialize::__Field>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_key_seed::<serde::private::de::content::ContentVisitor>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::StrRead> as serde_core::de::MapAccess>::next_key_seed::<core::marker::PhantomData<alloc::string::String>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::StrRead> as serde_core::de::MapAccess>::next_key_seed::<serde_json::value::de::KeyClassifier>
2026
2027
0
    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
2028
0
    where
2029
0
        V: de::DeserializeSeed<'de>,
2030
    {
2031
0
        tri!(self.de.parse_object_colon());
2032
2033
0
        seed.deserialize(&mut *self.de)
2034
0
    }
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<core::option::Option<surrealdb_core::iam::token::Audience>>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<core::option::Option<i64>>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<jsonwebtoken::validation::TryParse<alloc::borrow::Cow<str>>>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<jsonwebtoken::validation::TryParse<jsonwebtoken::validation::Issuer>>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<jsonwebtoken::validation::TryParse<jsonwebtoken::validation::Audience>>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<serde_core::de::ignored_any::IgnoredAny>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<<<jsonwebtoken::validation::ClaimsForValidation as serde_core::de::Deserialize>::deserialize::__Visitor as serde_core::de::Visitor>::visit_map::__DeserializeWith>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<<<jsonwebtoken::validation::ClaimsForValidation as serde_core::de::Deserialize>::deserialize::__Visitor as serde_core::de::Visitor>::visit_map::__DeserializeWith>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<core::option::Option<alloc::vec::Vec<alloc::string::String>>>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<core::option::Option<jsonwebtoken::jwk::Jwk>>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<core::option::Option<jsonwebtoken::header::Enc>>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<core::option::Option<jsonwebtoken::header::Zip>>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<core::option::Option<alloc::string::String>>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<jsonwebtoken::algorithms::Algorithm>>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::SliceRead> as serde_core::de::MapAccess>::next_value_seed::<serde::private::de::content::ContentVisitor>
Unexecuted instantiation: <serde_json::de::MapAccess<serde_json::read::StrRead> as serde_core::de::MapAccess>::next_value_seed::<core::marker::PhantomData<serde_json::value::Value>>
2035
}
2036
2037
struct VariantAccess<'a, R: 'a> {
2038
    de: &'a mut Deserializer<R>,
2039
}
2040
2041
impl<'a, R: 'a> VariantAccess<'a, R> {
2042
0
    fn new(de: &'a mut Deserializer<R>) -> Self {
2043
0
        VariantAccess { de }
2044
0
    }
Unexecuted instantiation: <serde_json::de::VariantAccess<serde_json::read::SliceRead>>::new
Unexecuted instantiation: <serde_json::de::VariantAccess<_>>::new
2045
}
2046
2047
impl<'de, 'a, R: Read<'de> + 'a> de::EnumAccess<'de> for VariantAccess<'a, R> {
2048
    type Error = Error;
2049
    type Variant = Self;
2050
2051
0
    fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)>
2052
0
    where
2053
0
        V: de::DeserializeSeed<'de>,
2054
    {
2055
0
        let val = tri!(seed.deserialize(&mut *self.de));
2056
0
        tri!(self.de.parse_object_colon());
2057
0
        Ok((val, self))
2058
0
    }
Unexecuted instantiation: <serde_json::de::VariantAccess<serde_json::read::SliceRead> as serde_core::de::EnumAccess>::variant_seed::<core::marker::PhantomData<<jsonwebtoken::algorithms::Algorithm as serde_core::de::Deserialize>::deserialize::__Field>>
Unexecuted instantiation: <serde_json::de::VariantAccess<_> as serde_core::de::EnumAccess>::variant_seed::<_>
2059
}
2060
2061
impl<'de, 'a, R: Read<'de> + 'a> de::VariantAccess<'de> for VariantAccess<'a, R> {
2062
    type Error = Error;
2063
2064
0
    fn unit_variant(self) -> Result<()> {
2065
0
        de::Deserialize::deserialize(self.de)
2066
0
    }
Unexecuted instantiation: <serde_json::de::VariantAccess<serde_json::read::SliceRead> as serde_core::de::VariantAccess>::unit_variant
Unexecuted instantiation: <serde_json::de::VariantAccess<_> as serde_core::de::VariantAccess>::unit_variant
2067
2068
0
    fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
2069
0
    where
2070
0
        T: de::DeserializeSeed<'de>,
2071
    {
2072
0
        seed.deserialize(self.de)
2073
0
    }
2074
2075
0
    fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
2076
0
    where
2077
0
        V: de::Visitor<'de>,
2078
    {
2079
0
        de::Deserializer::deserialize_seq(self.de, visitor)
2080
0
    }
2081
2082
0
    fn struct_variant<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value>
2083
0
    where
2084
0
        V: de::Visitor<'de>,
2085
    {
2086
0
        de::Deserializer::deserialize_struct(self.de, "", fields, visitor)
2087
0
    }
2088
}
2089
2090
struct UnitVariantAccess<'a, R: 'a> {
2091
    de: &'a mut Deserializer<R>,
2092
}
2093
2094
impl<'a, R: 'a> UnitVariantAccess<'a, R> {
2095
0
    fn new(de: &'a mut Deserializer<R>) -> Self {
2096
0
        UnitVariantAccess { de }
2097
0
    }
Unexecuted instantiation: <serde_json::de::UnitVariantAccess<serde_json::read::SliceRead>>::new
Unexecuted instantiation: <serde_json::de::UnitVariantAccess<_>>::new
2098
}
2099
2100
impl<'de, 'a, R: Read<'de> + 'a> de::EnumAccess<'de> for UnitVariantAccess<'a, R> {
2101
    type Error = Error;
2102
    type Variant = Self;
2103
2104
0
    fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)>
2105
0
    where
2106
0
        V: de::DeserializeSeed<'de>,
2107
    {
2108
0
        let variant = tri!(seed.deserialize(&mut *self.de));
2109
0
        Ok((variant, self))
2110
0
    }
Unexecuted instantiation: <serde_json::de::UnitVariantAccess<serde_json::read::SliceRead> as serde_core::de::EnumAccess>::variant_seed::<core::marker::PhantomData<<jsonwebtoken::algorithms::Algorithm as serde_core::de::Deserialize>::deserialize::__Field>>
Unexecuted instantiation: <serde_json::de::UnitVariantAccess<_> as serde_core::de::EnumAccess>::variant_seed::<_>
2111
}
2112
2113
impl<'de, 'a, R: Read<'de> + 'a> de::VariantAccess<'de> for UnitVariantAccess<'a, R> {
2114
    type Error = Error;
2115
2116
0
    fn unit_variant(self) -> Result<()> {
2117
0
        Ok(())
2118
0
    }
Unexecuted instantiation: <serde_json::de::UnitVariantAccess<serde_json::read::SliceRead> as serde_core::de::VariantAccess>::unit_variant
Unexecuted instantiation: <serde_json::de::UnitVariantAccess<_> as serde_core::de::VariantAccess>::unit_variant
2119
2120
0
    fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value>
2121
0
    where
2122
0
        T: de::DeserializeSeed<'de>,
2123
    {
2124
0
        Err(de::Error::invalid_type(
2125
0
            Unexpected::UnitVariant,
2126
0
            &"newtype variant",
2127
0
        ))
2128
0
    }
2129
2130
0
    fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value>
2131
0
    where
2132
0
        V: de::Visitor<'de>,
2133
    {
2134
0
        Err(de::Error::invalid_type(
2135
0
            Unexpected::UnitVariant,
2136
0
            &"tuple variant",
2137
0
        ))
2138
0
    }
2139
2140
0
    fn struct_variant<V>(self, _fields: &'static [&'static str], _visitor: V) -> Result<V::Value>
2141
0
    where
2142
0
        V: de::Visitor<'de>,
2143
    {
2144
0
        Err(de::Error::invalid_type(
2145
0
            Unexpected::UnitVariant,
2146
0
            &"struct variant",
2147
0
        ))
2148
0
    }
2149
}
2150
2151
/// Only deserialize from this after peeking a '"' byte! Otherwise it may
2152
/// deserialize invalid JSON successfully.
2153
struct MapKey<'a, R: 'a> {
2154
    de: &'a mut Deserializer<R>,
2155
}
2156
2157
macro_rules! deserialize_numeric_key {
2158
    ($method:ident) => {
2159
0
        fn $method<V>(self, visitor: V) -> Result<V::Value>
2160
0
        where
2161
0
            V: de::Visitor<'de>,
2162
        {
2163
0
            self.deserialize_number(visitor)
2164
0
        }
Unexecuted instantiation: <serde_json::de::MapKey<_> as serde_core::de::Deserializer>::deserialize_i8::<_>
Unexecuted instantiation: <serde_json::de::MapKey<_> as serde_core::de::Deserializer>::deserialize_u8::<_>
Unexecuted instantiation: <serde_json::de::MapKey<_> as serde_core::de::Deserializer>::deserialize_f32::<_>
Unexecuted instantiation: <serde_json::de::MapKey<_> as serde_core::de::Deserializer>::deserialize_f64::<_>
Unexecuted instantiation: <serde_json::de::MapKey<_> as serde_core::de::Deserializer>::deserialize_i16::<_>
Unexecuted instantiation: <serde_json::de::MapKey<_> as serde_core::de::Deserializer>::deserialize_i32::<_>
Unexecuted instantiation: <serde_json::de::MapKey<_> as serde_core::de::Deserializer>::deserialize_i64::<_>
Unexecuted instantiation: <serde_json::de::MapKey<_> as serde_core::de::Deserializer>::deserialize_u16::<_>
Unexecuted instantiation: <serde_json::de::MapKey<_> as serde_core::de::Deserializer>::deserialize_u32::<_>
Unexecuted instantiation: <serde_json::de::MapKey<_> as serde_core::de::Deserializer>::deserialize_u64::<_>
2165
    };
2166
2167
    ($method:ident, $delegate:ident) => {
2168
0
        fn $method<V>(self, visitor: V) -> Result<V::Value>
2169
0
        where
2170
0
            V: de::Visitor<'de>,
2171
        {
2172
0
            self.de.eat_char();
2173
2174
0
            match tri!(self.de.peek()) {
2175
0
                Some(b'0'..=b'9' | b'-') => {}
2176
0
                _ => return Err(self.de.error(ErrorCode::ExpectedNumericKey)),
2177
            }
2178
2179
0
            let value = tri!(self.de.$delegate(visitor));
2180
2181
0
            match tri!(self.de.peek()) {
2182
0
                Some(b'"') => self.de.eat_char(),
2183
0
                _ => return Err(self.de.peek_error(ErrorCode::ExpectedDoubleQuote)),
2184
            }
2185
2186
0
            Ok(value)
2187
0
        }
Unexecuted instantiation: <serde_json::de::MapKey<_>>::deserialize_number::<_>
Unexecuted instantiation: <serde_json::de::MapKey<_> as serde_core::de::Deserializer>::deserialize_i128::<_>
Unexecuted instantiation: <serde_json::de::MapKey<_> as serde_core::de::Deserializer>::deserialize_u128::<_>
2188
    };
2189
}
2190
2191
impl<'de, 'a, R> MapKey<'a, R>
2192
where
2193
    R: Read<'de>,
2194
{
2195
    deserialize_numeric_key!(deserialize_number, deserialize_number);
2196
}
2197
2198
impl<'de, 'a, R> de::Deserializer<'de> for MapKey<'a, R>
2199
where
2200
    R: Read<'de>,
2201
{
2202
    type Error = Error;
2203
2204
    #[inline]
2205
0
    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
2206
0
    where
2207
0
        V: de::Visitor<'de>,
2208
    {
2209
0
        self.de.eat_char();
2210
0
        self.de.scratch.clear();
2211
0
        match tri!(self.de.read.parse_str(&mut self.de.scratch)) {
2212
0
            Reference::Borrowed(s) => visitor.visit_borrowed_str(s),
2213
0
            Reference::Copied(s) => visitor.visit_str(s),
2214
        }
2215
0
    }
Unexecuted instantiation: <serde_json::de::MapKey<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_any::<<jsonwebtoken::validation::ClaimsForValidation as serde_core::de::Deserialize>::deserialize::__FieldVisitor>
Unexecuted instantiation: <serde_json::de::MapKey<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_any::<<surrealdb_core::iam::token::Claims as serde_core::de::Deserialize>::deserialize::__FieldVisitor>
Unexecuted instantiation: <serde_json::de::MapKey<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_any::<serde::private::de::content::ContentVisitor>
Unexecuted instantiation: <serde_json::de::MapKey<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_any::<<jsonwebtoken::jwk::Jwk as serde_core::de::Deserialize>::deserialize::__FieldVisitor>
Unexecuted instantiation: <serde_json::de::MapKey<serde_json::read::SliceRead> as serde_core::de::Deserializer>::deserialize_any::<<jsonwebtoken::header::Header as serde_core::de::Deserialize>::deserialize::__FieldVisitor>
Unexecuted instantiation: <serde_json::de::MapKey<serde_json::read::StrRead> as serde_core::de::Deserializer>::deserialize_any::<serde_core::de::impls::StringVisitor>
Unexecuted instantiation: <serde_json::de::MapKey<serde_json::read::StrRead> as serde_core::de::Deserializer>::deserialize_any::<serde_json::value::de::KeyClassifier>
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
0
    fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
2234
0
    where
2235
0
        V: de::Visitor<'de>,
2236
    {
2237
0
        self.de.eat_char();
2238
2239
0
        let peek = match tri!(self.de.next_char()) {
2240
0
            Some(b) => b,
2241
            None => {
2242
0
                return Err(self.de.peek_error(ErrorCode::EofWhileParsingValue));
2243
            }
2244
        };
2245
2246
0
        let value = match peek {
2247
            b't' => {
2248
0
                tri!(self.de.parse_ident(b"rue\""));
2249
0
                visitor.visit_bool(true)
2250
            }
2251
            b'f' => {
2252
0
                tri!(self.de.parse_ident(b"alse\""));
2253
0
                visitor.visit_bool(false)
2254
            }
2255
            _ => {
2256
0
                self.de.scratch.clear();
2257
0
                let s = tri!(self.de.read.parse_str(&mut self.de.scratch));
2258
0
                Err(de::Error::invalid_type(Unexpected::Str(&s), &visitor))
2259
            }
2260
        };
2261
2262
0
        match value {
2263
0
            Ok(value) => Ok(value),
2264
0
            Err(err) => Err(self.de.fix_position(err)),
2265
        }
2266
0
    }
2267
2268
    #[inline]
2269
0
    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
2270
0
    where
2271
0
        V: de::Visitor<'de>,
2272
    {
2273
        // Map keys cannot be null.
2274
0
        visitor.visit_some(self)
2275
0
    }
2276
2277
    #[inline]
2278
0
    fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value>
2279
0
    where
2280
0
        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
0
        let _ = name;
2290
0
        visitor.visit_newtype_struct(self)
2291
0
    }
2292
2293
    #[inline]
2294
0
    fn deserialize_enum<V>(
2295
0
        self,
2296
0
        name: &'static str,
2297
0
        variants: &'static [&'static str],
2298
0
        visitor: V,
2299
0
    ) -> Result<V::Value>
2300
0
    where
2301
0
        V: de::Visitor<'de>,
2302
    {
2303
0
        self.de.deserialize_enum(name, variants, visitor)
2304
0
    }
2305
2306
    #[inline]
2307
0
    fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
2308
0
    where
2309
0
        V: de::Visitor<'de>,
2310
    {
2311
0
        self.de.deserialize_bytes(visitor)
2312
0
    }
2313
2314
    #[inline]
2315
0
    fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
2316
0
    where
2317
0
        V: de::Visitor<'de>,
2318
    {
2319
0
        self.de.deserialize_bytes(visitor)
2320
0
    }
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
/// ```
2351
pub 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
2359
impl<'de, R, T> StreamDeserializer<'de, R, T>
2360
where
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
0
    pub fn new(read: R) -> Self {
2373
0
        let offset = read.byte_offset();
2374
0
        StreamDeserializer {
2375
0
            de: Deserializer::new(read),
2376
0
            offset,
2377
0
            failed: false,
2378
0
            output: PhantomData,
2379
0
            lifetime: PhantomData,
2380
0
        }
2381
0
    }
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
0
    pub fn byte_offset(&self) -> usize {
2415
0
        self.offset
2416
0
    }
2417
2418
0
    fn peek_end_of_value(&mut self) -> Result<()> {
2419
0
        match tri!(self.de.peek()) {
2420
            Some(b' ' | b'\n' | b'\t' | b'\r' | b'"' | b'[' | b']' | b'{' | b'}' | b',' | b':')
2421
0
            | None => Ok(()),
2422
            Some(_) => {
2423
0
                let position = self.de.read.peek_position();
2424
0
                Err(Error::syntax(
2425
0
                    ErrorCode::TrailingCharacters,
2426
0
                    position.line,
2427
0
                    position.column,
2428
0
                ))
2429
            }
2430
        }
2431
0
    }
2432
}
2433
2434
impl<'de, R, T> Iterator for StreamDeserializer<'de, R, T>
2435
where
2436
    R: Read<'de>,
2437
    T: de::Deserialize<'de>,
2438
{
2439
    type Item = Result<T>;
2440
2441
0
    fn next(&mut self) -> Option<Result<T>> {
2442
0
        if R::should_early_return_if_failed && self.failed {
2443
0
            return None;
2444
0
        }
2445
2446
        // skip whitespaces, if any
2447
        // this helps with trailing whitespaces, since whitespaces between
2448
        // values are handled for us.
2449
0
        match self.de.parse_whitespace() {
2450
            Ok(None) => {
2451
0
                self.offset = self.de.read.byte_offset();
2452
0
                None
2453
            }
2454
0
            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
0
                let self_delineated_value = match b {
2459
0
                    b'[' | b'"' | b'{' => true,
2460
0
                    _ => false,
2461
                };
2462
0
                self.offset = self.de.read.byte_offset();
2463
0
                let result = de::Deserialize::deserialize(&mut self.de);
2464
2465
0
                Some(match result {
2466
0
                    Ok(value) => {
2467
0
                        self.offset = self.de.read.byte_offset();
2468
0
                        if self_delineated_value {
2469
0
                            Ok(value)
2470
                        } else {
2471
0
                            self.peek_end_of_value().map(|()| value)
2472
                        }
2473
                    }
2474
0
                    Err(e) => {
2475
0
                        self.de.read.set_failed(&mut self.failed);
2476
0
                        Err(e)
2477
                    }
2478
                })
2479
            }
2480
0
            Err(e) => {
2481
0
                self.de.read.set_failed(&mut self.failed);
2482
0
                Some(Err(e))
2483
            }
2484
        }
2485
0
    }
2486
}
2487
2488
impl<'de, R, T> FusedIterator for StreamDeserializer<'de, R, T>
2489
where
2490
    R: Read<'de> + Fused,
2491
    T: de::Deserialize<'de>,
2492
{
2493
}
2494
2495
//////////////////////////////////////////////////////////////////////////////
2496
2497
0
fn from_trait<'de, R, T>(read: R) -> Result<T>
2498
0
where
2499
0
    R: Read<'de>,
2500
0
    T: de::Deserialize<'de>,
2501
{
2502
0
    let mut de = Deserializer::new(read);
2503
0
    let value = tri!(de::Deserialize::deserialize(&mut de));
2504
2505
    // Make sure the whole stream has been consumed.
2506
0
    tri!(de.end());
2507
0
    Ok(value)
2508
0
}
Unexecuted instantiation: serde_json::de::from_trait::<serde_json::read::SliceRead, jsonwebtoken::validation::ClaimsForValidation>
Unexecuted instantiation: serde_json::de::from_trait::<serde_json::read::SliceRead, surrealdb_core::iam::token::Claims>
Unexecuted instantiation: serde_json::de::from_trait::<serde_json::read::SliceRead, jsonwebtoken::header::Header>
Unexecuted instantiation: serde_json::de::from_trait::<serde_json::read::StrRead, serde_json::map::Map<alloc::string::String, serde_json::value::Value>>
Unexecuted instantiation: serde_json::de::from_trait::<serde_json::read::StrRead, serde_json::value::Value>
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")))]
2614
0
pub fn from_reader<R, T>(rdr: R) -> Result<T>
2615
0
where
2616
0
    R: crate::io::Read,
2617
0
    T: de::DeserializeOwned,
2618
{
2619
0
    from_trait(read::IoRead::new(rdr))
2620
0
}
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.
2657
0
pub fn from_slice<'a, T>(v: &'a [u8]) -> Result<T>
2658
0
where
2659
0
    T: de::Deserialize<'a>,
2660
{
2661
0
    from_trait(read::SliceRead::new(v))
2662
0
}
Unexecuted instantiation: serde_json::de::from_slice::<jsonwebtoken::validation::ClaimsForValidation>
Unexecuted instantiation: serde_json::de::from_slice::<surrealdb_core::iam::token::Claims>
Unexecuted instantiation: serde_json::de::from_slice::<jsonwebtoken::header::Header>
Unexecuted instantiation: serde_json::de::from_slice::<_>
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.
2699
0
pub fn from_str<'a, T>(s: &'a str) -> Result<T>
2700
0
where
2701
0
    T: de::Deserialize<'a>,
2702
{
2703
0
    from_trait(read::StrRead::new(s))
2704
0
}
Unexecuted instantiation: serde_json::de::from_str::<serde_json::map::Map<alloc::string::String, serde_json::value::Value>>
Unexecuted instantiation: serde_json::de::from_str::<serde_json::value::Value>