Hi I'm new to Rust, and I'm having an issue trying to deserialize some data using serde and enums + structs.
I created these struct and enum:
#[derive(Deserialize, Debug)]
pub struct PairOHLCData {
time: u128,
open: String,
high: String,
low: String,
close: String,
vwap: String,
volume: String,
count: u128,
}
#[derive(Deserialize, Debug)]
pub enum OHLC {
Array(Vec<PairOHLCData>),
Number(u32),
}
When the data values are just one type and I just use the struct to deserialize, I'm able to get the data without errors, for example this works:
let j = r#" {"randomkey": [[1611429000,"24.96171","25.00000","24.90000","24.94870","24.95744","6030.00911360",38],[1611429300,"24.97083","25.12873","24.97083","25.11965","25.00859","1688.08294838",30]]} "#;
let array: HashMap<String, Vec<PairOHLCData>> = serde_json::from_str(j).unwrap();
println!("{:#?}", array);
But when the data has different value types(the first value it's the same data as before, but now it has an extra key with an integer value), and I try to use the enum to deserialize the data, then I get an error:
let j = r#" {"randomkey": [[1611429000,"24.96171","25.00000","24.90000","24.94870","24.95744","6030.00911360",38],[1611429300,"24.97083","25.12873","24.97083","25.11965","25.00859","1688.08294838",30]],"last":1611644400} "#;
let array: HashMap<String, OHLC> = serde_json::from_str(j).unwrap();
println!("{:#?}", array);
The previous code fails showing me the error:
Error("expected value", line: 1, column: 16)
Can anyone tell me what I'm doing wrong?
Thanks.