rustserde

Gen enum string serialized value with serde


Currently working on a project where I need to serialize some types to a string from a dependency. I am trying to figure out the most efficient way to convert said types into a string while also trying to minimize the amount of dependencies. The simplest option I have been able to figure out so far is to do serde_json::from_value(serde_json::json!(type)), but this requires adding serde_json as a dependency that I only need for this one use case.

Id prefer to avoid adding serde_json if I dont have to. I am already using serde but dont need json format specifically.

An example of an enum I am dealing with:

#[derive(Debug, Default, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
#[typeshare(serialized_as = "String")]
pub enum PublicKeyCredentialType {
    /// Currently the only type defined is a `PublicKey` meaning the public conterpart of an
    /// asymmetric key pair.
    PublicKey,
    /// This is the default as it will be ignored if the value is unknown during deserialization
    #[default]
    Unknown,
}

Solution

  • The way to get information from a type implementing Serialize is to pass it a Serializer. I do not know of any pre-built implementations that just extract a string like this. Fortunately it is not hard, just takes a lot of boiler-plate:

    use serde::ser::{Serializer, Impossible, Error};
    
    struct StringSerializer;
    
    impl Serializer for StringSerializer {
        type Ok = String;
        type Error = std::fmt::Error;
        type SerializeSeq = Impossible<Self::Ok, Self::Error>;
        type SerializeTuple = Impossible<Self::Ok, Self::Error>;
        type SerializeTupleStruct = Impossible<Self::Ok, Self::Error>;
        type SerializeTupleVariant = Impossible<Self::Ok, Self::Error>;
        type SerializeMap = Impossible<Self::Ok, Self::Error>;
        type SerializeStruct = Impossible<Self::Ok, Self::Error>;
        type SerializeStructVariant = Impossible<Self::Ok, Self::Error>;
        
        fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
            Ok(v.to_owned())
        }
        fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
            Ok(name.to_owned())
        }
        fn serialize_unit_variant(self, _name: &'static str, _variant_index: u32, variant: &'static str) -> Result<Self::Ok, Self::Error> {
            Ok(variant.to_owned())
        }
        
        fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize bool"))
        }
        fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize i8"))
        }
        fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize i16"))
        }
        fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize i32"))
        }
        fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize i64"))
        }
        fn serialize_i128(self, _v: i128) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize i128"))
        }
        fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize u8"))
        }
        fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize u16"))
        }
        fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize u32"))
        }
        fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize u64"))
        }
        fn serialize_u128(self, _v: u128) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize u128"))
        }
        fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize f32"))
        }
        fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize f64"))
        }
        fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize char"))
        }
        fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize bytes"))
        }
        fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize none"))
        }
        fn serialize_some<T>(self, _value: &T) -> Result<Self::Ok, Self::Error> where T: ?Sized + Serialize {
            Err(Self::Error::custom("cannot serialize some"))
        }
        fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
            Err(Self::Error::custom("cannot serialize unit"))
        }
        fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<Self::Ok, Self::Error> where T: ?Sized + Serialize {
            Err(Self::Error::custom("cannot serialize newtype struct"))
        }
        fn serialize_newtype_variant<T>(self, _name: &'static str, _variant_index: u32, _variant: &'static str, _value: &T) -> Result<Self::Ok, Self::Error> where T: ?Sized + Serialize {
            Err(Self::Error::custom("cannot serialize newtype variant"))
        }
        fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
            Err(Self::Error::custom("cannot serialize seq"))
        }
        fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
            Err(Self::Error::custom("cannot serialize tuple"))
        }
        fn serialize_tuple_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeTupleStruct, Self::Error> {
            Err(Self::Error::custom("cannot serialize tuple struct"))
        }
        fn serialize_tuple_variant(self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize) -> Result<Self::SerializeTupleVariant, Self::Error> {
            Err(Self::Error::custom("cannot serialize tuple variant"))
        }
        fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
            Err(Self::Error::custom("cannot serialize map"))
        }
        fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct, Self::Error> {
            Err(Self::Error::custom("cannot serialize struct"))
        }
        fn serialize_struct_variant(self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize) -> Result<Self::SerializeStructVariant, Self::Error> {
            Err(Self::Error::custom("cannot serialize struct variant"))
        }
    }
    

    Using it you would just call .serialize() on your value and pass a StringSerializer defined above:

    fn main() {
        dbg!(PublicKeyCredentialType::PublicKey.serialize(StringSerializer));
        dbg!(PublicKeyCredentialType::Unknown.serialize(StringSerializer));
    }
    
    [src/main.rs:118:5] PublicKeyCredentialType::PublicKey.serialize(StringSerializer) = Ok(
        "public-key",
    )
    [src/main.rs:119:5] PublicKeyCredentialType::Unknown.serialize(StringSerializer) = Ok(
        "unknown",
    )
    

    playground link