ndarray icon indicating copy to clipboard operation
ndarray copied to clipboard

How to use serde with ndarray to parse json?

Open wangm23456 opened this issue 2 years ago • 3 comments

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 an Err value: Error("invalid type: sequence, expected u8", line: 3, column: 4)'

wangm23456 avatar May 24 '22 09:05 wangm23456

try this in your json file:

{
"array": {
    "v":1,
    "dim":[2,3],
    "data":[1,2,3,4,5,6]
}}

bg5hfc avatar May 24 '22 10:05 bg5hfc

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.

bluss avatar May 24 '22 15:05 bluss

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.

wangm23456 avatar May 25 '22 12:05 wangm23456

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

RReverser avatar Apr 02 '23 16:04 RReverser

Closing this issue, as bluss made it clear that ndarray is not designed for that shoudn't be used like that.

nilgoyette avatar May 28 '23 17:05 nilgoyette