ndarray
ndarray copied to clipboard
How to use serde with ndarray to parse json?
cargo:
[dependencies]
ndarray = {version = "0.15", features = ["serde", "rayon"]}
serde_json = "1.0"
serde = {version = "1.0", features = ["derive"]}
#[derive(Serialize, Deserialize, Debug)]
struct myst{
array: Array2<f32>,
}
#[test]
fn test_json() {
let pathbuf = PathBuf::from("/path/to/test.json");
let mut file = File::open(pathbuf).unwrap();
let mut data = String::new();
match file.read_to_string(&mut data) {
Err(e) => panic!("{}",e),
Ok(_) => {}
}
let v: myst = serde_json::from_str(&data).unwrap();
println!("{:?}", v);
}
the json is
{
"array": [
[1,2,3],
[4,5,6]]
}
but I get error:
thread 'tests::test_json' panicked at 'called
Result::unwrap()
on anErr
value: Error("invalid type: sequence, expected u8", line: 3, column: 4)'
try this in your json file:
{
"array": {
"v":1,
"dim":[2,3],
"data":[1,2,3,4,5,6]
}}
ndarray is not designed for parsing json, and doesn't do it. Our serialization is generic for serde and its design is to roundtrip to whatever backend/serializer that you use with serde. So there is no design particular to json and I would not recommend relying on any particular json format to be compatible with ndarray, except for files produced using ndarray itself.
try this in your json file:
{ "array": { "v":1, "dim":[2,3], "data":[1,2,3,4,5,6] }}
It is so helpful to me.
I needed this too, so I implemented and published a crate that does just that - parses multi-dimensional arrays from self-describing sources like JSON, while determining and validating the shape from the input array alone: https://github.com/RReverser/serde-ndim
Closing this issue, as bluss made it clear that ndarray is not designed for that shoudn't be used like that.