[][src]Struct hyperdrive::body::Json

pub struct Json<T: DeserializeOwned + Send + 'static>(pub T);

Decodes a JSON-encoded request body.

The FromBody implementation of this type will retrieve the request body and decode it as JSON using serde_json. The Content-Type and Content-Length headers are ignored.

Examples

#[derive(Deserialize)]
struct BodyData {
    id: u32,
    names: Vec<String>,
}

#[derive(FromRequest)]
enum Route {
    #[post("/json")]
    Index {
        #[body]
        data: Json<BodyData>,
    },
}

let data = r#"
{
    "id": 123,
    "names": [
        "Joachim",
        "Johannes",
        "Jonathan"
    ]
}
"#;

let Route::Index { data: Json(body) } = Route::from_request_sync(
    http::Request::post("/json").body(data.into()).unwrap(),
    NoContext,
).unwrap();

assert_eq!(body.id, 123);
assert_eq!(body.names, vec![
    "Joachim".to_string(),
    "Johannes".to_string(),
    "Jonathan".to_string(),
]);

Trait Implementations

impl<T: DeserializeOwned + Send + 'static> FromBody for Json<T>[src]

type Context = NoContext

A context parameter passed to [from_body]. Read more

type Result = DefaultFuture<Self, BoxedError>

The result returned by [from_body]. Read more

impl<T: PartialEq + DeserializeOwned + Send + 'static> PartialEq<Json<T>> for Json<T>[src]

impl<T: Eq + DeserializeOwned + Send + 'static> Eq for Json<T>[src]

impl<T: DeserializeOwned + Send + 'static> DerefMut for Json<T>[src]

impl<T: DeserializeOwned + Send + 'static> Deref for Json<T>[src]

type Target = T

The resulting type after dereferencing.

impl<T: Debug + DeserializeOwned + Send + 'static> Debug for Json<T>[src]

Auto Trait Implementations

impl<T> Unpin for Json<T> where
    T: Unpin

impl<T> Sync for Json<T> where
    T: Sync

impl<T> Send for Json<T>

impl<T> UnwindSafe for Json<T> where
    T: UnwindSafe

impl<T> RefUnwindSafe for Json<T> where
    T: RefUnwindSafe

Blanket Implementations

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

impl<T, U> Into<U> for T where
    U: From<T>, 
[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<T> Borrow<T> for T where
    T: ?Sized
[src]

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

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

impl<Q, K> Equivalent<K> for Q where
    K: Borrow<Q> + ?Sized,
    Q: Eq + ?Sized
[src]

impl<T> Erased for T