#Deserialising API request with JSON serde issues

18 messages · Page 1 of 1 (latest)

somber void
#

I'm having issues getting serde to deserialize my json and im not sure why
here is my json

{
    "message": null,
    "data": [
        {
            "shockers": [
                {
                    "name": "NAME",
                    "isPaused": false,
                    "createdOn": "2024-05-15T00:03:26.560347Z",
                    "id": "UUID",
                    "rfId": 27734,
                    "model": "CaiXianlin"
                }
            ],
            "id": "UUID",
            "name": "NAME",
            "createdOn": "2024-05-14T00:33:36.675453Z"
        }
    ]
}

and here are my structs

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct ListShockersBaseResponse {
   message: Option<String>,
   data: Vec<ListShockersResponse>
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct ListShockersResponse {
   shockers: Vec<ShockerResponse>,
   id:String,
   name:String,
   created_on: String
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct ShockerResponse {
   name: Option<String>,
   is_paused:    bool,
   created_on:    String,
   id:    String,
   rf_id: i32,
   model: ShockerModel
}

#[derive(EnumString, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
enum ShockerModel{
   CaiXianlin, 
   PetTrainer, 
   Petrainer998DR
}
humble thistle
#

trouble like compile or runtime errors ?

somber void
#

runtime

#

called `Result::unwrap()` on an `Err` value: Error("missing field `shockers`", line: 1, column: 309)

#

Pulling in data from a web request but above is the response exactly (minus some sensitive data)

#

The API docs

humble thistle
#

i think it's this

  #[derive(EnumString, Serialize, Deserialize, Debug)]
- #[serde(rename_all = "camelCase")]
  enum ShockerModel{
      CaiXianlin, 
      PetTrainer, 
      Petrainer998DR
  }
#

so the error is weird, i can't reproduce that exact string

somber void
#

Same issue

#
    let resp = client.get("http://api.url").send().await/?.text().await?;
    let deserialized: ListShockersResponse = serde_json::from_str(&resp.as_str()).unwrap();
#

oh my god

#

i see it

#
    let resp = client.get("http://api.url").send().await/?.text().await?;
-    let deserialized: ListShockersResponse = serde_json::from_str(&resp.as_str()).unwrap();
+    let deserialized: ListShockersBaseResponse = serde_json::from_str(&resp.as_str()).unwrap();
#

🤦

#

I hate the way they names these Structs lol

#

ty for the help

#

works like a charm