Understanding the difference between axum::extract::Json & axum::Json

I have been doing a tutorial on Axum and sqlx in which this code was suggested for one of the handlers:

use axum::{extract, http};
...
pub async fn create_quote(
    extract::State(pool): extract::State<PgPool>,
    axum::Json(payload): axum::Json<CreateQuote>,
) -> Result<(http::StatusCode, axum::Json<Quote>), http::StatusCode> {
...
}

Trying to understand what I was writing, I read the axum::Json docs extractor example. Based on this example, I changed my code above so that axum::Json became extract::Json:

pub async fn create_quote(
    extract::State(pool): extract::State<PgPool>,
    extract::Json(payload): extract::Json<CreateQuote>,
) -> Result<(http::StatusCode, axum::Json<Quote>), http::StatusCode> {
...
}

They both work. My inclination is to follow the official docs, but I would like to understand how these differ and why they both work. In the Response example the docs do use axum::Json directly.

I have read through the docs for axum::Json and axum::extract multiple times, but I am still confused. Can someone please explain how these two similar snippets of code differ and why they both work?

They're the same exact type. You can see this in the docs for the axum::extract module: under "Re-exports", one of the items is pub use crate::Json;, which means that axum::Json is re-exported as axum::extract::Json.

2 Likes

Thank you! I did see that, but being a newbie I did not understand the concept of "Re-export."

2 Likes