axum-serde
📑 Overview
axum-serde is a library that provides multiple serde-based extractors / responses for the Axum web framework. It also offers a macro to easily customize extractors and responses without writing much boilerplate code.
If you were using crates like axum-yaml, axum-msgpack etc. in axum 0.6 and wish to upgrade to axum 0.7, * axum-serde* can be used as a replacement to simplify the migration, without having to modify existing code too much.
🚀 Basic usage
- Install
cargo add axum-serde --features yaml,sonic
# Enable features as you need
- Example
use post;
use Router;
use ;
use ;
use ;
use TcpListener;
pub async
pub async
async
- Test
curl -X POST http://localhost:8080/y2j -H "Content-Type: application/yaml" -d $'a: 42\nb: Hello'
curl -X POST http://localhost:8080/j2y -H "Content-Type: application/json" -d '{"a": 42, "b": "Hello, world!"}'
🗂️ Extractors / responses
Extractor | Feature | Backend |
---|---|---|
Yaml<T> |
yaml | serde_yaml v0.9.27 |
MsgPack<T> / MsgPackRaw<T> |
msgpack | rmp-serde v1.1.2 |
Toml<T> |
toml | toml v0.8.8 |
Xml<T> |
xml | quick-xml v0.31.0 |
Sonic<T> |
sonic | sonic-rs v0.3.3 |
Cbor<T> |
cbor | ciborium v0.2.2 |
🎁 Custom extractor / response
Use the extractor
macro to create custom extractors with minimal boilerplate:
- Example
use ;
extractor!;
- Test
More dev-dependencies
are required to run the tests:
# Add dev-dependencies for tests
cargo add axum-test --dev
cargo add serde --features derive --dev
cargo add tokio --features macros --dev
# Run the generated tests
cargo test myfmt
📜 License
This project is licensed under the MIT License.