Skip to content

Commit

Permalink
Fixed serialization for serde 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
fizyk20 committed Sep 27, 2016
1 parent 69ccd7c commit 456113c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "generic-array"
version = "0.5.0"
version = "0.5.1"
authors = [ "Bartłomiej Kamiński <[email protected]>" ]
description = "Generic types implementing functionality of arrays"
license = "MIT"
Expand Down
9 changes: 6 additions & 3 deletions src/impl_serde.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::ser::impls::SeqIteratorVisitor;
use serde::de::impls::VecVisitor;
use {ArrayLength, GenericArray};

Expand All @@ -10,7 +9,11 @@ impl<T, N> Serialize for GenericArray<T, N>
where S: Serializer,
{
// serializes this array just like a slice or a vector
serializer.serialize_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len())))
let mut state = try!(serializer.serialize_seq(Some(N::to_usize())));
for e in self.iter() {
try!(serializer.serialize_seq_elt(&mut state, e));
}
serializer.serialize_seq_end(state)
}
}

Expand All @@ -24,4 +27,4 @@ impl<T, N> Deserialize for GenericArray<T, N>
// TODO: write a better 'allocationless' version
deserializer.deserialize_seq(VecVisitor::new()).map(|vec| GenericArray::from_slice(&vec))
}
}
}
2 changes: 1 addition & 1 deletion tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ mod impl_serde {
fn test_serde_implementation() {
let array: GenericArray<f64, U6> = arr![f64; 0.0, 5.0, 3.0, 7.07192, 76.0, -9.0];
let string = serde_json::to_string(&array).unwrap();
assert_eq!(string, "[0,5,3,7.07192,76,-9]");
assert_eq!(string, "[0.0,5.0,3.0,7.07192,76.0,-9.0]");

let test_array: GenericArray<f64, U6> = serde_json::from_str(&string).unwrap();
assert_eq!(test_array, array);
Expand Down

0 comments on commit 456113c

Please sign in to comment.