Struct actix_web::web::Json[][src]

pub struct Json<T>(pub T);

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

impl<T> Json<T>[src]

pub fn into_inner(self) -> T[src]

Unwrap into inner T value.

Trait Implementations

impl<T: Debug> Debug for Json<T>[src]

impl<T> Deref for Json<T>[src]

type Target = T

The resulting type after dereferencing.

impl<T> DerefMut for Json<T>[src]

impl<T> Display for Json<T> where
    T: Display
[src]

impl<T> FromRequest for Json<T> where
    T: DeserializeOwned + 'static, 
[src]

See here for example of usage as an extractor.

type Error = Error

The associated error which can be returned.

type Future = JsonExtractFut<T>

Future that resolves to a Self.

type Config = JsonConfig

Configuration for this extractor.

impl<T: Serialize> Responder for Json<T>[src]

Creates response with OK status code, correct content type header, and serialized JSON payload.

If serialization failed

impl<T> Serialize for Json<T> where
    T: Serialize
[src]

Auto Trait Implementations

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> UnwindSafe for Json<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,