ndarray-vision icon indicating copy to clipboard operation
ndarray-vision copied to clipboard

Added serde support for `Image` type

Open volks73 opened this issue 1 year ago • 3 comments

Resolves #54.

A feature flag still needs to be added, but I wanted to share what I have so far to make sure I am on the right track.

I am not sure if this is a good implementation because I have added (de)serialization of the colour model as a string but during deserialization is actually never used. The colour model must be known ahead of time and used for the type annotation. The type is not determined from the "model" field. See the unit test for some context.

    #[test]
    fn deserialize_image_base() {
        const EXPECTED: &str = r#"{"data":{"v":1,"dim":[2,3,3],"data":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},"model":"RGB"}"#;
        let actual: Image<u8, RGB> = serde_json::from_str(EXPECTED).expect("Deserialized image");
        assert_eq!(actual.model, PhantomData);
    }

If the model is not RGB then an error occurs instead of creating an image based on the colour model that is specified, but maybe this is correct way to do this? This is my first time really diving into manually implementing (de)serialization. Is there a way to deserialize into a "generic" Image.

volks73 avatar Jan 07 '23 21:01 volks73