mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-05-01 05:28:00 +00:00
Update to rust HEAD
This commit is contained in:
@@ -1994,20 +1994,6 @@ pub fn from_json<
|
|||||||
de::Deserializable::deserialize(&mut d)
|
de::Deserializable::deserialize(&mut d)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A structure to decode JSON to values in rust.
|
|
||||||
pub struct Decoder {
|
|
||||||
stack: Vec<Json>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder {
|
|
||||||
/// Creates a new decoder instance for decoding the specified JSON value.
|
|
||||||
pub fn new(json: Json) -> Decoder {
|
|
||||||
Decoder {
|
|
||||||
stack: vec!(json),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! expect(
|
macro_rules! expect(
|
||||||
($e:expr, Null) => ({
|
($e:expr, Null) => ({
|
||||||
match $e {
|
match $e {
|
||||||
@@ -2027,257 +2013,6 @@ macro_rules! expect(
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
|
||||||
impl ::Decoder<DecoderError> for Decoder {
|
|
||||||
|
|
||||||
fn read_nil(&mut self) -> DecodeResult<()> {
|
|
||||||
debug!("read_nil");
|
|
||||||
try!(expect!(self.pop(), Null));
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_u64(&mut self) -> DecodeResult<u64 > { Ok(try!(self.read_f64()) as u64) }
|
|
||||||
fn read_u32(&mut self) -> DecodeResult<u32 > { Ok(try!(self.read_f64()) as u32) }
|
|
||||||
fn read_u16(&mut self) -> DecodeResult<u16 > { Ok(try!(self.read_f64()) as u16) }
|
|
||||||
fn read_u8 (&mut self) -> DecodeResult<u8 > { Ok(try!(self.read_f64()) as u8) }
|
|
||||||
fn read_uint(&mut self) -> DecodeResult<uint> { Ok(try!(self.read_f64()) as uint) }
|
|
||||||
|
|
||||||
fn read_i64(&mut self) -> DecodeResult<i64> { Ok(try!(self.read_f64()) as i64) }
|
|
||||||
fn read_i32(&mut self) -> DecodeResult<i32> { Ok(try!(self.read_f64()) as i32) }
|
|
||||||
fn read_i16(&mut self) -> DecodeResult<i16> { Ok(try!(self.read_f64()) as i16) }
|
|
||||||
fn read_i8 (&mut self) -> DecodeResult<i8 > { Ok(try!(self.read_f64()) as i8) }
|
|
||||||
fn read_int(&mut self) -> DecodeResult<int> { Ok(try!(self.read_f64()) as int) }
|
|
||||||
|
|
||||||
fn read_bool(&mut self) -> DecodeResult<bool> {
|
|
||||||
debug!("read_bool");
|
|
||||||
Ok(try!(expect!(self.pop(), Boolean)))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_f64(&mut self) -> DecodeResult<f64> {
|
|
||||||
use std::from_str::FromStr;
|
|
||||||
debug!("read_f64");
|
|
||||||
match self.pop() {
|
|
||||||
Number(f) => Ok(f),
|
|
||||||
String(s) => {
|
|
||||||
// re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc)
|
|
||||||
// is going to have a string here, as per JSON spec..
|
|
||||||
Ok(FromStr::from_str(s.as_slice()).unwrap())
|
|
||||||
},
|
|
||||||
value => {
|
|
||||||
Err(ExpectedError("Number".to_string(),
|
|
||||||
format!("{}", value)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_f32(&mut self) -> DecodeResult<f32> { Ok(try!(self.read_f64()) as f32) }
|
|
||||||
|
|
||||||
fn read_char(&mut self) -> DecodeResult<char> {
|
|
||||||
let s = try!(self.read_str());
|
|
||||||
{
|
|
||||||
let mut it = s.as_slice().chars();
|
|
||||||
match (it.next(), it.next()) {
|
|
||||||
// exactly one character
|
|
||||||
(Some(c), None) => return Ok(c),
|
|
||||||
_ => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(ExpectedError("single character string".to_string(),
|
|
||||||
format!("{}", s)))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_str(&mut self) -> DecodeResult<String> {
|
|
||||||
debug!("read_str");
|
|
||||||
Ok(try!(expect!(self.pop(), String)))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_enum<T>(&mut self,
|
|
||||||
name: &str,
|
|
||||||
f: |&mut Decoder| -> DecodeResult<T>) -> DecodeResult<T> {
|
|
||||||
debug!("read_enum({})", name);
|
|
||||||
f(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_enum_variant<T>(&mut self,
|
|
||||||
names: &[&str],
|
|
||||||
f: |&mut Decoder, uint| -> DecodeResult<T>)
|
|
||||||
-> DecodeResult<T> {
|
|
||||||
debug!("read_enum_variant(names={:?})", names);
|
|
||||||
let name = match self.pop() {
|
|
||||||
String(s) => s,
|
|
||||||
Object(mut o) => {
|
|
||||||
let n = match o.pop(&"variant".to_string()) {
|
|
||||||
Some(String(s)) => s,
|
|
||||||
Some(val) => {
|
|
||||||
return Err(ExpectedError("String".to_string(),
|
|
||||||
format!("{}", val)))
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
return Err(MissingFieldError("variant".to_string()))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
match o.pop(&"fields".to_string()) {
|
|
||||||
Some(List(l)) => {
|
|
||||||
for field in l.move_iter().rev() {
|
|
||||||
self.stack.push(field.clone());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Some(val) => {
|
|
||||||
return Err(ExpectedError("List".to_string(),
|
|
||||||
format!("{}", val)))
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
return Err(MissingFieldError("fields".to_string()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
n
|
|
||||||
}
|
|
||||||
json => {
|
|
||||||
return Err(ExpectedError("String or Object".to_string(),
|
|
||||||
format!("{}", json)))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let idx = match names.iter()
|
|
||||||
.position(|n| {
|
|
||||||
str::eq_slice(*n, name.as_slice())
|
|
||||||
}) {
|
|
||||||
Some(idx) => idx,
|
|
||||||
None => return Err(UnknownVariantError(name))
|
|
||||||
};
|
|
||||||
f(self, idx)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_enum_variant_arg<T>(&mut self, idx: uint, f: |&mut Decoder| -> DecodeResult<T>)
|
|
||||||
-> DecodeResult<T> {
|
|
||||||
debug!("read_enum_variant_arg(idx={})", idx);
|
|
||||||
f(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_enum_struct_variant<T>(&mut self,
|
|
||||||
names: &[&str],
|
|
||||||
f: |&mut Decoder, uint| -> DecodeResult<T>)
|
|
||||||
-> DecodeResult<T> {
|
|
||||||
debug!("read_enum_struct_variant(names={:?})", names);
|
|
||||||
self.read_enum_variant(names, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn read_enum_struct_variant_field<T>(&mut self,
|
|
||||||
name: &str,
|
|
||||||
idx: uint,
|
|
||||||
f: |&mut Decoder| -> DecodeResult<T>)
|
|
||||||
-> DecodeResult<T> {
|
|
||||||
debug!("read_enum_struct_variant_field(name={}, idx={})", name, idx);
|
|
||||||
self.read_enum_variant_arg(idx, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_struct<T>(&mut self,
|
|
||||||
name: &str,
|
|
||||||
len: uint,
|
|
||||||
f: |&mut Decoder| -> DecodeResult<T>)
|
|
||||||
-> DecodeResult<T> {
|
|
||||||
debug!("read_struct(name={}, len={})", name, len);
|
|
||||||
let value = try!(f(self));
|
|
||||||
self.pop();
|
|
||||||
Ok(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_struct_field<T>(&mut self,
|
|
||||||
name: &str,
|
|
||||||
idx: uint,
|
|
||||||
f: |&mut Decoder| -> DecodeResult<T>)
|
|
||||||
-> DecodeResult<T> {
|
|
||||||
debug!("read_struct_field(name={}, idx={})", name, idx);
|
|
||||||
let mut obj = try!(expect!(self.pop(), Object));
|
|
||||||
|
|
||||||
let value = match obj.pop(&name.to_string()) {
|
|
||||||
None => return Err(MissingFieldError(name.to_string())),
|
|
||||||
Some(json) => {
|
|
||||||
self.stack.push(json);
|
|
||||||
try!(f(self))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
self.stack.push(Object(obj));
|
|
||||||
Ok(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_tuple<T>(&mut self, f: |&mut Decoder, uint| -> DecodeResult<T>) -> DecodeResult<T> {
|
|
||||||
debug!("read_tuple()");
|
|
||||||
self.read_seq(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_tuple_arg<T>(&mut self,
|
|
||||||
idx: uint,
|
|
||||||
f: |&mut Decoder| -> DecodeResult<T>) -> DecodeResult<T> {
|
|
||||||
debug!("read_tuple_arg(idx={})", idx);
|
|
||||||
self.read_seq_elt(idx, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_tuple_struct<T>(&mut self,
|
|
||||||
name: &str,
|
|
||||||
f: |&mut Decoder, uint| -> DecodeResult<T>)
|
|
||||||
-> DecodeResult<T> {
|
|
||||||
debug!("read_tuple_struct(name={})", name);
|
|
||||||
self.read_tuple(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_tuple_struct_arg<T>(&mut self,
|
|
||||||
idx: uint,
|
|
||||||
f: |&mut Decoder| -> DecodeResult<T>)
|
|
||||||
-> DecodeResult<T> {
|
|
||||||
debug!("read_tuple_struct_arg(idx={})", idx);
|
|
||||||
self.read_tuple_arg(idx, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_option<T>(&mut self, f: |&mut Decoder, bool| -> DecodeResult<T>) -> DecodeResult<T> {
|
|
||||||
match self.pop() {
|
|
||||||
Null => f(self, false),
|
|
||||||
value => { self.stack.push(value); f(self, true) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_seq<T>(&mut self, f: |&mut Decoder, uint| -> DecodeResult<T>) -> DecodeResult<T> {
|
|
||||||
debug!("read_seq()");
|
|
||||||
let list = try!(expect!(self.pop(), List));
|
|
||||||
let len = list.len();
|
|
||||||
for v in list.move_iter().rev() {
|
|
||||||
self.stack.push(v);
|
|
||||||
}
|
|
||||||
f(self, len)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_seq_elt<T>(&mut self,
|
|
||||||
idx: uint,
|
|
||||||
f: |&mut Decoder| -> DecodeResult<T>) -> DecodeResult<T> {
|
|
||||||
debug!("read_seq_elt(idx={})", idx);
|
|
||||||
f(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_map<T>(&mut self, f: |&mut Decoder, uint| -> DecodeResult<T>) -> DecodeResult<T> {
|
|
||||||
debug!("read_map()");
|
|
||||||
let obj = try!(expect!(self.pop(), Object));
|
|
||||||
let len = obj.len();
|
|
||||||
for (key, value) in obj.move_iter() {
|
|
||||||
self.stack.push(value);
|
|
||||||
self.stack.push(String(key));
|
|
||||||
}
|
|
||||||
f(self, len)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_map_elt_key<T>(&mut self, idx: uint, f: |&mut Decoder| -> DecodeResult<T>)
|
|
||||||
-> DecodeResult<T> {
|
|
||||||
debug!("read_map_elt_key(idx={})", idx);
|
|
||||||
f(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_map_elt_val<T>(&mut self, idx: uint, f: |&mut Decoder| -> DecodeResult<T>)
|
|
||||||
-> DecodeResult<T> {
|
|
||||||
debug!("read_map_elt_val(idx={})", idx);
|
|
||||||
f(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/// Test if two json values are less than one another
|
/// Test if two json values are less than one another
|
||||||
impl PartialOrd for Json {
|
impl PartialOrd for Json {
|
||||||
fn lt(&self, other: &Json) -> bool {
|
fn lt(&self, other: &Json) -> bool {
|
||||||
@@ -2428,10 +2163,6 @@ impl<A:ToJson,B:ToJson,C:ToJson> ToJson for (A, B, C) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A:ToJson> ToJson for ~[A] {
|
|
||||||
fn to_json(&self) -> Json { List(self.iter().map(|elt| elt.to_json()).collect()) }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A:ToJson> ToJson for Vec<A> {
|
impl<A:ToJson> ToJson for Vec<A> {
|
||||||
fn to_json(&self) -> Json { List(self.iter().map(|elt| elt.to_json()).collect()) }
|
fn to_json(&self) -> Json { List(self.iter().map(|elt| elt.to_json()).collect()) }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user