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