I want to call the api in aws-sdk-rust to get aws resources' parameter and serialize the result to json file. (for example, sdk/lambda/src/client/get_function.rs in aws-sdk-lambda)
Unfortunately, the result struct FunctionConfiguration does not implement Serialize trait, so cargo can not compile the code.
the code is like this:
let client = LambdaClient::new(&config);
let response = client.get_function().function_name(function_name).send().await?;
let configuration = response.configuration().unwrap();
let json_data = serde_json::to_string_pretty(&configuration)?;
the error message is:
error[E0277]: the trait bound `FunctionConfiguration: Serialize` is not satisfied
141 | let json_data = serde_json::to_string_pretty(&configuration) {
| ---------------------------- ^^^^^^^^^^^^^^ the trait `Serialize` is not implemented for `FunctionConfiguration
I don't want to manually implement the Serialize trait for the struct by myself, because there are so many similar structs in aws-sdk-rust, and each one has a complex nested structure.
Implementing Serialize for a type from a third party crate is indeed tedisome. I believe making a feature request for this while bridging the gap till it lands by patching the dependency with your own fork of the crate where you add derive(Serialize) for the types you need would possibly be the solution requiring the least amount of effort.
There are many APIs, each API has its own return structure type, and each structure has layers of nested substructures.
Even if this method can achieve the goal, the workload is very huge. I guess that this workload should be about the same as implementing serde::Serialize trait manually.
Are there any other better solutions?
Likely not. Deriving is just one attribute per struct, while implementing manually is either a one implementation per struct or a huge implementation for top-level struct, essentially inlining everything into it. derive(Serialize) (or cfg_attr(feature = "serde", derive(Serialize))) is a solution, in most cases.
Yes,I agree with you.
But unfortunately, for the 3-party API library, I can not control how its code is generated. unless it provides the corresponding feature option...