Struct ajars_actix_web::actix_web::web::Json [−][src]
pub struct Json<T>(pub T);
Expand description
JSON extractor and responder.
Json
has two uses: JSON responses, and extracting typed data from JSON request payloads.
Extractor
To extract typed data from a request body, the inner type T
must implement the
serde::Deserialize
trait.
Use JsonConfig
to configure extraction process.
use actix_web::{post, web, App}; use serde::Deserialize; #[derive(Deserialize)] struct Info { username: String, } /// deserialize `Info` from request's body #[post("/")] async fn index(info: web::Json<Info>) -> String { format!("Welcome {}!", info.username) }
Responder
The Json
type JSON formatted responses. A handler may return a value of type
Json<T>
where T
is the type of a structure to serialize into JSON. The type T
must
implement serde::Serialize
.
use actix_web::{post, web, HttpRequest}; use serde::Serialize; #[derive(Serialize)] struct Info { name: String, } #[post("/{name}")] async fn index(req: HttpRequest) -> web::Json<Info> { web::Json(Info { name: req.match_info().get("name").unwrap().to_owned(), }) }
Implementations
Unwrap into inner T
value.
Trait Implementations
See here for example of usage as an extractor.
type Future = JsonExtractFut<T>
type Future = JsonExtractFut<T>
Future that resolves to a Self.
type Config = JsonConfig
type Config = JsonConfig
Configuration for this extractor.
pub fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <Json<T> as FromRequest>::Future
pub fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <Json<T> as FromRequest>::Future
Create a Self from request parts asynchronously.
Create a Self from request head asynchronously. Read more
Creates response with OK status code, correct content type header, and serialized JSON payload.
If serialization failed
pub fn respond_to(self, &HttpRequest) -> HttpResponse<AnyBody>ⓘNotable traits for HttpResponse<AnyBody>
impl Future for HttpResponse<AnyBody> type Output = Result<Response<AnyBody>, Error>;
pub fn respond_to(self, &HttpRequest) -> HttpResponse<AnyBody>ⓘNotable traits for HttpResponse<AnyBody>
impl Future for HttpResponse<AnyBody> type Output = Result<Response<AnyBody>, Error>;
Convert self to HttpResponse
.
Override a status code for a Responder. Read more
Insert header to the final response. Read more
pub fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
pub fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
Serialize this value into the given Serde serializer. Read more
Auto Trait Implementations
impl<T> RefUnwindSafe for Json<T> where
T: RefUnwindSafe,
impl<T> UnwindSafe for Json<T> where
T: UnwindSafe,