-
-
Notifications
You must be signed in to change notification settings - Fork 6
Derive macros to serialize and deserialize struct as an array of values #3
Comments
I found this issue from serde-rs/serde#637 while working on a JSONRPC related mechanism for deserializing a struct by name or position. I've made a first attempt at this: https://github.com/kardeiz/serde_tuple, though it is still a little rough. My implementation also lets you specify output position (e.g., if you want to change the order of values in the output array). It also offers a It is currently limited to no generics. I think this could be pretty easily lifted to just "no reference fields". It might be possible to be open to any Question: is there a reason to prefer |
Thanks for taking a look at this. Some notes on your implementation:
|
Thanks for the comments! I've never really thought about struct field position as meaningful, but that makes sense: the I've been using I've updated the names. I've also made everything generic (hopefully in a way that covers all cases). https://github.com/kardeiz/serde_tuple/blob/master/src/lib.rs |
Nicely done. Let me know once the crate is documented and published to crates.io and I'll close out this issue. |
Thanks! See https://crates.io/crates/serde_tuple. |
Fantastic. I marked off the idea in the readme with a link to your crate. |
By default, Serde will serialize a struct to JSON as a JSON map like
{"s":"","i":0,"b":true}
. In some cases it can be preferable to serialize as a JSON array like["",0,true]
. Serde serializes tuple structs like this, but then we lose the field names when working with the type in Rust code.I would like there to be derive macros
Serialize_tuple
andDeserialize_tuple
that let me write an ordinary braced struct with named fields but serialize it as if it were a tuple struct.See
serde_repr
as an existing example of an independent special-purpose derive macro for Serde traits.The text was updated successfully, but these errors were encountered: