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