use std::io;
use std::marker::PhantomData;
use std::result;
use std::str::FromStr;
use std::{i32, u64};
use serde::de::{self, Expected, Unexpected};
use super::error::{Error, ErrorCode, Result};
use read::{self, Reference};
pub use read::{IoRead, Read, SliceRead, StrRead};
use number::Number;
#[cfg(feature = "arbitrary_precision")]
use number::NumberDeserializer;
pub struct Deserializer<R> {
read: R,
scratch: Vec<u8>,
remaining_depth: u8,
}
impl<'de, R> Deserializer<R>
where
R: read::Read<'de>,
{
pub fn new(read: R) -> Self {
Deserializer {
read: read,
scratch: Vec::new(),
remaining_depth: 128,
}
}
}
impl<R> Deserializer<read::IoRead<R>>
where
R: io::Read,
{
pub fn from_reader(reader: R) -> Self {
Deserializer::new(read::IoRead::new(reader))
}
}
impl<'a> Deserializer<read::SliceRead<'a>> {
pub fn from_slice(bytes: &'a [u8]) -> Self {
Deserializer::new(read::SliceRead::new(bytes))
}
}
impl<'a> Deserializer<read::StrRead<'a>> {
pub fn from_str(s: &'a str) -> Self {
Deserializer::new(read::StrRead::new(s))
}
}
macro_rules! overflow {
($a:ident * 10 + $b:ident, $c:expr) => {
$a >= $c / 10 && ($a > $c / 10 || $b > $c % 10)
};
}
#[doc(hidden)]
pub enum ParserNumber {
F64(f64),
U64(u64),
I64(i64),
#[cfg(feature = "arbitrary_precision")]
String(String),
}
impl ParserNumber {
fn visit<'de, V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
match self {
ParserNumber::F64(x) => visitor.visit_f64(x),
ParserNumber::U64(x) => visitor.visit_u64(x),
ParserNumber::I64(x) => visitor.visit_i64(x),
#[cfg(feature = "arbitrary_precision")]
ParserNumber::String(x) => visitor.visit_map(NumberDeserializer { number: x.into() }),
}
}
fn invalid_type(self, exp: &Expected) -> Error {
match self {
ParserNumber::F64(x) => de::Error::invalid_type(Unexpected::Float(x), exp),
ParserNumber::U64(x) => de::Error::invalid_type(Unexpected::Unsigned(x), exp),
ParserNumber::I64(x) => de::Error::invalid_type(Unexpected::Signed(x), exp),
#[cfg(feature = "arbitrary_precision")]
ParserNumber::String(_) => de::Error::invalid_type(Unexpected::Other("number"), exp),
}
}
}
impl<'de, R: Read<'de>> Deserializer<R> {
pub fn end(&mut self) -> Result<()> {
match try!(self.parse_whitespace()) {
Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)),
None => Ok(()),
}
}
pub fn into_iter<T>(self) -> StreamDeserializer<'de, R, T>
where
T: de::Deserialize<'de>,
{
let offset = self.read.byte_offset();
StreamDeserializer {
de: self,
offset: offset,
output: PhantomData,
lifetime: PhantomData,
}
}
fn peek(&mut self) -> Result<Option<u8>> {
self.read.peek()
}
fn peek_or_null(&mut self) -> Result<u8> {
Ok(try!(self.peek()).unwrap_or(b'\x00'))
}
fn eat_char(&mut self) {
self.read.discard();
}
fn next_char(&mut self) -> Result<Option<u8>> {
self.read.next()
}
fn next_char_or_null(&mut self) -> Result<u8> {
Ok(try!(self.next_char()).unwrap_or(b'\x00'))
}
#[cold]
fn error(&self, reason: ErrorCode) -> Error {
let position = self.read.position();
Error::syntax(reason, position.line, position.column)
}
#[cold]
fn peek_error(&self, reason: ErrorCode) -> Error {
let position = self.read.peek_position();
Error::syntax(reason, position.line, position.column)
}
fn parse_whitespace(&mut self) -> Result<Option<u8>> {
loop {
match try!(self.peek()) {
Some(b' ') | Some(b'\n') | Some(b'\t') | Some(b'\r') => {
self.eat_char();
}
other => {
return Ok(other);
}
}
}
}
#[cold]
fn peek_invalid_type(&mut self, exp: &Expected) -> Error {
let err = match self.peek_or_null().unwrap_or(b'\x00') {
b'n' => {
self.eat_char();
if let Err(err) = self.parse_ident(b"ull") {
return err;
}
de::Error::invalid_type(Unexpected::Unit, exp)
}
b't' => {
self.eat_char();
if let Err(err) = self.parse_ident(b"rue") {
return err;
}
de::Error::invalid_type(Unexpected::Bool(true), exp)
}
b'f' => {
self.eat_char();
if let Err(err) = self.parse_ident(b"alse") {
return err;
}
de::Error::invalid_type(Unexpected::Bool(false), exp)
}
b'-' => {
self.eat_char();
match self.parse_any_number(false) {
Ok(n) => n.invalid_type(exp),
Err(err) => return err,
}
}
b'0'...b'9' => match self.parse_any_number(true) {
Ok(n) => n.invalid_type(exp),
Err(err) => return err,
},
b'"' => {
self.eat_char();
self.scratch.clear();
match self.read.parse_str(&mut self.scratch) {
Ok(s) => de::Error::invalid_type(Unexpected::Str(&s), exp),
Err(err) => return err,
}
}
b'[' => de::Error::invalid_type(Unexpected::Seq, exp),
b'{' => de::Error::invalid_type(Unexpected::Map, exp),
_ => self.peek_error(ErrorCode::ExpectedSomeValue),
};
self.fix_position(err)
}
fn deserialize_prim_number<V>(&mut self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match try!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'-' => {
self.eat_char();
try!(self.parse_integer(false)).visit(visitor)
}
b'0'...b'9' => try!(self.parse_integer(true)).visit(visitor),
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
serde_if_integer128! {
fn scan_integer128(&mut self, buf: &mut String) -> Result<()> {
match try!(self.next_char_or_null()) {
b'0' => {
buf.push('0');
match try!(self.peek_or_null()) {
b'0'...b'9' => {
Err(self.peek_error(ErrorCode::InvalidNumber))
}
_ => Ok(()),
}
}
c @ b'1'...b'9' => {
buf.push(c as char);
while let c @ b'0'...b'9' = try!(self.peek_or_null()) {
self.eat_char();
buf.push(c as char);
}
Ok(())
}
_ => {
Err(self.error(ErrorCode::InvalidNumber))
}
}
}
}
#[cold]
fn fix_position(&self, err: Error) -> Error {
err.fix_position(move |code| self.error(code))
}
fn parse_ident(&mut self, ident: &[u8]) -> Result<()> {
for expected in ident {
match try!(self.next_char()) {
None => {
return Err(self.error(ErrorCode::EofWhileParsingValue));
}
Some(next) => {
if next != *expected {
return Err(self.error(ErrorCode::ExpectedSomeIdent));
}
}
}
}
Ok(())
}
fn parse_integer(&mut self, positive: bool) -> Result<ParserNumber> {
match try!(self.next_char_or_null()) {
b'0' => {
match try!(self.peek_or_null()) {
b'0'...b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)),
_ => self.parse_number(positive, 0),
}
}
c @ b'1'...b'9' => {
let mut res = (c - b'0') as u64;
loop {
match try!(self.peek_or_null()) {
c @ b'0'...b'9' => {
self.eat_char();
let digit = (c - b'0') as u64;
if overflow!(res * 10 + digit, u64::max_value()) {
return Ok(ParserNumber::F64(try!(self.parse_long_integer(
positive,
res,
1,
))));
}
res = res * 10 + digit;
}
_ => {
return self.parse_number(positive, res);
}
}
}
}
_ => Err(self.error(ErrorCode::InvalidNumber)),
}
}
fn parse_long_integer(
&mut self,
positive: bool,
significand: u64,
mut exponent: i32,
) -> Result<f64> {
loop {
match try!(self.peek_or_null()) {
b'0'...b'9' => {
self.eat_char();
exponent += 1;
}
b'.' => {
return self.parse_decimal(positive, significand, exponent);
}
b'e' | b'E' => {
return self.parse_exponent(positive, significand, exponent);
}
_ => {
return self.f64_from_parts(positive, significand, exponent);
}
}
}
}
fn parse_number(&mut self, positive: bool, significand: u64) -> Result<ParserNumber> {
Ok(match try!(self.peek_or_null()) {
b'.' => ParserNumber::F64(try!(self.parse_decimal(positive, significand, 0))),
b'e' | b'E' => ParserNumber::F64(try!(self.parse_exponent(positive, significand, 0))),
_ => {
if positive {
ParserNumber::U64(significand)
} else {
let neg = (significand as i64).wrapping_neg();
if neg > 0 {
ParserNumber::F64(-(significand as f64))
} else {
ParserNumber::I64(neg)
}
}
}
})
}
fn parse_decimal(
&mut self,
positive: bool,
mut significand: u64,
mut exponent: i32,
) -> Result<f64> {
self.eat_char();
let mut at_least_one_digit = false;
while let c @ b'0'...b'9' = try!(self.peek_or_null()) {
self.eat_char();
let digit = (c - b'0') as u64;
at_least_one_digit = true;
if overflow!(significand * 10 + digit, u64::max_value()) {
while let b'0'...b'9' = try!(self.peek_or_null()) {
self.eat_char();
}
break;
}
significand = significand * 10 + digit;
exponent -= 1;
}
if !at_least_one_digit {
return Err(self.peek_error(ErrorCode::InvalidNumber));
}
match try!(self.peek_or_null()) {
b'e' | b'E' => self.parse_exponent(positive, significand, exponent),
_ => self.f64_from_parts(positive, significand, exponent),
}
}
fn parse_exponent(
&mut self,
positive: bool,
significand: u64,
starting_exp: i32,
) -> Result<f64> {
self.eat_char();
let positive_exp = match try!(self.peek_or_null()) {
b'+' => {
self.eat_char();
true
}
b'-' => {
self.eat_char();
false
}
_ => true,
};
let mut exp = match try!(self.next_char_or_null()) {
c @ b'0'...b'9' => (c - b'0') as i32,
_ => {
return Err(self.error(ErrorCode::InvalidNumber));
}
};
while let c @ b'0'...b'9' = try!(self.peek_or_null()) {
self.eat_char();
let digit = (c - b'0') as i32;
if overflow!(exp * 10 + digit, i32::max_value()) {
return self.parse_exponent_overflow(positive, significand, positive_exp);
}
exp = exp * 10 + digit;
}
let final_exp = if positive_exp {
starting_exp.saturating_add(exp)
} else {
starting_exp.saturating_sub(exp)
};
self.f64_from_parts(positive, significand, final_exp)
}
#[cold]
#[inline(never)]
fn parse_exponent_overflow(
&mut self,
positive: bool,
significand: u64,
positive_exp: bool,
) -> Result<f64> {
if significand != 0 && positive_exp {
return Err(self.error(ErrorCode::NumberOutOfRange));
}
while let b'0'...b'9' = try!(self.peek_or_null()) {
self.eat_char();
}
Ok(if positive { 0.0 } else { -0.0 })
}
fn parse_any_signed_number(&mut self) -> Result<ParserNumber> {
let peek = match try!(self.peek()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'-' => {
self.eat_char();
self.parse_any_number(false)
}
b'0'...b'9' => self.parse_any_number(true),
_ => Err(self.peek_error(ErrorCode::InvalidNumber)),
};
let value = match try!(self.peek()) {
Some(_) => Err(self.peek_error(ErrorCode::InvalidNumber)),
None => value,
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
#[cfg(not(feature = "arbitrary_precision"))]
fn parse_any_number(&mut self, positive: bool) -> Result<ParserNumber> {
self.parse_integer(positive)
}
#[cfg(feature = "arbitrary_precision")]
fn parse_any_number(&mut self, positive: bool) -> Result<ParserNumber> {
let mut buf = String::with_capacity(16);
if !positive {
buf.push('-');
}
self.scan_integer(&mut buf)?;
Ok(ParserNumber::String(buf))
}
#[cfg(feature = "arbitrary_precision")]
fn scan_or_null(&mut self, buf: &mut String) -> Result<u8> {
match try!(self.next_char()) {
Some(b) => {
buf.push(b as char);
Ok(b)
}
None => Ok(b'\x00'),
}
}
#[cfg(feature = "arbitrary_precision")]
fn scan_integer(&mut self, buf: &mut String) -> Result<()> {
match try!(self.scan_or_null(buf)) {
b'0' => {
match try!(self.peek_or_null()) {
b'0'...b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)),
_ => self.scan_number(buf),
}
}
b'1'...b'9' => loop {
match try!(self.peek_or_null()) {
c @ b'0'...b'9' => {
self.eat_char();
buf.push(c as char);
}
_ => {
return self.scan_number(buf);
}
}
},
_ => Err(self.error(ErrorCode::InvalidNumber)),
}
}
#[cfg(feature = "arbitrary_precision")]
fn scan_number(&mut self, buf: &mut String) -> Result<()> {
match try!(self.peek_or_null()) {
b'.' => self.scan_decimal(buf),
b'e' | b'E' => self.scan_exponent(buf),
_ => Ok(()),
}
}
#[cfg(feature = "arbitrary_precision")]
fn scan_decimal(&mut self, buf: &mut String) -> Result<()> {
self.eat_char();
buf.push('.');
let mut at_least_one_digit = false;
while let c @ b'0'...b'9' = try!(self.peek_or_null()) {
self.eat_char();
buf.push(c as char);
at_least_one_digit = true;
}
if !at_least_one_digit {
return Err(self.peek_error(ErrorCode::InvalidNumber));
}
match try!(self.peek_or_null()) {
b'e' | b'E' => self.scan_exponent(buf),
_ => Ok(()),
}
}
#[cfg(feature = "arbitrary_precision")]
fn scan_exponent(&mut self, buf: &mut String) -> Result<()> {
self.eat_char();
buf.push('e');
match try!(self.peek_or_null()) {
b'+' => {
self.eat_char();
}
b'-' => {
self.eat_char();
buf.push('-');
}
_ => {}
}
match try!(self.scan_or_null(buf)) {
b'0'...b'9' => {}
_ => {
return Err(self.error(ErrorCode::InvalidNumber));
}
}
while let c @ b'0'...b'9' = try!(self.peek_or_null()) {
self.eat_char();
buf.push(c as char);
}
Ok(())
}
fn f64_from_parts(
&mut self,
positive: bool,
significand: u64,
mut exponent: i32,
) -> Result<f64> {
let mut f = significand as f64;
loop {
match POW10.get(exponent.wrapping_abs() as usize) {
Some(&pow) => {
if exponent >= 0 {
f *= pow;
if f.is_infinite() {
return Err(self.error(ErrorCode::NumberOutOfRange));
}
} else {
f /= pow;
}
break;
}
None => {
if f == 0.0 {
break;
}
if exponent >= 0 {
return Err(self.error(ErrorCode::NumberOutOfRange));
}
f /= 1e308;
exponent += 308;
}
}
}
Ok(if positive { f } else { -f })
}
fn parse_object_colon(&mut self) -> Result<()> {
match try!(self.parse_whitespace()) {
Some(b':') => {
self.eat_char();
Ok(())
}
Some(_) => Err(self.peek_error(ErrorCode::ExpectedColon)),
None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
}
}
fn end_seq(&mut self) -> Result<()> {
match try!(self.parse_whitespace()) {
Some(b']') => {
self.eat_char();
Ok(())
}
Some(b',') => {
self.eat_char();
match self.parse_whitespace() {
Ok(Some(b']')) => Err(self.peek_error(ErrorCode::TrailingComma)),
_ => Err(self.peek_error(ErrorCode::TrailingCharacters)),
}
}
Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)),
None => Err(self.peek_error(ErrorCode::EofWhileParsingList)),
}
}
fn end_map(&mut self) -> Result<()> {
match try!(self.parse_whitespace()) {
Some(b'}') => {
self.eat_char();
Ok(())
}
Some(b',') => Err(self.peek_error(ErrorCode::TrailingComma)),
Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)),
None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
}
}
fn ignore_value(&mut self) -> Result<()> {
self.scratch.clear();
let mut enclosing = None;
loop {
let peek = match try!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let frame = match peek {
b'n' => {
self.eat_char();
try!(self.parse_ident(b"ull"));
None
}
b't' => {
self.eat_char();
try!(self.parse_ident(b"rue"));
None
}
b'f' => {
self.eat_char();
try!(self.parse_ident(b"alse"));
None
}
b'-' => {
self.eat_char();
try!(self.ignore_integer());
None
}
b'0'...b'9' => {
try!(self.ignore_integer());
None
}
b'"' => {
self.eat_char();
try!(self.read.ignore_str());
None
}
frame @ b'[' | frame @ b'{' => {
self.scratch.extend(enclosing.take());
self.eat_char();
Some(frame)
}
_ => return Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
};
let (mut accept_comma, mut frame) = match frame {
Some(frame) => (false, frame),
None => match enclosing.take() {
Some(frame) => (true, frame),
None => match self.scratch.pop() {
Some(frame) => (true, frame),
None => return Ok(()),
},
},
};
loop {
match try!(self.parse_whitespace()) {
Some(b',') if accept_comma => {
self.eat_char();
break;
}
Some(b']') if frame == b'[' => {}
Some(b'}') if frame == b'{' => {}
Some(_) => {
if accept_comma {
return Err(self.peek_error(match frame {
b'[' => ErrorCode::ExpectedListCommaOrEnd,
b'{' => ErrorCode::ExpectedObjectCommaOrEnd,
_ => unreachable!(),
}));
} else {
break;
}
}
None => {
return Err(self.peek_error(match frame {
b'[' => ErrorCode::EofWhileParsingList,
b'{' => ErrorCode::EofWhileParsingObject,
_ => unreachable!(),
}));
}
}
self.eat_char();
frame = match self.scratch.pop() {
Some(frame) => frame,
None => return Ok(()),
};
accept_comma = true;
}
if frame == b'{' {
match try!(self.parse_whitespace()) {
Some(b'"') => self.eat_char(),
Some(_) => return Err(self.peek_error(ErrorCode::KeyMustBeAString)),
None => return Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
}
try!(self.read.ignore_str());
match try!(self.parse_whitespace()) {
Some(b':') => self.eat_char(),
Some(_) => return Err(self.peek_error(ErrorCode::ExpectedColon)),
None => return Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
}
}
enclosing = Some(frame);
}
}
fn ignore_integer(&mut self) -> Result<()> {
match try!(self.next_char_or_null()) {
b'0' => {
if let b'0'...b'9' = try!(self.peek_or_null()) {
return Err(self.peek_error(ErrorCode::InvalidNumber));
}
}
b'1'...b'9' => {
while let b'0'...b'9' = try!(self.peek_or_null()) {
self.eat_char();
}
}
_ => {
return Err(self.error(ErrorCode::InvalidNumber));
}
}
match try!(self.peek_or_null()) {
b'.' => self.ignore_decimal(),
b'e' | b'E' => self.ignore_exponent(),
_ => Ok(()),
}
}
fn ignore_decimal(&mut self) -> Result<()> {
self.eat_char();
let mut at_least_one_digit = false;
while let b'0'...b'9' = try!(self.peek_or_null()) {
self.eat_char();
at_least_one_digit = true;
}
if !at_least_one_digit {
return Err(self.peek_error(ErrorCode::InvalidNumber));
}
match try!(self.peek_or_null()) {
b'e' | b'E' => self.ignore_exponent(),
_ => Ok(()),
}
}
fn ignore_exponent(&mut self) -> Result<()> {
self.eat_char();
match try!(self.peek_or_null()) {
b'+' | b'-' => self.eat_char(),
_ => {}
}
match try!(self.next_char_or_null()) {
b'0'...b'9' => {}
_ => {
return Err(self.error(ErrorCode::InvalidNumber));
}
}
while let b'0'...b'9' = try!(self.peek_or_null()) {
self.eat_char();
}
Ok(())
}
#[cfg(feature = "raw_value")]
fn deserialize_raw_value<V>(&mut self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.parse_whitespace()?;
self.read.begin_raw_buffering();
self.ignore_value()?;
self.read.end_raw_buffering(visitor)
}
}
impl FromStr for Number {
type Err = Error;
fn from_str(s: &str) -> result::Result<Self, Self::Err> {
Deserializer::from_str(s)
.parse_any_signed_number()
.map(Into::into)
}
}
static POW10: [f64; 309] = [
1e000, 1e001, 1e002, 1e003, 1e004, 1e005, 1e006, 1e007, 1e008, 1e009,
1e010, 1e011, 1e012, 1e013, 1e014, 1e015, 1e016, 1e017, 1e018, 1e019,
1e020, 1e021, 1e022, 1e023, 1e024, 1e025, 1e026, 1e027, 1e028, 1e029,
1e030, 1e031, 1e032, 1e033, 1e034, 1e035, 1e036, 1e037, 1e038, 1e039,
1e040, 1e041, 1e042, 1e043, 1e044, 1e045, 1e046, 1e047, 1e048, 1e049,
1e050, 1e051, 1e052, 1e053, 1e054, 1e055, 1e056, 1e057, 1e058, 1e059,
1e060, 1e061, 1e062, 1e063, 1e064, 1e065, 1e066, 1e067, 1e068, 1e069,
1e070, 1e071, 1e072, 1e073, 1e074, 1e075, 1e076, 1e077, 1e078, 1e079,
1e080, 1e081, 1e082, 1e083, 1e084, 1e085, 1e086, 1e087, 1e088, 1e089,
1e090, 1e091, 1e092, 1e093, 1e094, 1e095, 1e096, 1e097, 1e098, 1e099,
1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109,
1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119,
1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129,
1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139,
1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149,
1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159,
1e160, 1e161, 1e162, 1e163, 1e164, 1e165, 1e166, 1e167, 1e168, 1e169,
1e170, 1e171, 1e172, 1e173, 1e174, 1e175, 1e176, 1e177, 1e178, 1e179,
1e180, 1e181, 1e182, 1e183, 1e184, 1e185, 1e186, 1e187, 1e188, 1e189,
1e190, 1e191, 1e192, 1e193, 1e194, 1e195, 1e196, 1e197, 1e198, 1e199,
1e200, 1e201, 1e202, 1e203, 1e204, 1e205, 1e206, 1e207, 1e208, 1e209,
1e210, 1e211, 1e212, 1e213, 1e214, 1e215, 1e216, 1e217, 1e218, 1e219,
1e220, 1e221, 1e222, 1e223, 1e224, 1e225, 1e226, 1e227, 1e228, 1e229,
1e230, 1e231, 1e232, 1e233, 1e234, 1e235, 1e236, 1e237, 1e238, 1e239,
1e240, 1e241, 1e242, 1e243, 1e244, 1e245, 1e246, 1e247, 1e248, 1e249,
1e250, 1e251, 1e252, 1e253, 1e254, 1e255, 1e256, 1e257, 1e258, 1e259,
1e260, 1e261, 1e262, 1e263, 1e264, 1e265, 1e266, 1e267, 1e268, 1e269,
1e270, 1e271, 1e272, 1e273, 1e274, 1e275, 1e276, 1e277, 1e278, 1e279,
1e280, 1e281, 1e282, 1e283, 1e284, 1e285, 1e286, 1e287, 1e288, 1e289,
1e290, 1e291, 1e292, 1e293, 1e294, 1e295, 1e296, 1e297, 1e298, 1e299,
1e300, 1e301, 1e302, 1e303, 1e304, 1e305, 1e306, 1e307, 1e308,
];
macro_rules! deserialize_prim_number {
($method:ident) => {
fn $method<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_prim_number(visitor)
}
}
}
impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
type Error = Error;
#[inline]
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match try!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'n' => {
self.eat_char();
try!(self.parse_ident(b"ull"));
visitor.visit_unit()
}
b't' => {
self.eat_char();
try!(self.parse_ident(b"rue"));
visitor.visit_bool(true)
}
b'f' => {
self.eat_char();
try!(self.parse_ident(b"alse"));
visitor.visit_bool(false)
}
b'-' => {
self.eat_char();
try!(self.parse_any_number(false)).visit(visitor)
}
b'0'...b'9' => try!(self.parse_any_number(true)).visit(visitor),
b'"' => {
self.eat_char();
self.scratch.clear();
match try!(self.read.parse_str(&mut self.scratch)) {
Reference::Borrowed(s) => visitor.visit_borrowed_str(s),
Reference::Copied(s) => visitor.visit_str(s),
}
}
b'[' => {
self.remaining_depth -= 1;
if self.remaining_depth == 0 {
return Err(self.peek_error(ErrorCode::RecursionLimitExceeded));
}
self.eat_char();
let ret = visitor.visit_seq(SeqAccess::new(self));
self.remaining_depth += 1;
match (ret, self.end_seq()) {
(Ok(ret), Ok(())) => Ok(ret),
(Err(err), _) | (_, Err(err)) => Err(err),
}
}
b'{' => {
self.remaining_depth -= 1;
if self.remaining_depth == 0 {
return Err(self.peek_error(ErrorCode::RecursionLimitExceeded));
}
self.eat_char();
let ret = visitor.visit_map(MapAccess::new(self));
self.remaining_depth += 1;
match (ret, self.end_map()) {
(Ok(ret), Ok(())) => Ok(ret),
(Err(err), _) | (_, Err(err)) => Err(err),
}
}
_ => Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match try!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b't' => {
self.eat_char();
try!(self.parse_ident(b"rue"));
visitor.visit_bool(true)
}
b'f' => {
self.eat_char();
try!(self.parse_ident(b"alse"));
visitor.visit_bool(false)
}
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
deserialize_prim_number!(deserialize_i8);
deserialize_prim_number!(deserialize_i16);
deserialize_prim_number!(deserialize_i32);
deserialize_prim_number!(deserialize_i64);
deserialize_prim_number!(deserialize_u8);
deserialize_prim_number!(deserialize_u16);
deserialize_prim_number!(deserialize_u32);
deserialize_prim_number!(deserialize_u64);
deserialize_prim_number!(deserialize_f32);
deserialize_prim_number!(deserialize_f64);
serde_if_integer128! {
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let mut buf = String::new();
match try!(self.parse_whitespace()) {
Some(b'-') => {
self.eat_char();
buf.push('-');
}
Some(_) => {}
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
try!(self.scan_integer128(&mut buf));
let value = match buf.parse() {
Ok(int) => visitor.visit_i128(int),
Err(_) => {
return Err(self.error(ErrorCode::NumberOutOfRange));
}
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
match try!(self.parse_whitespace()) {
Some(b'-') => {
return Err(self.peek_error(ErrorCode::NumberOutOfRange));
}
Some(_) => {}
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
}
let mut buf = String::new();
try!(self.scan_integer128(&mut buf));
let value = match buf.parse() {
Ok(int) => visitor.visit_u128(int),
Err(_) => {
return Err(self.error(ErrorCode::NumberOutOfRange));
}
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
}
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_str(visitor)
}
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match try!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'"' => {
self.eat_char();
self.scratch.clear();
match try!(self.read.parse_str(&mut self.scratch)) {
Reference::Borrowed(s) => visitor.visit_borrowed_str(s),
Reference::Copied(s) => visitor.visit_str(s),
}
}
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_str(visitor)
}
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match try!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'"' => {
self.eat_char();
self.scratch.clear();
match try!(self.read.parse_str_raw(&mut self.scratch)) {
Reference::Borrowed(b) => visitor.visit_borrowed_bytes(b),
Reference::Copied(b) => visitor.visit_bytes(b),
}
}
b'[' => self.deserialize_seq(visitor),
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
#[inline]
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_bytes(visitor)
}
#[inline]
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
match try!(self.parse_whitespace()) {
Some(b'n') => {
self.eat_char();
try!(self.parse_ident(b"ull"));
visitor.visit_none()
}
_ => visitor.visit_some(self),
}
}
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match try!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'n' => {
self.eat_char();
try!(self.parse_ident(b"ull"));
visitor.visit_unit()
}
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_unit(visitor)
}
#[inline]
fn deserialize_newtype_struct<V>(self, name: &str, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
#[cfg(feature = "raw_value")]
{
if name == ::raw::TOKEN {
return self.deserialize_raw_value(visitor);
}
}
let _ = name;
visitor.visit_newtype_struct(self)
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match try!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'[' => {
self.remaining_depth -= 1;
if self.remaining_depth == 0 {
return Err(self.peek_error(ErrorCode::RecursionLimitExceeded));
}
self.eat_char();
let ret = visitor.visit_seq(SeqAccess::new(self));
self.remaining_depth += 1;
match (ret, self.end_seq()) {
(Ok(ret), Ok(())) => Ok(ret),
(Err(err), _) | (_, Err(err)) => Err(err),
}
}
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_seq(visitor)
}
fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
_len: usize,
visitor: V,
) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_seq(visitor)
}
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match try!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'{' => {
self.remaining_depth -= 1;
if self.remaining_depth == 0 {
return Err(self.peek_error(ErrorCode::RecursionLimitExceeded));
}
self.eat_char();
let ret = visitor.visit_map(MapAccess::new(self));
self.remaining_depth += 1;
match (ret, self.end_map()) {
(Ok(ret), Ok(())) => Ok(ret),
(Err(err), _) | (_, Err(err)) => Err(err),
}
}
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
fn deserialize_struct<V>(
self,
_name: &'static str,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let peek = match try!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let value = match peek {
b'[' => {
self.remaining_depth -= 1;
if self.remaining_depth == 0 {
return Err(self.peek_error(ErrorCode::RecursionLimitExceeded));
}
self.eat_char();
let ret = visitor.visit_seq(SeqAccess::new(self));
self.remaining_depth += 1;
match (ret, self.end_seq()) {
(Ok(ret), Ok(())) => Ok(ret),
(Err(err), _) | (_, Err(err)) => Err(err),
}
}
b'{' => {
self.remaining_depth -= 1;
if self.remaining_depth == 0 {
return Err(self.peek_error(ErrorCode::RecursionLimitExceeded));
}
self.eat_char();
let ret = visitor.visit_map(MapAccess::new(self));
self.remaining_depth += 1;
match (ret, self.end_map()) {
(Ok(ret), Ok(())) => Ok(ret),
(Err(err), _) | (_, Err(err)) => Err(err),
}
}
_ => Err(self.peek_invalid_type(&visitor)),
};
match value {
Ok(value) => Ok(value),
Err(err) => Err(self.fix_position(err)),
}
}
#[inline]
fn deserialize_enum<V>(
self,
_name: &str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
match try!(self.parse_whitespace()) {
Some(b'{') => {
self.remaining_depth -= 1;
if self.remaining_depth == 0 {
return Err(self.peek_error(ErrorCode::RecursionLimitExceeded));
}
self.eat_char();
let value = try!(visitor.visit_enum(VariantAccess::new(self)));
self.remaining_depth += 1;
match try!(self.parse_whitespace()) {
Some(b'}') => {
self.eat_char();
Ok(value)
}
Some(_) => Err(self.error(ErrorCode::ExpectedSomeValue)),
None => Err(self.error(ErrorCode::EofWhileParsingObject)),
}
}
Some(b'"') => visitor.visit_enum(UnitVariantAccess::new(self)),
Some(_) => Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
None => Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
}
}
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_str(visitor)
}
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
try!(self.ignore_value());
visitor.visit_unit()
}
}
struct SeqAccess<'a, R: 'a> {
de: &'a mut Deserializer<R>,
first: bool,
}
impl<'a, R: 'a> SeqAccess<'a, R> {
fn new(de: &'a mut Deserializer<R>) -> Self {
SeqAccess {
de: de,
first: true,
}
}
}
impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
type Error = Error;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
where
T: de::DeserializeSeed<'de>,
{
let peek = match try!(self.de.parse_whitespace()) {
Some(b']') => {
return Ok(None);
}
Some(b',') if !self.first => {
self.de.eat_char();
try!(self.de.parse_whitespace())
}
Some(b) => {
if self.first {
self.first = false;
Some(b)
} else {
return Err(self.de.peek_error(ErrorCode::ExpectedListCommaOrEnd));
}
}
None => {
return Err(self.de.peek_error(ErrorCode::EofWhileParsingList));
}
};
match peek {
Some(b']') => Err(self.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Ok(Some(try!(seed.deserialize(&mut *self.de)))),
None => Err(self.de.peek_error(ErrorCode::EofWhileParsingValue)),
}
}
}
struct MapAccess<'a, R: 'a> {
de: &'a mut Deserializer<R>,
first: bool,
}
impl<'a, R: 'a> MapAccess<'a, R> {
fn new(de: &'a mut Deserializer<R>) -> Self {
MapAccess {
de: de,
first: true,
}
}
}
impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
type Error = Error;
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
where
K: de::DeserializeSeed<'de>,
{
let peek = match try!(self.de.parse_whitespace()) {
Some(b'}') => {
return Ok(None);
}
Some(b',') if !self.first => {
self.de.eat_char();
try!(self.de.parse_whitespace())
}
Some(b) => {
if self.first {
self.first = false;
Some(b)
} else {
return Err(self.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd));
}
}
None => {
return Err(self.de.peek_error(ErrorCode::EofWhileParsingObject));
}
};
match peek {
Some(b'"') => seed.deserialize(MapKey { de: &mut *self.de }).map(Some),
Some(b'}') => Err(self.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Err(self.de.peek_error(ErrorCode::KeyMustBeAString)),
None => Err(self.de.peek_error(ErrorCode::EofWhileParsingValue)),
}
}
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
where
V: de::DeserializeSeed<'de>,
{
try!(self.de.parse_object_colon());
seed.deserialize(&mut *self.de)
}
}
struct VariantAccess<'a, R: 'a> {
de: &'a mut Deserializer<R>,
}
impl<'a, R: 'a> VariantAccess<'a, R> {
fn new(de: &'a mut Deserializer<R>) -> Self {
VariantAccess { de: de }
}
}
impl<'de, 'a, R: Read<'de> + 'a> de::EnumAccess<'de> for VariantAccess<'a, R> {
type Error = Error;
type Variant = Self;
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)>
where
V: de::DeserializeSeed<'de>,
{
let val = try!(seed.deserialize(&mut *self.de));
try!(self.de.parse_object_colon());
Ok((val, self))
}
}
impl<'de, 'a, R: Read<'de> + 'a> de::VariantAccess<'de> for VariantAccess<'a, R> {
type Error = Error;
fn unit_variant(self) -> Result<()> {
de::Deserialize::deserialize(self.de)
}
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
where
T: de::DeserializeSeed<'de>,
{
seed.deserialize(self.de)
}
fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
de::Deserializer::deserialize_seq(self.de, visitor)
}
fn struct_variant<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
de::Deserializer::deserialize_struct(self.de, "", fields, visitor)
}
}
struct UnitVariantAccess<'a, R: 'a> {
de: &'a mut Deserializer<R>,
}
impl<'a, R: 'a> UnitVariantAccess<'a, R> {
fn new(de: &'a mut Deserializer<R>) -> Self {
UnitVariantAccess { de: de }
}
}
impl<'de, 'a, R: Read<'de> + 'a> de::EnumAccess<'de> for UnitVariantAccess<'a, R> {
type Error = Error;
type Variant = Self;
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)>
where
V: de::DeserializeSeed<'de>,
{
let variant = try!(seed.deserialize(&mut *self.de));
Ok((variant, self))
}
}
impl<'de, 'a, R: Read<'de> + 'a> de::VariantAccess<'de> for UnitVariantAccess<'a, R> {
type Error = Error;
fn unit_variant(self) -> Result<()> {
Ok(())
}
fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value>
where
T: de::DeserializeSeed<'de>,
{
Err(de::Error::invalid_type(
Unexpected::UnitVariant,
&"newtype variant",
))
}
fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
Err(de::Error::invalid_type(
Unexpected::UnitVariant,
&"tuple variant",
))
}
fn struct_variant<V>(self, _fields: &'static [&'static str], _visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
Err(de::Error::invalid_type(
Unexpected::UnitVariant,
&"struct variant",
))
}
}
struct MapKey<'a, R: 'a> {
de: &'a mut Deserializer<R>,
}
macro_rules! deserialize_integer_key {
($method:ident => $visit:ident) => {
fn $method<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.de.eat_char();
self.de.scratch.clear();
let string = try!(self.de.read.parse_str(&mut self.de.scratch));
match (string.parse(), string) {
(Ok(integer), _) => visitor.$visit(integer),
(Err(_), Reference::Borrowed(s)) => visitor.visit_borrowed_str(s),
(Err(_), Reference::Copied(s)) => visitor.visit_str(s),
}
}
}
}
impl<'de, 'a, R> de::Deserializer<'de> for MapKey<'a, R>
where
R: Read<'de>,
{
type Error = Error;
#[inline]
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.de.eat_char();
self.de.scratch.clear();
match try!(self.de.read.parse_str(&mut self.de.scratch)) {
Reference::Borrowed(s) => visitor.visit_borrowed_str(s),
Reference::Copied(s) => visitor.visit_str(s),
}
}
deserialize_integer_key!(deserialize_i8 => visit_i8);
deserialize_integer_key!(deserialize_i16 => visit_i16);
deserialize_integer_key!(deserialize_i32 => visit_i32);
deserialize_integer_key!(deserialize_i64 => visit_i64);
deserialize_integer_key!(deserialize_u8 => visit_u8);
deserialize_integer_key!(deserialize_u16 => visit_u16);
deserialize_integer_key!(deserialize_u32 => visit_u32);
deserialize_integer_key!(deserialize_u64 => visit_u64);
serde_if_integer128! {
deserialize_integer_key!(deserialize_i128 => visit_i128);
deserialize_integer_key!(deserialize_u128 => visit_u128);
}
#[inline]
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
visitor.visit_some(self)
}
#[inline]
fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
visitor.visit_newtype_struct(self)
}
#[inline]
fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.de.deserialize_enum(name, variants, visitor)
}
#[inline]
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.de.deserialize_bytes(visitor)
}
#[inline]
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.de.deserialize_bytes(visitor)
}
forward_to_deserialize_any! {
bool f32 f64 char str string unit unit_struct seq tuple tuple_struct map
struct identifier ignored_any
}
}
pub struct StreamDeserializer<'de, R, T> {
de: Deserializer<R>,
offset: usize,
output: PhantomData<T>,
lifetime: PhantomData<&'de ()>,
}
impl<'de, R, T> StreamDeserializer<'de, R, T>
where
R: read::Read<'de>,
T: de::Deserialize<'de>,
{
pub fn new(read: R) -> Self {
let offset = read.byte_offset();
StreamDeserializer {
de: Deserializer::new(read),
offset: offset,
output: PhantomData,
lifetime: PhantomData,
}
}
pub fn byte_offset(&self) -> usize {
self.offset
}
fn peek_end_of_value(&mut self) -> Result<()> {
match try!(self.de.peek()) {
Some(b' ') | Some(b'\n') | Some(b'\t') | Some(b'\r') | Some(b'"') | Some(b'[')
| Some(b']') | Some(b'{') | Some(b'}') | Some(b',') | Some(b':') | None => Ok(()),
Some(_) => {
let position = self.de.read.peek_position();
Err(Error::syntax(
ErrorCode::TrailingCharacters,
position.line,
position.column,
))
}
}
}
}
impl<'de, R, T> Iterator for StreamDeserializer<'de, R, T>
where
R: Read<'de>,
T: de::Deserialize<'de>,
{
type Item = Result<T>;
fn next(&mut self) -> Option<Result<T>> {
match self.de.parse_whitespace() {
Ok(None) => {
self.offset = self.de.read.byte_offset();
None
}
Ok(Some(b)) => {
let self_delineated_value = match b {
b'[' | b'"' | b'{' => true,
_ => false,
};
self.offset = self.de.read.byte_offset();
let result = de::Deserialize::deserialize(&mut self.de);
Some(match result {
Ok(value) => {
self.offset = self.de.read.byte_offset();
if self_delineated_value {
Ok(value)
} else {
self.peek_end_of_value().map(|_| value)
}
}
Err(e) => Err(e),
})
}
Err(e) => Some(Err(e)),
}
}
}
fn from_trait<'de, R, T>(read: R) -> Result<T>
where
R: Read<'de>,
T: de::Deserialize<'de>,
{
let mut de = Deserializer::new(read);
let value = try!(de::Deserialize::deserialize(&mut de));
try!(de.end());
Ok(value)
}
pub fn from_reader<R, T>(rdr: R) -> Result<T>
where
R: io::Read,
T: de::DeserializeOwned,
{
from_trait(read::IoRead::new(rdr))
}
pub fn from_slice<'a, T>(v: &'a [u8]) -> Result<T>
where
T: de::Deserialize<'a>,
{
from_trait(read::SliceRead::new(v))
}
pub fn from_str<'a, T>(s: &'a str) -> Result<T>
where
T: de::Deserialize<'a>,
{
from_trait(read::StrRead::new(s))
}