initial work with json

This commit is contained in:
Erick Tryzelaar 2014-05-16 21:21:05 -07:00
parent 9bd5764574
commit 13fd782ac2
3 changed files with 3487 additions and 8 deletions

71
de.rs
View File

@ -5,7 +5,10 @@ use collections::HashMap;
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
pub enum Token { pub enum Token {
Null,
Bool(bool),
Int(int), Int(int),
F64(f64),
StrBuf(StrBuf), StrBuf(StrBuf),
CollectionStart, CollectionStart,
CollectionSep, CollectionSep,
@ -17,6 +20,28 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
fn syntax_error(&self) -> 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] #[inline]
fn expect_int(&mut self) -> Result<int, E> { fn expect_int(&mut self) -> Result<int, E> {
match self.next() { 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] #[inline]
fn expect_str(&mut self) -> Result<StrBuf, E> { fn expect_str(&mut self) -> Result<StrBuf, E> {
match self.next() { 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< impl<
E, E,
D: Deserializer<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< impl<
E, E,
D: Deserializer<E> D: Deserializer<E>
@ -203,10 +262,7 @@ impl<
> Deserializable<E, D> for () { > Deserializable<E, D> for () {
#[inline] #[inline]
fn deserialize(d: &mut D) -> Result<(), E> { fn deserialize(d: &mut D) -> Result<(), E> {
try!(d.expect_collection_start()); d.expect_null()
try!(d.expect_collection_end());
Ok(())
} }
} }
@ -282,13 +338,12 @@ impl<'a, A, B, T: Iterator<A>> Iterator<B> for Batch<'a, A, B, T> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
extern crate collections;
extern crate serialize; extern crate serialize;
extern crate test;
use std::vec; use std::vec;
use self::collections::HashMap; use collections::HashMap;
use self::test::Bencher; use test::Bencher;
use self::serialize::{Decoder, Decodable}; use self::serialize::{Decoder, Decodable};
use super::{Token, Int, StrBuf, CollectionStart, CollectionSep, CollectionEnd}; use super::{Token, Int, StrBuf, CollectionStart, CollectionSep, CollectionEnd};

3412
json.rs Normal file

File diff suppressed because it is too large Load Diff

12
serde.rs Normal file
View File

@ -0,0 +1,12 @@
#![feature(macro_rules, phase)]
extern crate collections;
// test harness access
#[cfg(test)]
extern crate test;
#[phase(syntax, link)]
extern crate log;
pub mod de;
pub mod json;