1 use crate::error::{Error, ErrorCode, Result};
2 use crate::lib::ops::Deref;
3 use crate::lib::*;
4
5 #[cfg(feature = "std")]
6 use crate::io;
7 #[cfg(feature = "std")]
8 use crate::iter::LineColIterator;
9
10 #[cfg(feature = "raw_value")]
11 use crate::raw::BorrowedRawDeserializer;
12 #[cfg(all(feature = "raw_value", feature = "std"))]
13 use crate::raw::OwnedRawDeserializer;
14 #[cfg(feature = "raw_value")]
15 use serde::de::Visitor;
16
17 /// Trait used by the deserializer for iterating over input. This is manually
18 /// "specialized" for iterating over &[u8]. Once feature(specialization) is
19 /// stable we can use actual specialization.
20 ///
21 /// This trait is sealed and cannot be implemented for types outside of
22 /// `serde_json`.
23 pub trait Read<'de>: private::Sealed {
24 #[doc(hidden)]
next(&mut self) -> Result<Option<u8>>25 fn next(&mut self) -> Result<Option<u8>>;
26 #[doc(hidden)]
peek(&mut self) -> Result<Option<u8>>27 fn peek(&mut self) -> Result<Option<u8>>;
28
29 /// Only valid after a call to peek(). Discards the peeked byte.
30 #[doc(hidden)]
discard(&mut self)31 fn discard(&mut self);
32
33 /// Position of the most recent call to next().
34 ///
35 /// The most recent call was probably next() and not peek(), but this method
36 /// should try to return a sensible result if the most recent call was
37 /// actually peek() because we don't always know.
38 ///
39 /// Only called in case of an error, so performance is not important.
40 #[doc(hidden)]
position(&self) -> Position41 fn position(&self) -> Position;
42
43 /// Position of the most recent call to peek().
44 ///
45 /// The most recent call was probably peek() and not next(), but this method
46 /// should try to return a sensible result if the most recent call was
47 /// actually next() because we don't always know.
48 ///
49 /// Only called in case of an error, so performance is not important.
50 #[doc(hidden)]
peek_position(&self) -> Position51 fn peek_position(&self) -> Position;
52
53 /// Offset from the beginning of the input to the next byte that would be
54 /// returned by next() or peek().
55 #[doc(hidden)]
byte_offset(&self) -> usize56 fn byte_offset(&self) -> usize;
57
58 /// Assumes the previous byte was a quotation mark. Parses a JSON-escaped
59 /// string until the next quotation mark using the given scratch space if
60 /// necessary. The scratch space is initially empty.
61 #[doc(hidden)]
parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>>62 fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>>;
63
64 /// Assumes the previous byte was a quotation mark. Parses a JSON-escaped
65 /// string until the next quotation mark using the given scratch space if
66 /// necessary. The scratch space is initially empty.
67 ///
68 /// This function returns the raw bytes in the string with escape sequences
69 /// expanded but without performing unicode validation.
70 #[doc(hidden)]
parse_str_raw<'s>( &'s mut self, scratch: &'s mut Vec<u8>, ) -> Result<Reference<'de, 's, [u8]>>71 fn parse_str_raw<'s>(
72 &'s mut self,
73 scratch: &'s mut Vec<u8>,
74 ) -> Result<Reference<'de, 's, [u8]>>;
75
76 /// Assumes the previous byte was a quotation mark. Parses a JSON-escaped
77 /// string until the next quotation mark but discards the data.
78 #[doc(hidden)]
ignore_str(&mut self) -> Result<()>79 fn ignore_str(&mut self) -> Result<()>;
80
81 /// Assumes the previous byte was a hex escape sequnce ('\u') in a string.
82 /// Parses next hexadecimal sequence.
83 #[doc(hidden)]
decode_hex_escape(&mut self) -> Result<u16>84 fn decode_hex_escape(&mut self) -> Result<u16>;
85
86 /// Switch raw buffering mode on.
87 ///
88 /// This is used when deserializing `RawValue`.
89 #[cfg(feature = "raw_value")]
90 #[doc(hidden)]
begin_raw_buffering(&mut self)91 fn begin_raw_buffering(&mut self);
92
93 /// Switch raw buffering mode off and provides the raw buffered data to the
94 /// given visitor.
95 #[cfg(feature = "raw_value")]
96 #[doc(hidden)]
end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value> where V: Visitor<'de>97 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
98 where
99 V: Visitor<'de>;
100
101 /// Whether StreamDeserializer::next needs to check the failed flag. True
102 /// for IoRead, false for StrRead and SliceRead which can track failure by
103 /// truncating their input slice to avoid the extra check on every next
104 /// call.
105 #[doc(hidden)]
106 const should_early_return_if_failed: bool;
107
108 /// Mark a persistent failure of StreamDeserializer, either by setting the
109 /// flag or by truncating the input data.
110 #[doc(hidden)]
set_failed(&mut self, failed: &mut bool)111 fn set_failed(&mut self, failed: &mut bool);
112 }
113
114 pub struct Position {
115 pub line: usize,
116 pub column: usize,
117 }
118
119 pub enum Reference<'b, 'c, T>
120 where
121 T: ?Sized + 'static,
122 {
123 Borrowed(&'b T),
124 Copied(&'c T),
125 }
126
127 impl<'b, 'c, T> Deref for Reference<'b, 'c, T>
128 where
129 T: ?Sized + 'static,
130 {
131 type Target = T;
132
deref(&self) -> &Self::Target133 fn deref(&self) -> &Self::Target {
134 match *self {
135 Reference::Borrowed(b) => b,
136 Reference::Copied(c) => c,
137 }
138 }
139 }
140
141 /// JSON input source that reads from a std::io input stream.
142 #[cfg(feature = "std")]
143 pub struct IoRead<R>
144 where
145 R: io::Read,
146 {
147 iter: LineColIterator<io::Bytes<R>>,
148 /// Temporary storage of peeked byte.
149 ch: Option<u8>,
150 #[cfg(feature = "raw_value")]
151 raw_buffer: Option<Vec<u8>>,
152 }
153
154 /// JSON input source that reads from a slice of bytes.
155 //
156 // This is more efficient than other iterators because peek() can be read-only
157 // and we can compute line/col position only if an error happens.
158 pub struct SliceRead<'a> {
159 slice: &'a [u8],
160 /// Index of the *next* byte that will be returned by next() or peek().
161 index: usize,
162 #[cfg(feature = "raw_value")]
163 raw_buffering_start_index: usize,
164 }
165
166 /// JSON input source that reads from a UTF-8 string.
167 //
168 // Able to elide UTF-8 checks by assuming that the input is valid UTF-8.
169 pub struct StrRead<'a> {
170 delegate: SliceRead<'a>,
171 #[cfg(feature = "raw_value")]
172 data: &'a str,
173 }
174
175 // Prevent users from implementing the Read trait.
176 mod private {
177 pub trait Sealed {}
178 }
179
180 //////////////////////////////////////////////////////////////////////////////
181
182 #[cfg(feature = "std")]
183 impl<R> IoRead<R>
184 where
185 R: io::Read,
186 {
187 /// Create a JSON input source to read from a std::io input stream.
new(reader: R) -> Self188 pub fn new(reader: R) -> Self {
189 IoRead {
190 iter: LineColIterator::new(reader.bytes()),
191 ch: None,
192 #[cfg(feature = "raw_value")]
193 raw_buffer: None,
194 }
195 }
196 }
197
198 #[cfg(feature = "std")]
199 impl<R> private::Sealed for IoRead<R> where R: io::Read {}
200
201 #[cfg(feature = "std")]
202 impl<R> IoRead<R>
203 where
204 R: io::Read,
205 {
parse_str_bytes<'s, T, F>( &'s mut self, scratch: &'s mut Vec<u8>, validate: bool, result: F, ) -> Result<T> where T: 's, F: FnOnce(&'s Self, &'s [u8]) -> Result<T>,206 fn parse_str_bytes<'s, T, F>(
207 &'s mut self,
208 scratch: &'s mut Vec<u8>,
209 validate: bool,
210 result: F,
211 ) -> Result<T>
212 where
213 T: 's,
214 F: FnOnce(&'s Self, &'s [u8]) -> Result<T>,
215 {
216 loop {
217 let ch = tri!(next_or_eof(self));
218 if !ESCAPE[ch as usize] {
219 scratch.push(ch);
220 continue;
221 }
222 match ch {
223 b'"' => {
224 return result(self, scratch);
225 }
226 b'\\' => {
227 tri!(parse_escape(self, scratch));
228 }
229 _ => {
230 if validate {
231 return error(self, ErrorCode::ControlCharacterWhileParsingString);
232 }
233 scratch.push(ch);
234 }
235 }
236 }
237 }
238 }
239
240 #[cfg(feature = "std")]
241 impl<'de, R> Read<'de> for IoRead<R>
242 where
243 R: io::Read,
244 {
245 #[inline]
next(&mut self) -> Result<Option<u8>>246 fn next(&mut self) -> Result<Option<u8>> {
247 match self.ch.take() {
248 Some(ch) => {
249 #[cfg(feature = "raw_value")]
250 {
251 if let Some(ref mut buf) = self.raw_buffer {
252 buf.push(ch);
253 }
254 }
255 Ok(Some(ch))
256 }
257 None => match self.iter.next() {
258 Some(Err(err)) => Err(Error::io(err)),
259 Some(Ok(ch)) => {
260 #[cfg(feature = "raw_value")]
261 {
262 if let Some(ref mut buf) = self.raw_buffer {
263 buf.push(ch);
264 }
265 }
266 Ok(Some(ch))
267 }
268 None => Ok(None),
269 },
270 }
271 }
272
273 #[inline]
peek(&mut self) -> Result<Option<u8>>274 fn peek(&mut self) -> Result<Option<u8>> {
275 match self.ch {
276 Some(ch) => Ok(Some(ch)),
277 None => match self.iter.next() {
278 Some(Err(err)) => Err(Error::io(err)),
279 Some(Ok(ch)) => {
280 self.ch = Some(ch);
281 Ok(self.ch)
282 }
283 None => Ok(None),
284 },
285 }
286 }
287
288 #[cfg(not(feature = "raw_value"))]
289 #[inline]
discard(&mut self)290 fn discard(&mut self) {
291 self.ch = None;
292 }
293
294 #[cfg(feature = "raw_value")]
discard(&mut self)295 fn discard(&mut self) {
296 if let Some(ch) = self.ch.take() {
297 if let Some(ref mut buf) = self.raw_buffer {
298 buf.push(ch);
299 }
300 }
301 }
302
position(&self) -> Position303 fn position(&self) -> Position {
304 Position {
305 line: self.iter.line(),
306 column: self.iter.col(),
307 }
308 }
309
peek_position(&self) -> Position310 fn peek_position(&self) -> Position {
311 // The LineColIterator updates its position during peek() so it has the
312 // right one here.
313 self.position()
314 }
315
byte_offset(&self) -> usize316 fn byte_offset(&self) -> usize {
317 match self.ch {
318 Some(_) => self.iter.byte_offset() - 1,
319 None => self.iter.byte_offset(),
320 }
321 }
322
parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>>323 fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>> {
324 self.parse_str_bytes(scratch, true, as_str)
325 .map(Reference::Copied)
326 }
327
parse_str_raw<'s>( &'s mut self, scratch: &'s mut Vec<u8>, ) -> Result<Reference<'de, 's, [u8]>>328 fn parse_str_raw<'s>(
329 &'s mut self,
330 scratch: &'s mut Vec<u8>,
331 ) -> Result<Reference<'de, 's, [u8]>> {
332 self.parse_str_bytes(scratch, false, |_, bytes| Ok(bytes))
333 .map(Reference::Copied)
334 }
335
ignore_str(&mut self) -> Result<()>336 fn ignore_str(&mut self) -> Result<()> {
337 loop {
338 let ch = tri!(next_or_eof(self));
339 if !ESCAPE[ch as usize] {
340 continue;
341 }
342 match ch {
343 b'"' => {
344 return Ok(());
345 }
346 b'\\' => {
347 tri!(ignore_escape(self));
348 }
349 _ => {
350 return error(self, ErrorCode::ControlCharacterWhileParsingString);
351 }
352 }
353 }
354 }
355
decode_hex_escape(&mut self) -> Result<u16>356 fn decode_hex_escape(&mut self) -> Result<u16> {
357 let mut n = 0;
358 for _ in 0..4 {
359 match decode_hex_val(tri!(next_or_eof(self))) {
360 None => return error(self, ErrorCode::InvalidEscape),
361 Some(val) => {
362 n = (n << 4) + val;
363 }
364 }
365 }
366 Ok(n)
367 }
368
369 #[cfg(feature = "raw_value")]
begin_raw_buffering(&mut self)370 fn begin_raw_buffering(&mut self) {
371 self.raw_buffer = Some(Vec::new());
372 }
373
374 #[cfg(feature = "raw_value")]
end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,375 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
376 where
377 V: Visitor<'de>,
378 {
379 let raw = self.raw_buffer.take().unwrap();
380 let raw = match String::from_utf8(raw) {
381 Ok(raw) => raw,
382 Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
383 };
384 visitor.visit_map(OwnedRawDeserializer {
385 raw_value: Some(raw),
386 })
387 }
388
389 const should_early_return_if_failed: bool = true;
390
391 #[inline]
392 #[cold]
set_failed(&mut self, failed: &mut bool)393 fn set_failed(&mut self, failed: &mut bool) {
394 *failed = true;
395 }
396 }
397
398 //////////////////////////////////////////////////////////////////////////////
399
400 impl<'a> SliceRead<'a> {
401 /// Create a JSON input source to read from a slice of bytes.
new(slice: &'a [u8]) -> Self402 pub fn new(slice: &'a [u8]) -> Self {
403 SliceRead {
404 slice,
405 index: 0,
406 #[cfg(feature = "raw_value")]
407 raw_buffering_start_index: 0,
408 }
409 }
410
position_of_index(&self, i: usize) -> Position411 fn position_of_index(&self, i: usize) -> Position {
412 let mut position = Position { line: 1, column: 0 };
413 for ch in &self.slice[..i] {
414 match *ch {
415 b'\n' => {
416 position.line += 1;
417 position.column = 0;
418 }
419 _ => {
420 position.column += 1;
421 }
422 }
423 }
424 position
425 }
426
427 /// The big optimization here over IoRead is that if the string contains no
428 /// backslash escape sequences, the returned &str is a slice of the raw JSON
429 /// data so we avoid copying into the scratch space.
parse_str_bytes<'s, T, F>( &'s mut self, scratch: &'s mut Vec<u8>, validate: bool, result: F, ) -> Result<Reference<'a, 's, T>> where T: ?Sized + 's, F: for<'f> FnOnce(&'s Self, &'f [u8]) -> Result<&'f T>,430 fn parse_str_bytes<'s, T, F>(
431 &'s mut self,
432 scratch: &'s mut Vec<u8>,
433 validate: bool,
434 result: F,
435 ) -> Result<Reference<'a, 's, T>>
436 where
437 T: ?Sized + 's,
438 F: for<'f> FnOnce(&'s Self, &'f [u8]) -> Result<&'f T>,
439 {
440 // Index of the first byte not yet copied into the scratch space.
441 let mut start = self.index;
442
443 loop {
444 while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
445 self.index += 1;
446 }
447 if self.index == self.slice.len() {
448 return error(self, ErrorCode::EofWhileParsingString);
449 }
450 match self.slice[self.index] {
451 b'"' => {
452 if scratch.is_empty() {
453 // Fast path: return a slice of the raw JSON without any
454 // copying.
455 let borrowed = &self.slice[start..self.index];
456 self.index += 1;
457 return result(self, borrowed).map(Reference::Borrowed);
458 } else {
459 scratch.extend_from_slice(&self.slice[start..self.index]);
460 self.index += 1;
461 return result(self, scratch).map(Reference::Copied);
462 }
463 }
464 b'\\' => {
465 scratch.extend_from_slice(&self.slice[start..self.index]);
466 self.index += 1;
467 tri!(parse_escape(self, scratch));
468 start = self.index;
469 }
470 _ => {
471 self.index += 1;
472 if validate {
473 return error(self, ErrorCode::ControlCharacterWhileParsingString);
474 }
475 }
476 }
477 }
478 }
479 }
480
481 impl<'a> private::Sealed for SliceRead<'a> {}
482
483 impl<'a> Read<'a> for SliceRead<'a> {
484 #[inline]
next(&mut self) -> Result<Option<u8>>485 fn next(&mut self) -> Result<Option<u8>> {
486 // `Ok(self.slice.get(self.index).map(|ch| { self.index += 1; *ch }))`
487 // is about 10% slower.
488 Ok(if self.index < self.slice.len() {
489 let ch = self.slice[self.index];
490 self.index += 1;
491 Some(ch)
492 } else {
493 None
494 })
495 }
496
497 #[inline]
peek(&mut self) -> Result<Option<u8>>498 fn peek(&mut self) -> Result<Option<u8>> {
499 // `Ok(self.slice.get(self.index).map(|ch| *ch))` is about 10% slower
500 // for some reason.
501 Ok(if self.index < self.slice.len() {
502 Some(self.slice[self.index])
503 } else {
504 None
505 })
506 }
507
508 #[inline]
discard(&mut self)509 fn discard(&mut self) {
510 self.index += 1;
511 }
512
position(&self) -> Position513 fn position(&self) -> Position {
514 self.position_of_index(self.index)
515 }
516
peek_position(&self) -> Position517 fn peek_position(&self) -> Position {
518 // Cap it at slice.len() just in case the most recent call was next()
519 // and it returned the last byte.
520 self.position_of_index(cmp::min(self.slice.len(), self.index + 1))
521 }
522
byte_offset(&self) -> usize523 fn byte_offset(&self) -> usize {
524 self.index
525 }
526
parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>>527 fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>> {
528 self.parse_str_bytes(scratch, true, as_str)
529 }
530
parse_str_raw<'s>( &'s mut self, scratch: &'s mut Vec<u8>, ) -> Result<Reference<'a, 's, [u8]>>531 fn parse_str_raw<'s>(
532 &'s mut self,
533 scratch: &'s mut Vec<u8>,
534 ) -> Result<Reference<'a, 's, [u8]>> {
535 self.parse_str_bytes(scratch, false, |_, bytes| Ok(bytes))
536 }
537
ignore_str(&mut self) -> Result<()>538 fn ignore_str(&mut self) -> Result<()> {
539 loop {
540 while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
541 self.index += 1;
542 }
543 if self.index == self.slice.len() {
544 return error(self, ErrorCode::EofWhileParsingString);
545 }
546 match self.slice[self.index] {
547 b'"' => {
548 self.index += 1;
549 return Ok(());
550 }
551 b'\\' => {
552 self.index += 1;
553 tri!(ignore_escape(self));
554 }
555 _ => {
556 return error(self, ErrorCode::ControlCharacterWhileParsingString);
557 }
558 }
559 }
560 }
561
decode_hex_escape(&mut self) -> Result<u16>562 fn decode_hex_escape(&mut self) -> Result<u16> {
563 if self.index + 4 > self.slice.len() {
564 self.index = self.slice.len();
565 return error(self, ErrorCode::EofWhileParsingString);
566 }
567
568 let mut n = 0;
569 for _ in 0..4 {
570 let ch = decode_hex_val(self.slice[self.index]);
571 self.index += 1;
572 match ch {
573 None => return error(self, ErrorCode::InvalidEscape),
574 Some(val) => {
575 n = (n << 4) + val;
576 }
577 }
578 }
579 Ok(n)
580 }
581
582 #[cfg(feature = "raw_value")]
begin_raw_buffering(&mut self)583 fn begin_raw_buffering(&mut self) {
584 self.raw_buffering_start_index = self.index;
585 }
586
587 #[cfg(feature = "raw_value")]
end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value> where V: Visitor<'a>,588 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
589 where
590 V: Visitor<'a>,
591 {
592 let raw = &self.slice[self.raw_buffering_start_index..self.index];
593 let raw = match str::from_utf8(raw) {
594 Ok(raw) => raw,
595 Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
596 };
597 visitor.visit_map(BorrowedRawDeserializer {
598 raw_value: Some(raw),
599 })
600 }
601
602 const should_early_return_if_failed: bool = false;
603
604 #[inline]
605 #[cold]
set_failed(&mut self, _failed: &mut bool)606 fn set_failed(&mut self, _failed: &mut bool) {
607 self.slice = &self.slice[..self.index];
608 }
609 }
610
611 //////////////////////////////////////////////////////////////////////////////
612
613 impl<'a> StrRead<'a> {
614 /// Create a JSON input source to read from a UTF-8 string.
new(s: &'a str) -> Self615 pub fn new(s: &'a str) -> Self {
616 StrRead {
617 delegate: SliceRead::new(s.as_bytes()),
618 #[cfg(feature = "raw_value")]
619 data: s,
620 }
621 }
622 }
623
624 impl<'a> private::Sealed for StrRead<'a> {}
625
626 impl<'a> Read<'a> for StrRead<'a> {
627 #[inline]
next(&mut self) -> Result<Option<u8>>628 fn next(&mut self) -> Result<Option<u8>> {
629 self.delegate.next()
630 }
631
632 #[inline]
peek(&mut self) -> Result<Option<u8>>633 fn peek(&mut self) -> Result<Option<u8>> {
634 self.delegate.peek()
635 }
636
637 #[inline]
discard(&mut self)638 fn discard(&mut self) {
639 self.delegate.discard();
640 }
641
position(&self) -> Position642 fn position(&self) -> Position {
643 self.delegate.position()
644 }
645
peek_position(&self) -> Position646 fn peek_position(&self) -> Position {
647 self.delegate.peek_position()
648 }
649
byte_offset(&self) -> usize650 fn byte_offset(&self) -> usize {
651 self.delegate.byte_offset()
652 }
653
parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>>654 fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>> {
655 self.delegate.parse_str_bytes(scratch, true, |_, bytes| {
656 // The input is assumed to be valid UTF-8 and the \u-escapes are
657 // checked along the way, so don't need to check here.
658 Ok(unsafe { str::from_utf8_unchecked(bytes) })
659 })
660 }
661
parse_str_raw<'s>( &'s mut self, scratch: &'s mut Vec<u8>, ) -> Result<Reference<'a, 's, [u8]>>662 fn parse_str_raw<'s>(
663 &'s mut self,
664 scratch: &'s mut Vec<u8>,
665 ) -> Result<Reference<'a, 's, [u8]>> {
666 self.delegate.parse_str_raw(scratch)
667 }
668
ignore_str(&mut self) -> Result<()>669 fn ignore_str(&mut self) -> Result<()> {
670 self.delegate.ignore_str()
671 }
672
decode_hex_escape(&mut self) -> Result<u16>673 fn decode_hex_escape(&mut self) -> Result<u16> {
674 self.delegate.decode_hex_escape()
675 }
676
677 #[cfg(feature = "raw_value")]
begin_raw_buffering(&mut self)678 fn begin_raw_buffering(&mut self) {
679 self.delegate.begin_raw_buffering()
680 }
681
682 #[cfg(feature = "raw_value")]
end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value> where V: Visitor<'a>,683 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
684 where
685 V: Visitor<'a>,
686 {
687 let raw = &self.data[self.delegate.raw_buffering_start_index..self.delegate.index];
688 visitor.visit_map(BorrowedRawDeserializer {
689 raw_value: Some(raw),
690 })
691 }
692
693 const should_early_return_if_failed: bool = false;
694
695 #[inline]
696 #[cold]
set_failed(&mut self, failed: &mut bool)697 fn set_failed(&mut self, failed: &mut bool) {
698 self.delegate.set_failed(failed);
699 }
700 }
701
702 //////////////////////////////////////////////////////////////////////////////
703
704 impl<'a, 'de, R> private::Sealed for &'a mut R where R: Read<'de> {}
705
706 impl<'a, 'de, R> Read<'de> for &'a mut R
707 where
708 R: Read<'de>,
709 {
next(&mut self) -> Result<Option<u8>>710 fn next(&mut self) -> Result<Option<u8>> {
711 R::next(self)
712 }
713
peek(&mut self) -> Result<Option<u8>>714 fn peek(&mut self) -> Result<Option<u8>> {
715 R::peek(self)
716 }
717
discard(&mut self)718 fn discard(&mut self) {
719 R::discard(self)
720 }
721
position(&self) -> Position722 fn position(&self) -> Position {
723 R::position(self)
724 }
725
peek_position(&self) -> Position726 fn peek_position(&self) -> Position {
727 R::peek_position(self)
728 }
729
byte_offset(&self) -> usize730 fn byte_offset(&self) -> usize {
731 R::byte_offset(self)
732 }
733
parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>>734 fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>> {
735 R::parse_str(self, scratch)
736 }
737
parse_str_raw<'s>( &'s mut self, scratch: &'s mut Vec<u8>, ) -> Result<Reference<'de, 's, [u8]>>738 fn parse_str_raw<'s>(
739 &'s mut self,
740 scratch: &'s mut Vec<u8>,
741 ) -> Result<Reference<'de, 's, [u8]>> {
742 R::parse_str_raw(self, scratch)
743 }
744
ignore_str(&mut self) -> Result<()>745 fn ignore_str(&mut self) -> Result<()> {
746 R::ignore_str(self)
747 }
748
decode_hex_escape(&mut self) -> Result<u16>749 fn decode_hex_escape(&mut self) -> Result<u16> {
750 R::decode_hex_escape(self)
751 }
752
753 #[cfg(feature = "raw_value")]
begin_raw_buffering(&mut self)754 fn begin_raw_buffering(&mut self) {
755 R::begin_raw_buffering(self)
756 }
757
758 #[cfg(feature = "raw_value")]
end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,759 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
760 where
761 V: Visitor<'de>,
762 {
763 R::end_raw_buffering(self, visitor)
764 }
765
766 const should_early_return_if_failed: bool = R::should_early_return_if_failed;
767
set_failed(&mut self, failed: &mut bool)768 fn set_failed(&mut self, failed: &mut bool) {
769 R::set_failed(self, failed)
770 }
771 }
772
773 //////////////////////////////////////////////////////////////////////////////
774
775 /// Marker for whether StreamDeserializer can implement FusedIterator.
776 pub trait Fused: private::Sealed {}
777 impl<'a> Fused for SliceRead<'a> {}
778 impl<'a> Fused for StrRead<'a> {}
779
780 // Lookup table of bytes that must be escaped. A value of true at index i means
781 // that byte i requires an escape sequence in the input.
782 static ESCAPE: [bool; 256] = {
783 const CT: bool = true; // control character \x00..=\x1F
784 const QU: bool = true; // quote \x22
785 const BS: bool = true; // backslash \x5C
786 const __: bool = false; // allow unescaped
787 [
788 // 1 2 3 4 5 6 7 8 9 A B C D E F
789 CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 0
790 CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 1
791 __, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
792 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3
793 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4
794 __, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5
795 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6
796 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7
797 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
798 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
799 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
800 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
801 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
802 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
803 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
804 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
805 ]
806 };
807
next_or_eof<'de, R>(read: &mut R) -> Result<u8> where R: ?Sized + Read<'de>,808 fn next_or_eof<'de, R>(read: &mut R) -> Result<u8>
809 where
810 R: ?Sized + Read<'de>,
811 {
812 match tri!(read.next()) {
813 Some(b) => Ok(b),
814 None => error(read, ErrorCode::EofWhileParsingString),
815 }
816 }
817
error<'de, R, T>(read: &R, reason: ErrorCode) -> Result<T> where R: ?Sized + Read<'de>,818 fn error<'de, R, T>(read: &R, reason: ErrorCode) -> Result<T>
819 where
820 R: ?Sized + Read<'de>,
821 {
822 let position = read.position();
823 Err(Error::syntax(reason, position.line, position.column))
824 }
825
as_str<'de, 's, R: Read<'de>>(read: &R, slice: &'s [u8]) -> Result<&'s str>826 fn as_str<'de, 's, R: Read<'de>>(read: &R, slice: &'s [u8]) -> Result<&'s str> {
827 str::from_utf8(slice).or_else(|_| error(read, ErrorCode::InvalidUnicodeCodePoint))
828 }
829
830 /// Parses a JSON escape sequence and appends it into the scratch space. Assumes
831 /// the previous byte read was a backslash.
parse_escape<'de, R: Read<'de>>(read: &mut R, scratch: &mut Vec<u8>) -> Result<()>832 fn parse_escape<'de, R: Read<'de>>(read: &mut R, scratch: &mut Vec<u8>) -> Result<()> {
833 let ch = tri!(next_or_eof(read));
834
835 match ch {
836 b'"' => scratch.push(b'"'),
837 b'\\' => scratch.push(b'\\'),
838 b'/' => scratch.push(b'/'),
839 b'b' => scratch.push(b'\x08'),
840 b'f' => scratch.push(b'\x0c'),
841 b'n' => scratch.push(b'\n'),
842 b'r' => scratch.push(b'\r'),
843 b't' => scratch.push(b'\t'),
844 b'u' => {
845 let c = match tri!(read.decode_hex_escape()) {
846 0xDC00..=0xDFFF => {
847 return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
848 }
849
850 // Non-BMP characters are encoded as a sequence of
851 // two hex escapes, representing UTF-16 surrogates.
852 n1 @ 0xD800..=0xDBFF => {
853 if tri!(next_or_eof(read)) != b'\\' {
854 return error(read, ErrorCode::UnexpectedEndOfHexEscape);
855 }
856 if tri!(next_or_eof(read)) != b'u' {
857 return error(read, ErrorCode::UnexpectedEndOfHexEscape);
858 }
859
860 let n2 = tri!(read.decode_hex_escape());
861
862 if n2 < 0xDC00 || n2 > 0xDFFF {
863 return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
864 }
865
866 let n = (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000;
867
868 match char::from_u32(n) {
869 Some(c) => c,
870 None => {
871 return error(read, ErrorCode::InvalidUnicodeCodePoint);
872 }
873 }
874 }
875
876 n => match char::from_u32(n as u32) {
877 Some(c) => c,
878 None => {
879 return error(read, ErrorCode::InvalidUnicodeCodePoint);
880 }
881 },
882 };
883
884 scratch.extend_from_slice(c.encode_utf8(&mut [0_u8; 4]).as_bytes());
885 }
886 _ => {
887 return error(read, ErrorCode::InvalidEscape);
888 }
889 }
890
891 Ok(())
892 }
893
894 /// Parses a JSON escape sequence and discards the value. Assumes the previous
895 /// byte read was a backslash.
ignore_escape<'de, R>(read: &mut R) -> Result<()> where R: ?Sized + Read<'de>,896 fn ignore_escape<'de, R>(read: &mut R) -> Result<()>
897 where
898 R: ?Sized + Read<'de>,
899 {
900 let ch = tri!(next_or_eof(read));
901
902 match ch {
903 b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't' => {}
904 b'u' => {
905 let n = match tri!(read.decode_hex_escape()) {
906 0xDC00..=0xDFFF => {
907 return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
908 }
909
910 // Non-BMP characters are encoded as a sequence of
911 // two hex escapes, representing UTF-16 surrogates.
912 n1 @ 0xD800..=0xDBFF => {
913 if tri!(next_or_eof(read)) != b'\\' {
914 return error(read, ErrorCode::UnexpectedEndOfHexEscape);
915 }
916 if tri!(next_or_eof(read)) != b'u' {
917 return error(read, ErrorCode::UnexpectedEndOfHexEscape);
918 }
919
920 let n2 = tri!(read.decode_hex_escape());
921
922 if n2 < 0xDC00 || n2 > 0xDFFF {
923 return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
924 }
925
926 (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000
927 }
928
929 n => n as u32,
930 };
931
932 if char::from_u32(n).is_none() {
933 return error(read, ErrorCode::InvalidUnicodeCodePoint);
934 }
935 }
936 _ => {
937 return error(read, ErrorCode::InvalidEscape);
938 }
939 }
940
941 Ok(())
942 }
943
944 static HEX: [u8; 256] = {
945 const __: u8 = 255; // not a hex digit
946 [
947 // 1 2 3 4 5 6 7 8 9 A B C D E F
948 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 0
949 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 1
950 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
951 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, __, __, __, __, __, __, // 3
952 __, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, // 4
953 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 5
954 __, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, // 6
955 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7
956 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
957 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
958 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
959 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
960 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
961 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
962 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
963 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
964 ]
965 };
966
decode_hex_val(val: u8) -> Option<u16>967 fn decode_hex_val(val: u8) -> Option<u16> {
968 let n = HEX[val as usize] as u16;
969 if n == 255 {
970 None
971 } else {
972 Some(n)
973 }
974 }
975