Comments (6)
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.
from bitflags.
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 yourCargo.toml
dep forbitflags
from bitflags.
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.
from bitflags.
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.
from bitflags.
(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.
from bitflags.
I think the original author may have been asking for #147 so I'm going to link it for others stumbling upon this.
from bitflags.
Related Issues (20)
- Breaking change released as 2.3.0, which causes build failures on upgrade HOT 6
- Problems deriving serde after upgrading HOT 2
- Treatment of unknown bits in operators is inconsistent HOT 18
- Inconsistent debug output for flag with no bits HOT 4
- SWC - Update to 2.3.x causes incorrect output HOT 9
- Flagging supply-chain security issues HOT 3
- Match expression question HOT 2
- messes with dependent crates! HOT 3
- Documenting bitflags: how to get documentation for the generated bitflags HOT 4
- clippy::iter_without_into_iter HOT 3
- Feature guard `parser`? HOT 4
- Static member variable on the impl? HOT 2
- [Feature Request] `repr(packed)` for internal bits types HOT 2
- [Feature Request] Smaller internal types with non-breaking size increases HOT 3
- [Feature Request] Serialize as struct of bools HOT 2
- [Feature Request] Make Flags trait methods public HOT 1
- Please allow to use the "internal" derives in the "custom-derive" case HOT 6
- Enforce uniqueness of flags HOT 4
- FR: Please make operators cosnt. bitwise or operator `|` is not const HOT 3
- Allow specifying attributes in `impl` mode
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bitflags.