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