1

JSON I want to parse in rust using Serde:

[999,{"a":[[1,2,3], [12,3]], "b":[[1,2,3], [12,3]]}, "some_string"]

I was trying to use

#[derive(Deserialize)]
pub struct SnapshotMessage {
    id: i32,
    snapshot: Snapshot,
    channel: String,
}

#[derive(Deserialize)]
struct Snapshot {
    a: Vec<Level>,
    b: Vec<Level>
}

This does not work because there is no mapping for id. I cant see anyway to deal with a custom array at the root level if it was just an array of identical objects I could simply do Vec but this is not a uniform root list.

One of my ideas is to use a custom deserializer but is there a way to only write part of a deserizlizer so that I can use derived for nested objects?

What is the best way to handle non uniform root arrays?

  • 1
    Does this answer your question? [Rust serde deserializing a mixed array](https://stackoverflow.com/questions/57903579/rust-serde-deserializing-a-mixed-array). Serde will deserialize from mixed arrays by default. You can see it working on [the playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e61482893c1c52788235c34a8d7f99f7) – kmdreko Dec 30 '20 at 23:34

0 Answers0