Code Monkey home page Code Monkey logo

Comments (6)

xoac avatar xoac commented on May 25, 2024 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.

Zannick avatar Zannick commented on May 25, 2024 2

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

from bitflags.

dtolnay avatar dtolnay commented on May 25, 2024

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.

robinst avatar robinst commented on May 25, 2024

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.

 avatar commented on May 25, 2024

(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.

jeff-hiner avatar jeff-hiner commented on May 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.