I'm using serde_json to deserialize data that comes in fixed-sized lists in the file, that gets reformatted into a structure. I'm currently successfully reading in the raw data, but I have to do it as a Vec, and manually check that the size is right.
Is there a way to read this in as an array? I.e., below I'm reading a Vec::, then checking that its length is 5 -- I'd rather read an [f64; 5] directly.
#![allow(unused)]
use ndarray::{Array1, Array2, Array3, Axis, array};
use serde::{Deserialize, Deserializer};
#[derive(Debug, PartialEq)]
struct Bob {
a: f64, b: f64, c: f64, d: f64, e: f64,
}
impl<'de> Deserialize<'de> for Bob {
/// Deserialize a JSON file
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
// Gee, it would be nice to read an array here
let coef = Vec::<f64>::deserialize(deserializer)?;
if coef.len() != 5 {
return Err(serde::de::Error::invalid_length(
coef.len(),
&"5")
)
}
let r = Self {
a: coef[0],
b: coef[1],
c: coef[2],
d: coef[3],
e: coef[4],
};
Ok(r)
}
}
fn main () {
let bob: Bob = serde_json::from_str(r#"[1.0, 2.0, 3.0, 4.0, 5.0]"#).unwrap();
println!("{:?}", bob);
}