Axum Codec
A body extractor for the Axum web framework.
Features
- Supports encoding and decoding of various formats with a single extractor.
- Provides a wrapper for
axum::routing::method_routingto automatically encode responses in the correct format according to the specifiedAcceptheader (with a fallback toContent-Type, then one of the enabled formats). - Provides an attribute macro (under the
macrosfeature) to add derives for all enabled formats to a struct/enum. - Zero-copy decoding with
BorrowCodec.
Here's a quick example that can do the following:
- Decode a
Userfrom the request body in any of the supported formats. - Encode a
Greetingto the response body in any of the supported formats.
use ;
use ;
// Shorthand for the following (assuming all features are enabled):
//
// #[derive(
// serde::Serialize, serde::Deserialize,
// bincode::Encode, bincode::Decode,
// bitcode::Encode, bitcode::Decode,
// validator::Validate,
// )]
// #[serde(crate = "...")]
// #[bincode(crate = "...")]
// #[bitcode(crate = "...")]
// #[validator(crate = "...")]
async
/// A manual implementation of the handler above.
async
/// Specify `impl IntoCodecResponse`, similar to `axum`
async
async
Feature flags
macros: Enables theaxum_codec::applyattribute macro.json*: EnablesJSONsupport.form: Enablesx-www-form-urlencodedsupport.msgpack: EnablesMessagePacksupport.bincode: EnablesBincodesupport.bitcode: EnablesBitcodesupport.yaml: EnablesYAMLsupport.toml: EnablesTOMLsupport.aide: Enables support for theAidedocumentation library.validator: Enables support for theValidatorvalidation library, validating all input when extracted withCodec<T>.
* Enabled by default.
A note about #[axum::debug_handler]
Since axum-codec uses its own IntoCodecResponse trait for encoding responses, it is not compatible with #[axum::debug_handler]. However, a new #[axum_codec::debug_handler] (and #[axum_codec::debug_middleware]) macro
is provided as a drop-in replacement.
Roadmap
- Add
codec!macro for defining custom codecs that use a different subset of enabled formats.
License
Dual-licensed under MIT or Apache License v2.0.