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