Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kodraus/bitflags-serde-legacy
Implement serde traits for bitflags 2.x types compatibly with 1.x
https://github.com/kodraus/bitflags-serde-legacy
Last synced: about 2 months ago
JSON representation
Implement serde traits for bitflags 2.x types compatibly with 1.x
- Host: GitHub
- URL: https://github.com/kodraus/bitflags-serde-legacy
- Owner: KodrAus
- License: apache-2.0
- Created: 2023-02-08T02:28:54.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-30T05:34:55.000Z (about 1 year ago)
- Last Synced: 2024-11-10T02:18:11.316Z (2 months ago)
- Language: Rust
- Size: 20.5 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# bitflags-serde-legacy
The serialization format used by [`bitflags!`](docs.rs/bitflags) has changed between `1.x` and
`2.x`. If you previously `#[derive(Serialize, Deserialize]`, then you can use this library to
maintain compatibility while upgrading.# Usage
You can either use this as a regular dependency, or pull the source into a private module
in your project.Add `bitflags-serde-legacy` to your `Cargo.toml`:
```toml
[dependencies.bitflags-serde-legacy]
version = "0.1.1"
```Then, replace an existing `#[derive(Serialize, Deserialize)]` on your `bitflags!`
generated types with the following manual implementations:```rust
use bitflags::bitflags;bitflags! {
// #[derive(Serialize, Deserialize)]
struct Flags: u32 {
const A = 0b00000001;
const B = 0b00000010;
const C = 0b00000100;
const ABC = Self::A.bits() | Self::B.bits() | Self::C.bits();
}
}impl serde::Serialize for Flags {
fn serialize(&self, serializer: S) -> Result {
bitflags_serde_legacy::serialize(self, "Flags", serializer)
}
}impl<'de> serde::Deserialize<'de> for Flags {
fn deserialize>(deserializer: D) -> Result {
bitflags_serde_legacy::deserialize("Flags", deserializer)
}
}
```