Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add axum_extra::json! #2962

Merged
merged 5 commits into from
Oct 6, 2024
Merged

Add axum_extra::json! #2962

merged 5 commits into from
Oct 6, 2024

Conversation

SabrinaJewson
Copy link
Contributor

Motivation

Closes: #2937

Solution

Add axum_extra::json!, a macro that forwards to typed_json::json! to produce an ad-hoc JSON response with zero allocations.

I chose to put this in axum-extra rather than core Axum for two reasons:

  1. It adds an extra dependency on typed-json. It’s a lightweight dependency and not exposed in the public API, but a dependency nonetheless.
  2. It allows for json! to return an ErasedJson. Using Json<impl Serialize> isn’t ideal because if the json! macro borrows data, the type can’t be returned from a function. Using Response directly isn’t ideal because we lose Clone.

I had to modify a couple lines in the Cargo.toml of axum-extra in order to get the tests to compile on my machine – just enabling some feature flags.

axum-extra/CHANGELOG.md Outdated Show resolved Hide resolved
axum-extra/src/response/erased_json.rs Outdated Show resolved Hide resolved
axum-extra/Cargo.toml Outdated Show resolved Hide resolved
axum-extra/src/response/erased_json.rs Outdated Show resolved Hide resolved
axum-extra/CHANGELOG.md Outdated Show resolved Hide resolved
axum-extra/src/response/erased_json.rs Outdated Show resolved Hide resolved
axum-extra/src/response/erased_json.rs Outdated Show resolved Hide resolved
Copy link
Member

@jplatte jplatte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@jplatte jplatte enabled auto-merge (squash) October 6, 2024 18:57
@jplatte jplatte merged commit 4b4dac4 into tokio-rs:main Oct 6, 2024
18 checks passed
@SabrinaJewson SabrinaJewson deleted the json-macro branch October 6, 2024 19:52
jplatte pushed a commit that referenced this pull request Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider adding axum::json!
2 participants