Skip to content
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

serde support #108

Closed
ghost opened this issue Jun 5, 2017 · 6 comments · Fixed by #125
Closed

serde support #108

ghost opened this issue Jun 5, 2017 · 6 comments · Fixed by #125

Comments

@ghost
Copy link

ghost commented Jun 5, 2017

Are there any plans to support Serialize and Deserialize for generated types? I'm writing the impls manually in my code, but this falls apart once there's a ton of bitflags generated types. If others are interested in such a feature, maybe I can find the time to implement it myself and submit a pull request :)

@dtolnay
Copy link
Contributor

dtolnay commented Jul 31, 2017

During the libs blitz evaluation we saw that a grand total of 8 crates have dependencies on both bitflags and Serde, so we decided not to pursue it at the time. I would welcome a PR that adds Serialize and Deserialize impls behind a cfg.

@robinst
Copy link

robinst commented Sep 28, 2017

Adding #[derive(Serialize, Deserialize)] to the struct seems to work fine, I'm not sure it's really necessary to add this to the library itself.

@ghost
Copy link
Author

ghost commented Mar 29, 2018

(Sorry for taking so long to write back)
I was mostly referring to serializing flags like this (assuming JSON serializer):

{ "flags": ["flag1", "flag2"] }

as opposed to

{ "flags": 3 }

Serializing as a sequence of named flags rather than the integer representation.
I suppose that this behavior may not be desired in all situations, so it would probably be best as an addon in a separate crate or something.

@xoac
Copy link

xoac commented Feb 27, 2019

I think people looking for serde integer representation will find this post:
So here my solution to make this work:

mod integer_representation {
    use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
    
    // CHANGE THIS ACCORDING TO YOUR CODE
    use super::BinaryQualityFlags; 
    type IntRep = u8;
    type Flags = BinaryQualityFlags;
    
    pub fn serialize<S>(date: &Flags, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {   
        date.bits().serialize(serializer)
    }
    
    pub fn deserialize<'de, D>(deserializer: D) -> Result<Flags, D::Error>
    where
        D: Deserializer<'de>,
    {   
        let raw: IntRep = IntRep::deserialize(deserializer)?;
        BinaryQualityFlags::from_bits(raw).ok_or(serde::de::Error::custom(format!(
            "Unexpected flags value {}",
            raw              
        )))                  
    }                  
}                      
                       
bitflags! {            
                       
#[derive(Serialize, Deserialize)]
pub struct BinaryQualityFlags: u8 {
  /// set when the data is "good", meaning that rest of the system can trust the value
  const ONLINE = 0x1;
  /// the quality all points get before we have established communication (or populated) the point
  const RESTART = 0x2;
}
}

#[derive(Serialize, Deserialize)]
struct Binary {
    ts: u64,
    #[serde(with = "integer_representation")]
    flags: BinaryQualityFlags,
    value: bool,
}

The disadvantage for this solution is that has to be done for every type u want to use and u need to specify IntRep and Flags types.

@jeff-hiner
Copy link

jeff-hiner commented May 21, 2020

I think the original author may have been asking for #147 so I'm going to link it for others stumbling upon this.

@Zannick
Copy link

Zannick commented Mar 26, 2023

Adding #[derive(Serialize, Deserialize)] to the struct seems to work fine, I'm not sure it's really necessary to add this to the library itself.

For future stumblers, you still need two more things (check out the example):

  • put #[serde(transparent)] on the struct
  • enable feature "serde" in your Cargo.toml dep for bitflags

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants