-
-
Notifications
You must be signed in to change notification settings - Fork 774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
De/serialize struct as an array of values #637
Comments
I have not seen this use case before but if it turns out to be pretty common, we could make it nicer with some sort of attribute. #[derive(Serialize, Deserialize, Debug)]
#[serde(as_tuple)]
struct T {
s: String,
i: i32,
b: bool,
} On the other hand if is common in mimbo's code but uncommon elsewhere, you could write a |
Use cases:
I think it would be reasonable for us to support this in #[derive(Serialize)]. |
I have similar use-case, but with enum as a target - items in the source I'm pulling data from are encoded as |
Here is a macro based on @dtolnay solution tested on |
I am closing this issue because I would prefer to see this use case supported by a derive macro in a separate crate. Something like: #[derive(Serialize_tuple, Deserialize_tuple)]
struct T {
s: String,
i: i32,
b: bool,
} which emits the appropriate Serialize and Deserialize impls. In this case: impl Serialize for T {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
#[derive(Serialize)]
#[serde(rename = "T")]
struct Helper<'a>(&'a String, &'a i32, &'a bool);
let helper = Helper(&self.s, &self.i, &self.b);
helper.serialize(serializer)
}
}
impl<'de> Deserialize<'de> for T {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(rename = "T")]
struct Helper(String, i32, bool);
let helper = Helper::deserialize(deserializer)?;
Ok(T {
s: helper.0,
i: helper.1,
b: helper.2,
})
}
} |
Posted a request for implementation: dtolnay/request-for-implementation#3. |
Just for reference, there is a crate for this now: https://crates.io/crates/serde_tuple |
From IRC:
Here is one approach.
The text was updated successfully, but these errors were encountered: