//! ron initially encoded byte-slices and byte-bufs as base64-encoded strings. //! However, since v0.9, ron now uses Rusty byte string literals instead. //! //! This example shows how the previous behaviour can be restored by serialising //! bytes with strongly-typed base64-encoded strings, or accepting both Rusty //! byte strings and the legacy base64-encoded string syntax.
use base64::engine::{general_purpose::STANDARD as BASE64, Engine}; use serde::{de::Visitor, Deserialize, Deserializer, Serialize, Serializer};
impl ByteStrOrBase64 { fn serialize<S: Serializer>(data: &[u8], serializer: S) -> Result<S::Ok, S::Error> { if cfg!(all()) { // either of these would work
serializer.serialize_str(&BASE64.encode(data))
} else {
serializer.serialize_bytes(data)
}
}
fn main() { let ron = r#"Config(
bytes: b"only byte strings are allowed",
base64: "b25seSBiYXNlNjQtZW5jb2RlZCBzdHJpbmdzIGFyZSBhbGxvd2Vk",
bytes_or_base64: b"both byte strings and base64-encoded strings work",
)"#;
assert_eq!(
ron::from_str::<Config>(ron).unwrap(),
Config {
bytes: b"only byte strings are allowed".to_vec(),
base64: b"only base64-encoded strings are allowed".to_vec(),
bytes_or_base64: b"both byte strings and base64-encoded strings work".to_vec()
}
);
let ron = r#"Config(
bytes: b"only byte strings are allowed",
base64: "b25seSBiYXNlNjQtZW5jb2RlZCBzdHJpbmdzIGFyZSBhbGxvd2Vk",
bytes_or_base64: "Ym90aCBieXRlIHN0cmluZ3MgYW5kIGJhc2U2NC1lbmNvZGVkIHN0cmluZ3Mgd29yaw==",
)"#;
assert_eq!(
ron::from_str::<Config>(ron).unwrap(),
Config {
bytes: b"only byte strings are allowed".to_vec(),
base64: b"only base64-encoded strings are allowed".to_vec(),
bytes_or_base64: b"both byte strings and base64-encoded strings work".to_vec()
}
);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.