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