You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is similar to #745 ("Allow integer tags for internally tagged enums") but this issue focuses on types and struct fields.
When working with structs and fields, the Serializer and Deserializer traits take name: &'static str parameters, and other traits like SerializeStruct take key: &'static str parameters. These parameters describe the type/field.
But not all formats use textual keys. Some, for example, uses integers for their keys. I'd like to be able to use #[serde(rename = 42)] (or #[serde(rename = SOME_CONST)] if that's not too much), which would allow me to provide serde with the integer key for that particular type or field. But doing this is problematic for two reasons:
It requires Serializer/Deserializer/etc. to use a different type in their methods for the name or key parameters.
It doesn't play well when trying to (de)serialize the same struct to/from a human readable format (e.g., JSON).
I think this could be worked around by using the is_human_readable() function and allowing users to provide both a human-readable and machine-readable name to Serde. For example:
Then the #[derive] macros could be changed like so:
impl Serialize for Foo {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
// The check for is_human_readable() is only added if machine_name is used.
if serializer.is_human_readable() {
let mut foo = serializer.serialize_struct("ThisIsANameForHumans", 2)?;
foo.serialize_field("Bar", &self.bar)?;
foo.serialize_field("Baz", &self.baz)?;
foo.end()
} else {
let mut foo = serializer.serialize_machine_struct(0x10, 2)?;
foo.serialize_field(0x21, &self.bar)?;
foo.serialize_field(0x8014, &self.baz)?;
foo.end()
}
}
}
Open questions:
If only some of the fields have machine_name specified, how should the be handled? Fallback to using human names? Or fallback to using the field ordinal number? Or fail to compile?
Not all formats use integers as keys. Should the machine_name instead take a string of bytes (i.e. a byte literal that can have non-UTF-8 contents)?
I'd be willing to take a shot at implementing this if the serde maintainers are interested.
The text was updated successfully, but these errors were encountered:
This is similar to #745 ("Allow integer tags for internally tagged enums") but this issue focuses on types and struct fields.
When working with structs and fields, the
Serializer
andDeserializer
traits takename: &'static str
parameters, and other traits likeSerializeStruct
takekey: &'static str
parameters. These parameters describe the type/field.But not all formats use textual keys. Some, for example, uses integers for their keys. I'd like to be able to use
#[serde(rename = 42)]
(or#[serde(rename = SOME_CONST)]
if that's not too much), which would allow me to provide serde with the integer key for that particular type or field. But doing this is problematic for two reasons:Serializer
/Deserializer
/etc. to use a different type in their methods for thename
orkey
parameters.I think this could be worked around by using the
is_human_readable()
function and allowing users to provide both a human-readable and machine-readable name to Serde. For example:Then the
#[derive]
macros could be changed like so:Open questions:
machine_name
specified, how should the be handled? Fallback to using human names? Or fallback to using the field ordinal number? Or fail to compile?I'd be willing to take a shot at implementing this if the serde maintainers are interested.
The text was updated successfully, but these errors were encountered: