How to serialize SDK Output struct? #714
-
I am using the Inspector V2 SDK (https://crates.io/crates/aws-sdk-inspector2) When I call But I want to send this to a different system, and now its parsed to into a Rust struct. How can I serialize it back to JSON (or get the raw json response)? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Unfortunately, we don't currently support serde (although there is an ongoing community contribution to enable that: RFC 30: serde support). It's possible to get access to the raw response, however, I'd caution you from going that route. The raw response may contain fields serialized in headers—just looking at the body won't necessarily contain everything you need. I'd probably suggest making your own parallel struct that contains the fields you need. In this case, Please note that this API is actually paginated so #[derive(Serialize, Deserialize)]
struct Findings { arns: Vec<String> }
async fn serialize_findings(client: &aws_sdk_inspector2::Client) -> Result<Findings, aws_sdk_inspector2::Error> {
let mut arns: Vec<String> = vec![];
let mut findings_stream = client.list_findings()/*.<params>*/.into_paginator().send();
while let Some(findings) = findings_stream.try_next().await? {
arns.extend(findings.findings().unwrap_or_default().iter().map(|finding|
// just copy the ARN so it can be retrieved. More fields could also be copied.
finding.finding_arn().unwrap_or_default().to_string())
);
}
Ok( Findings { arns })
} |
Beta Was this translation helpful? Give feedback.
Unfortunately, we don't currently support serde (although there is an ongoing community contribution to enable that: RFC 30: serde support).
It's possible to get access to the raw response, however, I'd caution you from going that route. The raw response may contain fields serialized in headers—just looking at the body won't necessarily contain everything you need.
I'd probably suggest making your own parallel struct that contains the fields you need. In this case,
ListFindingsOutput
only contains one meaningful field—finding_arns
which is the list ofarns
.Please note that this API is actually paginated so
ListFindingsOutput
isn't what you'd want to serialize anyway.#[derive(Serialize, D…