Skip to main content

Json

Struct Json 

Source
pub struct Json<T>(pub T);
Expand description

Json helper

Json can be used for two different purpose. First is for json response generation and second is for extracting typed information from request’s payload.

To extract typed information from request’s body, the type T must implement the Deserialize trait from serde.

JsonConfig allows to configure extraction process.

§Example

use ntex::web;

#[derive(serde::Deserialize)]
struct Info {
    username: String,
}

/// deserialize `Info` from request's body
async fn index(info: web::types::Json<Info>) -> String {
    format!("Welcome {}!", info.username)
}

fn main() {
    let app = web::App::default().service(
       web::resource("/index.html").route(
           web::post().to(index))
    );
}

The Json type allows you to respond with well-formed JSON data: simply return a value of type Json<T> where T is the type of a structure to serialize into JSON. The type T must implement the Serialize trait from serde.

use ntex::web;

#[derive(serde::Serialize)]
struct MyObj {
    name: String,
}

fn index(req: web::HttpRequest) -> Result<web::types::Json<MyObj>, std::io::Error> {
    Ok(web::types::Json(MyObj {
        name: req.match_info().get("name").unwrap().to_string(),
    }))
}

Tuple Fields§

§0: T

Implementations§

Source§

impl<T> Json<T>

Source

pub fn into_inner(self) -> T

Deconstruct to an inner value

Trait Implementations§

Source§

impl<T> Debug for Json<T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for Json<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T> DerefMut for Json<T>

Source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
Source§

impl<T> Display for Json<T>
where T: Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<St, T> FromRequest<St> for Json<T>
where St: State, T: DeserializeOwned + 'static,

Json extractor. Allow to extract typed information from request’s payload.

To extract typed information from request’s body, the type T must implement the Deserialize trait from serde.

JsonConfig allows to configure extraction process.

§Example
use ntex::web;

#[derive(serde::Deserialize)]
struct Info {
    username: String,
}

/// deserialize `Info` from request's body
async fn index(info: web::types::Json<Info>) -> String {
    format!("Welcome {}!", info.username)
}

fn main() {
    let app = web::App::default().service(
        web::resource("/index.html").route(
           web::post().to(index))
    );
}
Source§

type Error = JsonPayloadError

The error returned when extraction fails. Read more
Source§

async fn from_request( _: &St, req: &HttpRequest, payload: &mut Payload, ) -> Result<Self, Self::Error>

Creates the extractor value for this request.
Source§

impl<St, T: Serialize> Responder<St> for Json<T>
where St: State, JsonError: WebResponseError<St, St::Error>,

Source§

async fn respond_to(self, st: &St, _: &HttpRequest) -> Response

Builds an HTTP response from this value.
Source§

fn with_status(self, status: StatusCode) -> CustomResponder<Self, St>
where Self: Sized,

Replaces the status code produced by this responder. Read more
Source§

fn with_header<K, V>(self, key: K, value: V) -> CustomResponder<Self, St>

Sets a header on the response produced by this responder. Read more

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Formattable for T
where T: Deref, <T as Deref>::Target: Formattable,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Parsable for T
where T: Deref, <T as Deref>::Target: Parsable,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.