简体   繁体   中英

Encoding PathBuf containing path with invalid utf-8 characters using serde in Rust

I have a struct with pathbuf as one of its field, struct A { pub first: bool, pub path: PathBuf, } Then path can have invalid utf-8 characters as linux allow to create files and folder with invalid utf-8 characters. I am using rmp_serde::to_vec_named() to get vec from the struct object. But, In case of path with invalid utf-8 characters, it is crashing with Error: SerdeEncodeMspack(Syntax("path contains invalid UTF-8 characters")).

Is there any way to encode a struct with invalid utf-8 charcters without skipping it? Thanks in advance

Could you use a custom de/serialize function to convert it to/from a byte array when serializing?

Something like this should work. You will have to adjust it to handle non-unix systems.

use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Serialize, Deserialize)]
struct Demo {
    #[serde(with = "path_handling")]
    path: PathBuf,
}

mod path_handling {
    use super::*;
    use serde::de::Deserializer;
    use serde::ser::Serializer;
    use std::ffi::OsStr;
    use std::os::unix::ffi::OsStrExt;

    pub fn serialize<S>(p: &PathBuf, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_bytes(p.as_os_str().as_bytes())
    }
    pub fn deserialize<'de, D>(deserializer: D) -> Result<PathBuf, D::Error>
    where
        D: Deserializer<'de>,
    {
        let data = <&[u8]>::deserialize(deserializer)?;
        Ok(OsStr::from_bytes(data).into())
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM