How can I deserialise JSON {"arr":[1,2,3,4]} without performing a heap allocation using serde_json_core or similar? It performs one allocation currently. I see serde_json_core uses the heapless crate but I am unsure exactly how to make them work together.
#[derive(Deserialize)]
struct MyStruct {
arr: Vec<u64>,
}
fn main() {
let j = r#"{"arr":[1,2,3,4]}"#;
let r: serde_json_core::de::Result<(MyStruct, usize)> = serde_json_core::from_str(j);
let (out, _) = r.unwrap();
println!("First value: {}.", out.arr[0]);
}
I am using serde_json_core = 0.4.0.
[u64; N]. If not, you need to decide on a maximum possible array size you want to be able to deserialize. You need to preallocate everything on the stack, so you can't support arbitrary sizes. UsingVecis not an option, since it will allocate if it contains any elements.[u64; 10]I get'called 'Result::unwrap()' on an 'Err' value: CustomError'. I need the array size to be exactly 4 ([u64; 4]) to work.heapless::Vec. You should be able to adduse serde_json_core::heapless::Vec;to your code, and then add10as second template parameter to theVecinsideMyStruct.serde_json_corehowever theDeserializetrait is not implemented for all the varieties ofheapless::Vec:the trait 'Deserialize<'_>' is not implemented for 'serde_json_core::heapless ::Vec<u64, 4_usize>. I could implement theDeserializemyself for theVeccapacity I want but was hoping I didn't have to do that.serdefeature forheaplessto get the impl.