Struct Json
#[must_use]
pub struct Json<T>(pub T);
JSON Extractor / Response.
When used as an extractor, it can deserialize request bodies into some type that
implements serde::de::DeserializeOwned. The request will be rejected (and a JsonRejection will
be returned) if:
- The request doesn't have a
Content-Type: application/json(or similar) header. - The body doesn't contain syntactically valid JSON.
- The body contains syntactically valid JSON, but it couldn't be deserialized into the target type.
- Buffering the request body fails.
⚠️ Since parsing JSON requires consuming the request body, the Json extractor must be
last if there are multiple extractors in a handler.
See "the order of extractors"
See JsonRejection for more details.
Extractor example
use ;
use Deserialize;
async
let app = new.route;
# let _: Router = app;
When used as a response, it can serialize any type that implements serde::Serialize to
JSON, and will automatically set Content-Type: application/json header.
If the Serialize implementation decides to fail
or if a map with non-string keys is used,
a 500 response will be issued
whose body is the error message in UTF-8.
Response example
use ;
use Serialize;
use Uuid;
async
async
let app = new.route;
# let _: Router = app;
Fields
0: T
Implementations
impl<T> Json<T>
where
T: DeserializeOwned,
fn from_bytes(bytes: &[u8]) -> Result<Self, JsonRejection>Construct a
Json<T>from a byte slice. Most users should prefer to use theFromRequestimpl but special cases may require first extracting aRequestintoBytesthen optionally constructing aJson<T>.
Trait Implementations
impl<T> Deref for Json<T>
type Target = T;fn deref(&self) -> &Self::Target
impl<T> DerefMut for Json<T>
fn deref_mut(&mut self) -> &mut Self::Target
impl<T> From<T> for Json<T>
fn from(inner: T) -> Self
impl<T> IntoResponse for Json<T>
where
T: Serialize,
fn into_response(self) -> Response
impl<T, S> FromRequest<S> for Json<T>
where
T: DeserializeOwned,
S: Send + Sync,
type Rejection = JsonRejection;async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection>
impl<T, S> OptionalFromRequest<S> for Json<T>
where
T: DeserializeOwned,
S: Send + Sync,
type Rejection = JsonRejection;async fn from_request(req: Request, state: &S) -> Result<Option<Self>, Self::Rejection>
impl<T: Clone> Clone for Json<T>
fn clone(&self) -> Self
impl<T: Copy> Copy for Json<T>
impl<T: Debug> Debug for Json<T>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<T: Default> Default for Json<T>
fn default() -> Self
Auto Trait Implementations
impl<T> Freeze for Json<T>
where
T: Freeze,
impl<T> RefUnwindSafe for Json<T>
where
T: RefUnwindSafe,
impl<T> Send for Json<T>
where
T: Send,
impl<T> Sync for Json<T>
where
T: Sync,
impl<T> Unpin for Json<T>
where
T: Unpin,
impl<T> UnsafeUnpin for Json<T>
where
T: UnsafeUnpin,
impl<T> UnwindSafe for Json<T>
where
T: UnwindSafe,
Blanket Implementations
impl<H, T> HandlerWithoutStateExt<T> for Json<T>
where
H: Handler<T, ()>,
fn into_service(self) -> HandlerService<H, T, ()>fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>fn into_make_service_with_connect_info<C>(self) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
impl<P, T> Receiver for Json<T>
where
P: Deref<Target = T> + ?Sized,
T: ?Sized,
type Target = T;
impl<R> Rng for Json<T>
where
R: RngCore + ?Sized,
impl<R> TryCryptoRng for Json<T>
where
R: CryptoRng + ?Sized,
impl<R> TryRngCore for Json<T>
where
R: RngCore + ?Sized,
type Error = never;fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), <R as TryRngCore>::Error>
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for Json<T>
where
ST: ?Sized,
DT: ?Sized,
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for Json<T>
where
ST: ?Sized,
DT: ?Sized,
impl<T> Any for Json<T>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Json<T>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Json<T>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Json<T>
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> CryptoRng for Json<T>
where
T: DerefMut,
<T as Deref>::Target: CryptoRng,
impl<T> From<T> for Json<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> FromRef<T> for Json<T>
where
T: Clone,
fn from_ref(input: &T) -> T
impl<T> Instrument for Json<T>
impl<T> Read<Exclusive, BecauseExclusive> for Json<T>
where
T: ?Sized,
impl<T> RngCore for Json<T>
where
T: DerefMut,
<T as Deref>::Target: RngCore,
fn next_u32(&mut self) -> u32fn next_u64(&mut self) -> u64fn fill_bytes(&mut self, dst: &mut [u8])
impl<T> Same for Json<T>
type Output = T;
impl<T> ToOwned for Json<T>
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T> WithSubscriber for Json<T>
impl<T, U> Into<U> for Json<T>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Json<T>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Json<T>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
impl<V, T> VZip<V> for Json<T>
where
V: MultiLane<T>,
fn vzip(self) -> V