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
Comments
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. |
Adding |
(Sorry for taking so long to write back)
as opposed to
Serializing as a sequence of named flags rather than the integer representation. |
I think people looking for serde integer representation will find this post: 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 |
I think the original author may have been asking for #147 so I'm going to link it for others stumbling upon this. |
For future stumblers, you still need two more things (check out the example):
|
Are there any plans to support
Serialize
andDeserialize
for generated types? I'm writing the impls manually in my code, but this falls apart once there's a ton ofbitflags
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 :)The text was updated successfully, but these errors were encountered: