media-io / yaserde

Yet Another Serializer/Deserializer
MIT License
179 stars 60 forks source link

yaserde is reading the values of #[serde(...)] attributes #101

Closed ephraimkunz closed 3 years ago

ephraimkunz commented 3 years ago

I'm not sure if this is intentional or not, but it seems dangerous when we want serde and yaserde to coexist.

I have a struct like this:

#[derive(Serialize, YaSerialize)]
pub struct Test {
    #[yaserde(skip_serializing_if = "is_none")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub val: Option<String>
}

impl Test {
    fn is_none(&self, o: &Option<String>) -> bool {
        o.is_none()
    }
}

Yaserde's skip_serializing_if requires a method name and serde's skip_serializing_if requires a function name. This means that I need both attributes, if I want my both JSON and XML representation to skip serializing this field.

However when I put them both on I get this error:

error: proc-macro derive panicked
  --> gedcomx-cli/src/main.rs:18:21
   |
18 | #[derive(Serialize, YaSerialize)]
   |                     ^^^^^^^^^^^
   |
   = help: message: `"Option::is_none"` is not a valid identifier

So it appears that yaserde is trying to read the serde macro. So I'd have to write a custom serializer today if I want this behavior.