Skip to content

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.

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