Fix serializing json objects

This commit is contained in:
Erick Tryzelaar 2015-03-03 21:55:48 -08:00
parent 6382441f2e
commit b2f52df5ff
2 changed files with 25 additions and 25 deletions

View File

@ -8,7 +8,7 @@ use de;
use ser; use ser;
use super::error::Error; use super::error::Error;
#[derive(PartialEq)] #[derive(Clone, PartialEq)]
pub enum Value { pub enum Value {
Null, Null,
Bool(bool), Bool(bool),
@ -61,6 +61,7 @@ impl fmt::Debug for Value {
} }
} }
#[derive(Debug)]
enum State { enum State {
Value(Value), Value(Value),
Array(Vec<Value>), Array(Vec<Value>),
@ -227,21 +228,22 @@ impl ser::Visitor for Serializer {
V: ser::Serialize, V: ser::Serialize,
{ {
try!(key.visit(self)); try!(key.visit(self));
try!(value.visit(self));
let key = match self.state.pop().unwrap() { let key = match self.state.pop().unwrap() {
State::Value(Value::String(value)) => value, State::Value(Value::String(value)) => value,
_ => panic!(), state => panic!("expected key, found {:?}", state),
}; };
try!(value.visit(self));
let value = match self.state.pop().unwrap() { let value = match self.state.pop().unwrap() {
State::Value(value) => value, State::Value(value) => value,
_ => panic!(), state => panic!("expected value, found {:?}", state),
}; };
match *self.state.last_mut().unwrap() { match *self.state.last_mut().unwrap() {
State::Object(ref mut values) => { values.insert(key, value); } State::Object(ref mut values) => { values.insert(key, value); }
_ => panic!(), ref state => panic!("expected object, found {:?}", state),
} }
Ok(()) Ok(())

View File

@ -5,8 +5,6 @@ extern crate test;
extern crate serde2; extern crate serde2;
use std::fmt::Debug; use std::fmt::Debug;
use std::io;
use std::str;
use std::string; use std::string;
use std::collections::BTreeMap; use std::collections::BTreeMap;
@ -115,13 +113,12 @@ fn test_write_null() {
//test_pretty_encode_ok(tests); //test_pretty_encode_ok(tests);
} }
/*
#[test] #[test]
fn test_write_i64() { fn test_write_i64() {
let tests = &[ let tests = &[
(3is, "3"), (3i64, "3"),
(-2is, "-2"), (-2i64, "-2"),
(-1234is, "-1234"), (-1234i64, "-1234"),
]; ];
test_encode_ok(tests); test_encode_ok(tests);
//test_pretty_encode_ok(tests); //test_pretty_encode_ok(tests);
@ -130,7 +127,7 @@ fn test_write_i64() {
#[test] #[test]
fn test_write_f64() { fn test_write_f64() {
let tests = &[ let tests = &[
(3.0f64, "3"), (3.0, "3"),
(3.1, "3.1"), (3.1, "3.1"),
(-1.5, "-1.5"), (-1.5, "-1.5"),
(0.5, "0.5"), (0.5, "0.5"),
@ -306,7 +303,7 @@ fn test_write_object() {
fn test_write_tuple() { fn test_write_tuple() {
test_encode_ok(&[ test_encode_ok(&[
( (
(5is,), (5,),
"[5]", "[5]",
), ),
]); ]);
@ -314,7 +311,7 @@ fn test_write_tuple() {
/* /*
test_pretty_encode_ok(&[ test_pretty_encode_ok(&[
( (
(5is,), (5,),
concat!( concat!(
"[\n", "[\n",
" 5\n", " 5\n",
@ -326,7 +323,7 @@ fn test_write_tuple() {
test_encode_ok(&[ test_encode_ok(&[
( (
(5is, (6is, "abc")), (5, (6, "abc")),
"[5,[6,\"abc\"]]", "[5,[6,\"abc\"]]",
), ),
]); ]);
@ -334,7 +331,7 @@ fn test_write_tuple() {
/* /*
test_pretty_encode_ok(&[ test_pretty_encode_ok(&[
( (
(5is, (6is, "abc")), (5, (6, "abc")),
concat!( concat!(
"[\n", "[\n",
" 5,\n", " 5,\n",
@ -349,6 +346,7 @@ fn test_write_tuple() {
*/ */
} }
/*
#[test] #[test]
fn test_write_enum() { fn test_write_enum() {
test_encode_ok(&[ test_encode_ok(&[
@ -634,8 +632,8 @@ fn test_parse_list() {
]); ]);
test_parse_ok(&[ test_parse_ok(&[
("[3,1]", vec!(3is, 1)), ("[3,1]", vec!(3, 1)),
("[ 3 , 1 ]", vec!(3is, 1)), ("[ 3 , 1 ]", vec!(3, 1)),
]); ]);
test_parse_ok(&[ test_parse_ok(&[
@ -662,7 +660,7 @@ fn test_json_deserialize_list() {
]); ]);
test_json_deserialize_ok(&[ test_json_deserialize_ok(&[
vec!(3is, 1), vec!(3, 1),
]); ]);
test_json_deserialize_ok(&[ test_json_deserialize_ok(&[
@ -700,18 +698,18 @@ fn test_parse_object() {
), ),
( (
"{\"a\":3,\"b\":4}", "{\"a\":3,\"b\":4}",
treemap!("a".to_string() => 3is, "b".to_string() => 4) treemap!("a".to_string() => 3, "b".to_string() => 4)
), ),
( (
"{ \"a\" : 3 , \"b\" : 4 }", "{ \"a\" : 3 , \"b\" : 4 }",
treemap!("a".to_string() => 3is, "b".to_string() => 4), treemap!("a".to_string() => 3, "b".to_string() => 4),
), ),
]); ]);
test_parse_ok(&[ test_parse_ok(&[
( (
"{\"a\": {\"b\": 3, \"c\": 4}}", "{\"a\": {\"b\": 3, \"c\": 4}}",
treemap!("a".to_string() => treemap!("b".to_string() => 3is, "c".to_string() => 4is)), treemap!("a".to_string() => treemap!("b".to_string() => 3, "c".to_string() => 4is)),
), ),
]); ]);
} }
@ -720,12 +718,12 @@ fn test_parse_object() {
fn test_json_deserialize_object() { fn test_json_deserialize_object() {
test_json_deserialize_ok(&[ test_json_deserialize_ok(&[
treemap!(), treemap!(),
treemap!("a".to_string() => 3is), treemap!("a".to_string() => 3),
treemap!("a".to_string() => 3is, "b".to_string() => 4), treemap!("a".to_string() => 3, "b".to_string() => 4),
]); ]);
test_json_deserialize_ok(&[ test_json_deserialize_ok(&[
treemap!("a".to_string() => treemap!("b".to_string() => 3is, "c".to_string() => 4)), treemap!("a".to_string() => treemap!("b".to_string() => 3, "c".to_string() => 4)),
]); ]);
} }