Hello. I'm trying to flatten json response but got the missing field error. I want to collect some nested fields to a single struct in a vec. What am doing wrong?
#[derive(Serialize, Deserialize, Debug)]
struct AttachmentResponse {
results: Vec<Attachment>,
}
#[derive(Serialize, Deserialize, Debug)]
struct Attachment {
id: String,
title: String,
#[serde(flatten)]
metadata: AttachmentMetadata,
#[serde(flatten, rename = "_links")]
links: AttachmentLinks,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct AttachmentMetadata {
media_type: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct AttachmentLinks {
download: String,
}
fn main() -> anyhow::Result<()> {
let document = serde_json::json!({
"results": [
{
"id": "719414246",
"type": "attachment",
"status": "current",
"title": "Screenshot 2024-03-12 at 19.49.45.png",
"metadata": {
"mediaType": "image/png",
"labels": {
"results": [],
"start": 0,
"limit": 200,
"size": 0,
"_links": {
"self": "https://confluence.my.com/rest/api/content/719414246/label"
}
},
"_expandable": {
"currentuser": "",
"frontend": "",
"editorHtml": "",
"properties": ""
}
},
"extensions": {
"mediaType": "image/png",
"fileSize": 240734,
"comment": ""
},
"_links": {
"webui": "/display/~aaa/Files?preview=%2F719391738%2F719414246%2FScreenshot+2024-03-12+at+19.49.45.png",
"download": "/download/attachments/719391738/Screenshot%202024-03-12%20at%2019.49.45.png?version=1&modificationDate=1710867778590&api=v2",
"thumbnail": "/download/thumbnails/719391738/Screenshot%202024-03-12%20at%2019.49.45.png?api=v2",
"self": "https://confluence.my.com/rest/api/content/719414246"
},
"_expandable": {
"container": "/rest/api/content/719391738",
"operations": "",
"children": "/rest/api/content/719414246/child",
"restrictions": "/rest/api/content/719414246/restriction/byOperation",
"history": "/rest/api/content/719414246/history",
"ancestors": "",
"body": "",
"version": "",
"descendants": "/rest/api/content/719414246/descendant",
"space": "/rest/api/space/~aaa"
}
},
],
"start": 0,
"limit": 100,
"size": 5,
"_links": {
"self": "https://confluence.psbnk.msk.ru/rest/api/content/719391738/child/attachment",
"base": "https://confluence.psbnk.msk.ru",
"context": ""
}
})
.to_string();
let data: AttachmentResponse = serde_json::from_str::<AttachmentResponse>(&document)?.into();
println!("{:?}", data.results[0]);
Ok(())
}