mirror of
https://gitee.com/openharmony/third_party_rust_serde
synced 2024-11-23 06:50:23 +00:00
initial work with json
This commit is contained in:
parent
9bd5764574
commit
13fd782ac2
71
de.rs
71
de.rs
@ -5,7 +5,10 @@ use collections::HashMap;
|
||||
|
||||
#[deriving(Clone, Eq)]
|
||||
pub enum Token {
|
||||
Null,
|
||||
Bool(bool),
|
||||
Int(int),
|
||||
F64(f64),
|
||||
StrBuf(StrBuf),
|
||||
CollectionStart,
|
||||
CollectionSep,
|
||||
@ -17,6 +20,28 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
|
||||
fn syntax_error(&self) -> E;
|
||||
|
||||
#[inline]
|
||||
fn expect_null(&mut self) -> Result<(), E> {
|
||||
match self.next() {
|
||||
Some(Ok(Null)) => Ok(()),
|
||||
Some(Ok(CollectionStart)) => self.expect_collection_end(),
|
||||
Some(Ok(_)) => Err(self.syntax_error()),
|
||||
Some(Err(err)) => Err(err),
|
||||
None => Err(self.end_of_stream_error()),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn expect_bool(&mut self) -> Result<bool, E> {
|
||||
match self.next() {
|
||||
Some(Ok(Bool(value))) => Ok(value),
|
||||
Some(Ok(_)) => Err(self.syntax_error()),
|
||||
Some(Err(err)) => Err(err),
|
||||
None => Err(self.end_of_stream_error()),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn expect_int(&mut self) -> Result<int, E> {
|
||||
match self.next() {
|
||||
@ -27,6 +52,16 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn expect_f64(&mut self) -> Result<f64, E> {
|
||||
match self.next() {
|
||||
Some(Ok(F64(value))) => Ok(value),
|
||||
Some(Ok(_)) => Err(self.syntax_error()),
|
||||
Some(Err(err)) => Err(err),
|
||||
None => Err(self.end_of_stream_error()),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn expect_str(&mut self) -> Result<StrBuf, E> {
|
||||
match self.next() {
|
||||
@ -134,6 +169,18 @@ pub trait Deserializable<E, D: Deserializer<E>> {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<
|
||||
E,
|
||||
D: Deserializer<E>
|
||||
> Deserializable<E, D> for bool {
|
||||
#[inline]
|
||||
fn deserialize(d: &mut D) -> Result<bool, E> {
|
||||
d.expect_bool()
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<
|
||||
E,
|
||||
D: Deserializer<E>
|
||||
@ -146,6 +193,18 @@ impl<
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<
|
||||
E,
|
||||
D: Deserializer<E>
|
||||
> Deserializable<E, D> for f64 {
|
||||
#[inline]
|
||||
fn deserialize(d: &mut D) -> Result<f64, E> {
|
||||
d.expect_f64()
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<
|
||||
E,
|
||||
D: Deserializer<E>
|
||||
@ -203,10 +262,7 @@ impl<
|
||||
> Deserializable<E, D> for () {
|
||||
#[inline]
|
||||
fn deserialize(d: &mut D) -> Result<(), E> {
|
||||
try!(d.expect_collection_start());
|
||||
try!(d.expect_collection_end());
|
||||
|
||||
Ok(())
|
||||
d.expect_null()
|
||||
}
|
||||
}
|
||||
|
||||
@ -282,13 +338,12 @@ impl<'a, A, B, T: Iterator<A>> Iterator<B> for Batch<'a, A, B, T> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
extern crate collections;
|
||||
extern crate serialize;
|
||||
extern crate test;
|
||||
|
||||
use std::vec;
|
||||
use self::collections::HashMap;
|
||||
use self::test::Bencher;
|
||||
use collections::HashMap;
|
||||
use test::Bencher;
|
||||
|
||||
use self::serialize::{Decoder, Decodable};
|
||||
|
||||
use super::{Token, Int, StrBuf, CollectionStart, CollectionSep, CollectionEnd};
|
||||
|
Loading…
Reference in New Issue
Block a user