Skip to content

Commit

Permalink
Update for map serialization changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Jul 28, 2016
1 parent ff7f4b3 commit dd864cb
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 24 deletions.
3 changes: 3 additions & 0 deletions json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ clippy = { version = "^0.*", optional = true }
linked-hash-map = { version = "0.1", optional = true }
itoa = "0.1"
dtoa = "0.2"

[replace]
"serde:0.8.0-rc3" = { git = "https://github.com/serde-rs/serde" }
30 changes: 22 additions & 8 deletions json/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,10 @@ impl<W, F> ser::Serializer for Serializer<W, F>
}

#[inline]
fn serialize_map_elt<K: ser::Serialize, V: ser::Serialize>(
fn serialize_map_key<T: ser::Serialize>(
&mut self,
state: &mut State,
key: K,
value: V
key: T,
) -> Result<()> {
try!(self.formatter.comma(&mut self.writer, *state == State::First));
*state = State::Rest;
Expand All @@ -365,8 +364,15 @@ impl<W, F> ser::Serializer for Serializer<W, F>
ser: self,
}));

try!(self.formatter.colon(&mut self.writer));
self.formatter.colon(&mut self.writer)
}

#[inline]
fn serialize_map_value<T: ser::Serialize>(
&mut self,
_: &mut State,
value: T,
) -> Result<()> {
value.serialize(self)
}

Expand Down Expand Up @@ -394,7 +400,8 @@ impl<W, F> ser::Serializer for Serializer<W, F>
key: &'static str,
value: V
) -> Result<()> {
self.serialize_map_elt(state, key, value)
try!(self.serialize_map_key(state, key));
self.serialize_map_value(state, value)
}

#[inline]
Expand Down Expand Up @@ -650,11 +657,18 @@ impl<'a, W, F> ser::Serializer for MapKeySerializer<'a, W, F>
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}

fn serialize_map_elt<K: ser::Serialize, V: ser::Serialize>(
fn serialize_map_key<T: ser::Serialize>(
&mut self,
_state: &mut (),
_key: K,
_value: V
_key: T,
) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}

fn serialize_map_value<T: ser::Serialize>(
&mut self,
_state: &mut (),
_value: T,
) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
Expand Down
55 changes: 40 additions & 15 deletions json/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,15 +558,21 @@ pub struct StructVariantState {
map: Map<String, Value>,
}

#[doc(hidden)]
pub struct MapState {
map: Map<String, Value>,
next_key: Option<String>,
}

impl ser::Serializer for Serializer {
type Error = Error;

type SeqState = Vec<Value>;
type TupleState = Vec<Value>;
type TupleStructState = Vec<Value>;
type TupleVariantState = TupleVariantState;
type MapState = Map<String, Value>;
type StructState = Map<String, Value>;
type MapState = MapState;
type StructState = MapState;
type StructVariantState = StructVariantState;

#[inline]
Expand Down Expand Up @@ -829,51 +835,70 @@ impl ser::Serializer for Serializer {
fn serialize_map(
&mut self,
_len: Option<usize>
) -> Result<Map<String, Value>, Error> {
Ok(Map::new())
) -> Result<MapState, Error> {
Ok(MapState {
map: Map::new(),
next_key: None,
})
}

fn serialize_map_elt<K: ser::Serialize, V: ser::Serialize>(
fn serialize_map_key<T: ser::Serialize>(
&mut self,
state: &mut Map<String, Value>,
key: K,
value: V
state: &mut MapState,
key: T,
) -> Result<(), Error> {
match to_value(&key) {
Value::String(s) => state.insert(s, to_value(&value)),
Value::String(s) => state.next_key = Some(s),
_ => return Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)),
};
Ok(())
}

fn serialize_map_value<T: ser::Serialize>(
&mut self,
state: &mut MapState,
value: T,
) -> Result<(), Error> {
match state.next_key.take() {
Some(key) => state.map.insert(key, to_value(&value)),
None => {
return Err(Error::Syntax(ErrorCode::Custom("serialize_map_value without \
matching serialize_map_key".to_owned()),
0, 0));
}
};
Ok(())
}

fn serialize_map_end(
&mut self,
state: Map<String, Value>
state: MapState,
) -> Result<(), Error> {
self.value = Value::Object(state);
self.value = Value::Object(state.map);
Ok(())
}

fn serialize_struct(
&mut self,
_name: &'static str,
len: usize
) -> Result<Map<String, Value>, Error> {
) -> Result<MapState, Error> {
self.serialize_map(Some(len))
}

fn serialize_struct_elt<V: ser::Serialize>(
&mut self,
state: &mut Map<String, Value>,
state: &mut MapState,
key: &'static str,
value: V
) -> Result<(), Error> {
self.serialize_map_elt(state, key, value)
try!(self.serialize_map_key(state, key));
self.serialize_map_value(state, value)
}

fn serialize_struct_end(
&mut self,
state: Map<String, Value>
state: MapState
) -> Result<(), Error> {
self.serialize_map_end(state)
}
Expand Down
3 changes: 3 additions & 0 deletions json_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ path = "tests/test.rs"
[[bench]]
name = "bench"
path = "benches/bench.rs"

[replace]
"serde:0.8.0-rc3" = { git = "https://github.com/serde-rs/serde" }
3 changes: 2 additions & 1 deletion json_tests/tests/test_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,8 @@ fn test_serialize_map_with_no_len() {
{
let mut state = try!(serializer.serialize_map(None));
for (k, v) in &self.0 {
try!(serializer.serialize_map_elt(&mut state, k, v));
try!(serializer.serialize_map_key(&mut state, k));
try!(serializer.serialize_map_value(&mut state, v));
}
serializer.serialize_map_end(state)
}
Expand Down

0 comments on commit dd864cb

Please sign in to comment.