Crate Serde
Chester Wyke January 06, 2023 Updated: April 26, 2025 #Rust
Rust (Series)
Conditional Compilation
Install
Crate Serde
References
Cargo
Toolchain (Nightly)
String Formatting
Time
Crate Tokio
Publish Crate
rustfmt
Single file script
Snippets
CI
Pattern Type State
WASM
Create New Crate
Documentation
JSON
Pattern Builder
Testing
Working with collections of bytes
Thoughts about rust
Are we yet
OnceLock
Crate Actix Web
Stack Overflow
Crate Clap
Crate Poll Promise
Crate Insta
Tips
Create new egui project
Crate CSV
Crate egui
Iterators
Crate Tracing Subscriber
Regex
vscode
Enum Conversion
Macros
lettre
Google APIs
Conditional Compilation
Install
Crate Serde
References
Cargo
Toolchain (Nightly)
String Formatting
Time
Crate Tokio
Publish Crate
rustfmt
Single file script
Snippets
CI
Pattern Type State
WASM
Create New Crate
Documentation
JSON
Pattern Builder
Testing
Working with collections of bytes
Thoughts about rust
Are we yet
OnceLock
Crate Actix Web
Stack Overflow
Crate Clap
Crate Poll Promise
Crate Insta
Tips
Create new egui project
Crate CSV
Crate egui
Iterators
Crate Tracing Subscriber
Regex
vscode
Enum Conversion
Macros
lettre
Google APIs
Tips and Tricks for working with Serde.
Attributes
Website: https://serde.rs/attributes.html
Specially noted Container attributes
#[serde(rename_all = "lowercase")]
Default - Use default for any missing fields
#[serde(default)]Specially noted Field attributes
#[serde(rename = "name")]
#[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)]Enums and TOML
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();
}