/// Trait used by the deserializer for iterating over input. This is manually /// "specialized" for iterating over `&[u8]`. Once feature(specialization) is /// stable we can use actual specialization. /// /// This trait is sealed and cannot be implemented for types outside of /// `serde_json`. pubtrait Read<'de>: private::Sealed { #[doc(hidden)] fn next(&mutself) -> Result<Option<u8>>; #[doc(hidden)] fn peek(&mutself) -> Result<Option<u8>>;
/// Only valid after a call to peek(). Discards the peeked byte. #[doc(hidden)] fn discard(&mutself);
/// Position of the most recent call to next(). /// /// The most recent call was probably next() and not peek(), but this method /// should try to return a sensible result if the most recent call was /// actually peek() because we don't always know. /// /// Only called in case of an error, so performance is not important. #[doc(hidden)] fn position(&self) -> Position;
/// Position of the most recent call to peek(). /// /// The most recent call was probably peek() and not next(), but this method /// should try to return a sensible result if the most recent call was /// actually next() because we don't always know. /// /// Only called in case of an error, so performance is not important. #[doc(hidden)] fn peek_position(&self) -> Position;
/// Offset from the beginning of the input to the next byte that would be /// returned by next() or peek(). #[doc(hidden)] fn byte_offset(&self) -> usize;
/// Assumes the previous byte was a quotation mark. Parses a JSON-escaped /// string until the next quotation mark using the given scratch space if /// necessary. The scratch space is initially empty. #[doc(hidden)] fn parse_str<'s>(&'s mutself, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>>;
/// Assumes the previous byte was a quotation mark. Parses a JSON-escaped /// string until the next quotation mark using the given scratch space if /// necessary. The scratch space is initially empty. /// /// This function returns the raw bytes in the string with escape sequences /// expanded but without performing unicode validation. #[doc(hidden)] fn parse_str_raw<'s>(
&'s mut self,
scratch: &'s mut Vec<u8>,
) -> Result<Reference<'de, 's, [u8]>>;
/// Assumes the previous byte was a quotation mark. Parses a JSON-escaped /// string until the next quotation mark but discards the data. #[doc(hidden)] fn ignore_str(&mutself) -> Result<()>;
/// Assumes the previous byte was a hex escape sequence ('\u') in a string. /// Parses next hexadecimal sequence. #[doc(hidden)] fn decode_hex_escape(&mutself) -> Result<u16>;
/// Switch raw buffering mode on. /// /// This is used when deserializing `RawValue`. #[cfg(feature = "raw_value")] #[doc(hidden)] fn begin_raw_buffering(&mutself);
/// Switch raw buffering mode off and provides the raw buffered data to the /// given visitor. #[cfg(feature = "raw_value")] #[doc(hidden)] fn end_raw_buffering<V>(&mutself, visitor: V) -> Result<V::Value> where
V: Visitor<'de>;
/// Whether StreamDeserializer::next needs to check the failed flag. True /// for IoRead, false for StrRead and SliceRead which can track failure by /// truncating their input slice to avoid the extra check on every next /// call. #[doc(hidden)] const should_early_return_if_failed: bool;
/// Mark a persistent failure of StreamDeserializer, either by setting the /// flag or by truncating the input data. #[doc(hidden)] fn set_failed(&mutself, failed: &mut bool);
}
pubstruct Position { pub line: usize, pub column: usize,
}
/// JSON input source that reads from a std::io input stream. #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] pubstruct IoRead<R> where
R: io::Read,
{
iter: LineColIterator<io::Bytes<R>>, /// Temporary storage of peeked byte.
ch: Option<u8>, #[cfg(feature = "raw_value")]
raw_buffer: Option<Vec<u8>>,
}
/// JSON input source that reads from a slice of bytes. // // This is more efficient than other iterators because peek() can be read-only // and we can compute line/col position only if an error happens. pubstruct SliceRead<'a> {
slice: &'a [u8], /// Index of the *next* byte that will be returned by next() or peek().
index: usize, #[cfg(feature = "raw_value")]
raw_buffering_start_index: usize,
}
/// JSON input source that reads from a UTF-8 string. // // Able to elide UTF-8 checks by assuming that the input is valid UTF-8. pubstruct StrRead<'a> {
delegate: SliceRead<'a>, #[cfg(feature = "raw_value")]
data: &'a str,
}
// Prevent users from implementing the Read trait. mod private { pubtrait Sealed {}
}
impl<'a> SliceRead<'a> { /// Create a JSON input source to read from a slice of bytes. pubfn new(slice: &'a [u8]) -> Self {
SliceRead {
slice,
index: 0, #[cfg(feature = "raw_value")]
raw_buffering_start_index: 0,
}
}
fn position_of_index(&self, i: usize) -> Position { letmut position = Position { line: 1, column: 0 }; for ch in &self.slice[..i] { match *ch {
b'\n' => {
position.line += 1;
position.column = 0;
}
_ => {
position.column += 1;
}
}
}
position
}
/// The big optimization here over IoRead is that if the string contains no /// backslash escape sequences, the returned &str is a slice of the raw JSON /// data so we avoid copying into the scratch space. fn 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>,
{ // Index of the first byte not yet copied into the scratch space. letmut start = self.index;
loop { whileself.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] { self.index += 1;
} ifself.index == self.slice.len() { return error(self, ErrorCode::EofWhileParsingString);
} matchself.slice[self.index] {
b'"' => { if scratch.is_empty() { // Fast path: return a slice of the raw JSON without any // copying. let borrowed = &self.slice[start..self.index]; self.index += 1; return result(self, borrowed).map(Reference::Borrowed);
} else {
scratch.extend_from_slice(&self.slice[start..self.index]); self.index += 1; return result(self, scratch).map(Reference::Copied);
}
}
b'\\' => {
scratch.extend_from_slice(&self.slice[start..self.index]); self.index += 1;
tri!(parse_escape(self, validate, scratch));
start = self.index;
}
_ => { self.index += 1; if validate { return error(self, ErrorCode::ControlCharacterWhileParsingString);
}
}
}
}
}
}
fn position(&self) -> Position { self.position_of_index(self.index)
}
fn peek_position(&self) -> Position { // Cap it at slice.len() just in case the most recent call was next() // and it returned the last byte. self.position_of_index(cmp::min(self.slice.len(), self.index + 1))
}
fn parse_str<'s>(&'s mutself, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>> { self.delegate.parse_str_bytes(scratch, true, |_, bytes| { // The deserialization input came in as &str with a UTF-8 guarantee, // and the \u-escapes are checked along the way, so don't need to // check here.
Ok(unsafe { str::from_utf8_unchecked(bytes) })
})
}
/// Parses a JSON escape sequence and appends it into the scratch space. Assumes /// the previous byte read was a backslash. fn parse_escape<'de, R: Read<'de>>(
read: &mut R,
validate: bool,
scratch: &mut Vec<u8>,
) -> Result<()> { let ch = tri!(next_or_eof(read));
let c = match tri!(read.decode_hex_escape()) {
n @ 0xDC00..=0xDFFF => { returnif validate {
error(read, ErrorCode::LoneLeadingSurrogateInHexEscape)
} else {
encode_surrogate(scratch, n);
Ok(())
};
}
// Non-BMP characters are encoded as a sequence of two hex // escapes, representing UTF-16 surrogates. If deserializing a // utf-8 string the surrogates are required to be paired, // whereas deserializing a byte string accepts lone surrogates.
n1 @ 0xD800..=0xDBFF => { if tri!(peek_or_eof(read)) == b'\\' {
read.discard();
} else { returnif validate {
read.discard();
error(read, ErrorCode::UnexpectedEndOfHexEscape)
} else {
encode_surrogate(scratch, n1);
Ok(())
};
}
if tri!(peek_or_eof(read)) == b'u' {
read.discard();
} else { returnif validate {
read.discard();
error(read, ErrorCode::UnexpectedEndOfHexEscape)
} else {
encode_surrogate(scratch, n1); // The \ prior to this byte started an escape sequence, // so we need to parse that now. This recursive call // does not blow the stack on malicious input because // the escape is not \u, so it will be handled by one // of the easy nonrecursive cases.
parse_escape(read, validate, scratch)
};
}
/// Parses a JSON escape sequence and discards the value. Assumes the previous /// byte read was a backslash. fn ignore_escape<'de, R>(read: &mut R) -> Result<()> where
R: ?Sized + Read<'de>,
{ let ch = tri!(next_or_eof(read));
match ch {
b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't' => {}
b'u' => { // At this point we don't care if the codepoint is valid. We just // want to consume it. We don't actually know what is valid or not // at this point, because that depends on if this string will // ultimately be parsed into a string or a byte buffer in the "real" // parse.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.