Rust JSON Serialization

Viewed 135

I am new to Rust.

i have a JSON string in the format...

    Array([
    Object({
        "columns": Array([
            String(
                "attr",
            ),
            String(
                "user_count",
            ),
        ]),
        "data": Array([
            Object({
                "meta": Array([
                    Null,
                    Null,
                ]),
                "row": Array([
                    Array([
                        String(
                            "Built-in account for administering the computer/domain",
                        ),
                    ]),
                    Number(
                        1,
                    ),
                ]),
            }),
        ]),
    }),
])

i have created a Rust model to Serialise the JSON to...

#[derive(Serialize, Deserialize, Debug)]
pub struct ResolvedAttribute {
    pub columns: Vec<Option<String>>,
    pub data: Vec<MetaData>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct MetaData {
    pub meta: Vec<Option<String>>,
    pub row: (Vec<Vec<Option<String>>>, i64),
}

when calling my API and getting the JSON - on attempting to serialise to the object (code below) I keep getting the error...

  let res = REQWEST_MTLS.post(&uri).json(&req).send().await.unwrap();

  //println!("res {:#?}", res);

  if !res.status().is_success() {
      let body: Value = res.json().await.unwrap();
      println!("Error: {:#}", body);
      panic!();
  }

  let res: Value = res.json().await.unwrap();

  println!("res {:#?}", res);

  let root: Vec<ResolvedAttribute> = match serde_json::from_value(res.clone()) {
    Ok(r) => r,
    Err(e) => {
        println!("{:#}\nError:{:#?}", res, e);
        panic!("Deserialization Error.");
    }
  };
    
  return root

thread 'main' panicked at 'Deserialization Error.

my struct seems correct to my JSON schema - can anyone point me in the right direction?

1 Answers

Your row looks like this:

"row": [
    ["Built-in account for administering the computer/domain"],
    1,
],

So you have one too many Vecs for your row field type, it should be:

pub row: (Option<Vec<Option<String>>>, i64),
Related