Crate Serde

    Tips and Tricks for working with Serde.

    Attributes πŸ”—

    Website: https://serde.rs/attributes.html

    Specially noted Container attributes πŸ”—

    Rename All

    #[serde(rename_all = "lowercase")]
    

    Default - Use default for any missing fields

    #[serde(default)]
    

    Specially noted Field attributes πŸ”—

    Rename

    #[serde(rename = "name")]
    

    Alias

    #[serde(alias = "name")]
    

    Path - Use callable to set default value if field not found. This may also be a trait method.

    #[serde(default = "path")]
    

    Default - Use Default::default() if not present. See also default value for a field

    #[serde(default)]
    

    Snippets πŸ”—

    Enums and TOML πŸ”—

    Source: https://stackoverflow.com/questions/57560593/why-do-i-get-an-unsupportedtype-error-when-serializing-to-toml-with-a-manually-i

    Using #[serde(tag = "type")] can sometimes resolve Err value: UnsupportedType errors

    Transcoding πŸ”—

    Source: https://serde.rs/transcode.html

    Going from any self describing format to any other self describing format (Not tested yet, but wanted to be able to find it next time)

    use std::io;
    
    fn main() {
        // A JSON input with plenty of whitespace.
        let input = r#"
          {
            "a boolean": true,
            "an array": [3, 2, 1]
          }
        "#;
    
        // A JSON deserializer. You can use any Serde Deserializer here.
        let mut deserializer = serde_json::Deserializer::from_str(input);
    
        // A compacted JSON serializer. You can use any Serde Serializer here.
        let mut serializer = serde_json::Serializer::new(io::stdout());
    
        // Prints `{"a boolean":true,"an array":[3,2,1]}` to stdout.
        // This line works with any self-describing Deserializer and any Serializer.
        serde_transcode::transcode(&mut deserializer, &mut serializer).unwrap();
    }