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