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