mirror of
https://github.com/serde-rs/serde.git
synced 2025-10-02 23:35:26 +00:00
Implement deserializing json options
This commit is contained in:
parent
674983e5b7
commit
77a10caf34
27
json.rs
27
json.rs
@ -1164,6 +1164,7 @@ pub struct Parser<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Iterator<char>> Iterator<Result<de::Token, ParserError>> for Parser<T> {
|
impl<T: Iterator<char>> Iterator<Result<de::Token, ParserError>> for Parser<T> {
|
||||||
|
#[inline]
|
||||||
fn next(&mut self) -> Option<Result<de::Token, ParserError>> {
|
fn next(&mut self) -> Option<Result<de::Token, ParserError>> {
|
||||||
let state = match self.state.pop() {
|
let state = match self.state.pop() {
|
||||||
Some(state) => state,
|
Some(state) => state,
|
||||||
@ -1600,6 +1601,20 @@ impl<T: Iterator<char>> de::Deserializer<ParserError> for Parser<T> {
|
|||||||
fn syntax_error(&self) -> ParserError {
|
fn syntax_error(&self) -> ParserError {
|
||||||
SyntaxError(InvalidSyntax, self.line, self.col)
|
SyntaxError(InvalidSyntax, self.line, self.col)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Special case treating options as a nullable value.
|
||||||
|
#[inline]
|
||||||
|
fn expect_option<
|
||||||
|
U: de::Deserializable<ParserError, Parser<T>>
|
||||||
|
>(&mut self, token: de::Token) -> Result<Option<U>, ParserError> {
|
||||||
|
match token {
|
||||||
|
de::Null => Ok(None),
|
||||||
|
token => {
|
||||||
|
let value: U = try!(de::Deserializable::deserialize_token(self, token));
|
||||||
|
Ok(Some(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -2999,18 +3014,16 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_decode_option() {
|
fn test_decode_option() {
|
||||||
let mut decoder = Decoder::new(from_str("null").unwrap());
|
let value: Result<Option<String>, ParserError> = from_iter("null".chars());
|
||||||
let value: Option<String> = Decodable::decode(&mut decoder).unwrap();
|
assert_eq!(value, Ok(None));
|
||||||
assert_eq!(value, None);
|
|
||||||
|
|
||||||
let mut decoder = Decoder::new(from_str("\"jodhpurs\"").unwrap());
|
let value: Result<Option<String>, ParserError> = from_iter("\"jodhpurs\"".chars());
|
||||||
let value: Option<String> = Decodable::decode(&mut decoder).unwrap();
|
assert_eq!(value, Ok(Some("jodhpurs".to_strbuf())));
|
||||||
assert_eq!(value, Some("jodhpurs".to_strbuf()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
#[test]
|
#[test]
|
||||||
fn test_decode_enum() {
|
fn test_decode_enum() {
|
||||||
let mut decoder = Decoder::new(from_str("\"Dog\"").unwrap());
|
let mut decoder = Decoder::new(from_str("\"Dog\"").unwrap());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user