Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does "v" stand for in ndarray (de)serialize?

I am trying to understand the "v" in ndarray (de)serialize:

use ndarray::prelude::*;

pub fn example() {
    let ar2 = arr2(&[[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.],[0., 0., 0., 0., 0.],[0., 0., 0., 0., 0.]]);
    let s = serde_json::to_string(&ar2).unwrap();
    dbg!(s);
    
    let ar1 = arr1(&[[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.],[0., 0., 0., 0., 0.],[0., 0., 0., 0., 0.]]);
    let s = serde_json::to_string(&ar1).unwrap();
    dbg!(s);

    let anatoly = String::from("{\"v\":1,\"dim\":[3,3],\"data\":[1.0,1.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0]}");
    let a = serde_json::from_str::<Array2<f64>>(&anatoly).unwrap();
    dbg!(a);
}

Looking at:
https://docs.rs/ndarray/latest/src/ndarray/array_serde.rs.html#91-100
It refers to some kind of ARRAY_FORMAT_VERSION

What is this "version"?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is it always "1" (for latest versions of the lib)?

>Solution :

Yes, that field represents the version of the serialization format. At the moment that version might always be 1, but in the future the format could change. And if/when it does change, this field can be used to determine how to deserialize the data regardless of which version it came from. It also gives the option for a single version of ndarray to choose between multiple formats for a single type depending on the situation.

As an example, lets say they wanted to add an extra safety check where we embed the type of array values inside the serialized data to make sure it is deserialized to the same type.

{"v":2,"type":"f64","dim":[3,3],"data":[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0]}

However after this update is introduced, what happens to data that was serialized by the previous version? Using the version field in the data we can then tell which approach to use when reading the data and still maintain compatibility with the previous format.

Of course, that is a purely hypothetical example. As @IvanC pointed out in a comment, they likely want to implement a packed data format and the format version is a way of future proofing for when that time comes.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading