Skip to content

Commit 683433d

Browse files
authored
Merge pull request #368 from bluss/ixdyn-serialize
implement serialization for IxDyn
2 parents 4a34572 + 9ff2060 commit 683433d

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

serialization-tests/tests/serialize.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern crate ron;
1414
use serialize::json;
1515

1616

17-
use ndarray::{arr0, arr1, arr2, RcArray, RcArray1, RcArray2};
17+
use ndarray::{arr0, arr1, arr2, RcArray, RcArray1, RcArray2, ArrayD, IxDyn};
1818

1919
#[test]
2020
fn serial_many_dim()
@@ -123,6 +123,46 @@ fn serial_many_dim_serde()
123123
}
124124
}
125125

126+
#[test]
127+
fn serial_ixdyn_serde()
128+
{
129+
{
130+
let a = arr0::<f32>(2.72).into_dyn();
131+
let serial = serde_json::to_string(&a).unwrap();
132+
println!("Serde encode {:?} => {:?}", a, serial);
133+
let res = serde_json::from_str::<RcArray<f32, _>>(&serial);
134+
println!("{:?}", res);
135+
assert_eq!(a, res.unwrap());
136+
}
137+
138+
{
139+
let a = arr1::<f32>(&[2.72, 1., 2.]).into_dyn();
140+
let serial = serde_json::to_string(&a).unwrap();
141+
println!("Serde encode {:?} => {:?}", a, serial);
142+
let res = serde_json::from_str::<ArrayD<f32>>(&serial);
143+
println!("{:?}", res);
144+
assert_eq!(a, res.unwrap());
145+
}
146+
147+
{
148+
let a = arr2(&[[3., 1., 2.2], [3.1, 4., 7.]])
149+
.into_shape(IxDyn(&[3, 1, 1, 1, 2, 1])).unwrap();
150+
let serial = serde_json::to_string(&a).unwrap();
151+
println!("Serde encode {:?} => {:?}", a, serial);
152+
let res = serde_json::from_str::<ArrayD<f32>>(&serial);
153+
println!("{:?}", res);
154+
assert_eq!(a, res.unwrap());
155+
}
156+
157+
{
158+
let a = arr2(&[[3., 1., 2.2], [3.1, 4., 7.]]).into_dyn();
159+
let text = r##"{"v":1,"dim":[2,3],"data":[3,1,2.2,3.1,4,7]}"##;
160+
let b = serde_json::from_str::<ArrayD<f32>>(text);
161+
assert_eq!(a, b.unwrap());
162+
}
163+
164+
}
165+
126166
#[test]
127167
fn serial_wrong_count_serde()
128168
{

src/array_serde.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use imp_prelude::*;
1616

1717
use super::arraytraits::ARRAY_FORMAT_VERSION;
1818
use super::Iter;
19+
use IntoDimension;
1920

2021
/// Verifies that the version of the deserialized array matches the current
2122
/// `ARRAY_FORMAT_VERSION`.
@@ -52,6 +53,27 @@ impl<'de, I> Deserialize<'de> for Dim<I>
5253
}
5354
}
5455

56+
/// **Requires crate feature `"serde-1"`**
57+
impl Serialize for IxDyn
58+
{
59+
fn serialize<Se>(&self, serializer: Se) -> Result<Se::Ok, Se::Error>
60+
where Se: Serializer
61+
{
62+
self.ix().serialize(serializer)
63+
}
64+
}
65+
66+
/// **Requires crate feature `"serde-1"`**
67+
impl<'de> Deserialize<'de> for IxDyn
68+
{
69+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
70+
where D: Deserializer<'de>
71+
{
72+
let v = Vec::<Ix>::deserialize(deserializer)?;
73+
Ok(v.into_dimension())
74+
}
75+
}
76+
5577
/// **Requires crate feature `"serde-1"`**
5678
impl<A, D, S> Serialize for ArrayBase<S, D>
5779
where A: Serialize,

0 commit comments

Comments
 (0)