Struct Json
struct Json<T>(257)
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;
Implementations
impl<T> Json<T>
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>.
impl<H, T> HandlerWithoutStateExt for Json<T>
fn into_service(self: Self) -> HandlerService<H, T, ()>fn into_make_service(self: Self) -> IntoMakeService<HandlerService<H, T, ()>>fn into_make_service_with_connect_info<C>(self: Self) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
impl<P, T> Receiver for Json<T>
impl<R> Rng for Json<T>
impl<R> TryCryptoRng for Json<T>
impl<R> TryRngCore for Json<T>
fn try_next_u32(self: &mut Self) -> Result<u32, <R as TryRngCore>::Error>fn try_next_u64(self: &mut Self) -> Result<u64, <R as TryRngCore>::Error>fn try_fill_bytes(self: &mut Self, dst: &mut [u8]) -> Result<(), <R as TryRngCore>::Error>
impl<T> Any for Json<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Json<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Json<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Json<T>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> CryptoRng for Json<T>
impl<T> Deref for Json<T>
fn deref(self: &Self) -> &<Self as >::Target
impl<T> DerefMut for Json<T>
fn deref_mut(self: &mut Self) -> &mut <Self as >::Target
impl<T> Freeze for Json<T>
impl<T> From for Json<T>
fn from(inner: T) -> Self
impl<T> From for Json<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> From for Json<T>
fn from(t: never) -> T
impl<T> FromRef for Json<T>
fn from_ref(input: &T) -> T
impl<T> Instrument for Json<T>
impl<T> IntoResponse for Json<T>
fn into_response(self: Self) -> Response
impl<T> RefUnwindSafe for Json<T>
impl<T> RngCore for Json<T>
fn next_u32(self: &mut Self) -> u32fn next_u64(self: &mut Self) -> u64fn fill_bytes(self: &mut Self, dst: &mut [u8])
impl<T> Same for Json<T>
impl<T> Send for Json<T>
impl<T> Sync for Json<T>
impl<T> ToOwned for Json<T>
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T> Unpin for Json<T>
impl<T> UnsafeUnpin for Json<T>
impl<T> UnwindSafe for Json<T>
impl<T> WithSubscriber for Json<T>
impl<T, S> FromRequest for Json<T>
async fn from_request(req: Request, state: &S) -> Result<Self, <Self as >::Rejection>
impl<T, S> OptionalFromRequest for Json<T>
async fn from_request(req: Request, state: &S) -> Result<Option<Self>, <Self as >::Rejection>
impl<T, U> Into for Json<T>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Json<T>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Json<T>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T: $crate::clone::Clone> Clone for Json<T>
fn clone(self: &Self) -> Json<T>
impl<T: $crate::default::Default> Default for Json<T>
fn default() -> Json<T>
impl<T: $crate::fmt::Debug> Debug for Json<T>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<T: $crate::marker::Copy> Copy for Json<T>
impl<V, T> VZip for Json<T>
fn vzip(self: Self) -> V