I am trying to use Serde to deserialize JSON (serde-json
) and XML (serde-xml-rs
) files based on the following struct:
use serde_derive::Deserialize;
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct SchemaConfig {
pub name: String,
#[serde(rename = "Cube")]
pub cubes: Vec<CubeConfig>,
}
The fields I am deserializing on have different names based on the file type. In this case, I would like for a JSON file to have a cubes
key with a list of cubes, but the equivalent in XML would be multiple <Cube />
elements.
I can't figure out how to accept both cubes
and Cube
as keys for the deserialization. The closest thing I found was the #[serde(rename = "Cube")]
option but when I use that the JSON deserialization stops working since it only accepts the Cube
key. If I remove that option, the XML deserialization stops working as it then only accepts cubes
as the key.
Is there a simple way to accomplish this in Serde?