Struct rocket::serde::json::Json[][src]

pub struct Json<T>(pub T);
This is supported on crate feature json only.
Expand description

The JSON guard: easily consume and return JSON.

Receiving JSON

Json is both a data guard and a form guard.

Data Guard

To parse request body data as JSON , add a data route argument with a target type of Json<T>, where T is some type you’d like to parse from JSON. T must implement serde::Deserialize.

use rocket::serde::json::Json;

#[post("/user", format = "json", data = "<user>")]
fn new_user(user: Json<User>) {
    /* ... */

You don’t need to use format = "json", but it may be what you want. Using format = json means that any request that doesn’t specify “application/json” as its Content-Type header value will not be routed to the handler.

Form Guard

Json<T>, as a form guard, accepts value and data fields and parses the data as a T. Simple use Json<T>:

use rocket::form::{Form, FromForm};
use rocket::serde::json::Json;

struct User<'r> {
    name: &'r str,
    metadata: Json<Metadata>

#[post("/user", data = "<form>")]
fn new_user(form: Form<User<'_>>) {
    /* ... */

Incoming Data Limits

The default size limit for incoming JSON data is 1MiB. Setting a limit protects your application from denial of service (DoS) attacks and from resource exhaustion through high memory consumption. The limit can be increased by setting the limits.json configuration parameter. For instance, to increase the JSON limit to 5MiB for all environments, you may add the following to your Rocket.toml:

json = 5242880

Sending JSON

If you’re responding with JSON data, return a Json<T> type, where T implements Serialize from serde. The content type of the response is set to application/json automatically.

use rocket::serde::json::Json;

fn user(id: usize) -> Json<User> {
    let user_from_id = User::from(id);
    /* ... */


impl<T> Json<T>[src]

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

Consumes the JSON wrapper and returns the wrapped item.


let string = "Hello".to_string();
let my_json = Json(string);
assert_eq!(my_json.into_inner(), "Hello".to_string());

Trait Implementations

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

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

Formats the value using the given formatter. Read more

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

type Target = T

The resulting type after dereferencing.

fn deref(&self) -> &T[src]

Dereferences the value.

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

fn deref_mut(&mut self) -> &mut T[src]

Mutably dereferences the value.

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

fn from(value: T) -> Self[src]

Performs the conversion.

impl<'r, T: Deserialize<'r>> FromData<'r> for Json<T>[src]

type Error = Error<'r>

The associated error to be returned when the guard fails.

fn from_data<'life0, 'async_trait>(
    req: &'r Request<'life0>,
    data: Data<'r>
) -> Pin<Box<dyn Future<Output = Outcome<'r, Self>> + Send + 'async_trait>> where
    'r: 'async_trait,
    'life0: 'async_trait,
    Self: 'async_trait, 

Asynchronously validates, parses, and converts an instance of Self from the incoming request body data. Read more

impl<'v, T: Deserialize<'v> + Send> FromFormField<'v> for Json<T>[src]

fn from_value(field: ValueField<'v>) -> Result<Self, Errors<'v>>[src]

Parse a value of T from a form value field. Read more

fn from_data<'life0, 'async_trait>(
    f: DataField<'v, 'life0>
) -> Pin<Box<dyn Future<Output = Result<Self, Errors<'v>>> + Send + 'async_trait>> where
    'v: 'async_trait,
    'life0: 'async_trait,
    Self: 'async_trait, 

Parse a value of T from a form data field. Read more

fn default() -> Option<Self>[src]

Returns a default value, if any exists, to be used during lenient parsing when the form field is missing. Read more

impl<'r, T: Serialize> Responder<'r, 'static> for Json<T>[src]

Serializes the wrapped value into JSON. Returns a response with Content-Type JSON and a fixed-size body with the serialized value. If serialization fails, an Err of Status::InternalServerError is returned.

fn respond_to(self, req: &'r Request<'_>) -> Result<'static>[src]

Returns Ok if a Response could be generated successfully. Otherwise, returns an Err with a failing Status. Read more

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

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

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

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

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

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

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

pub fn from(t: !) -> T[src]

Performs the conversion.

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

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

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

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> IntoCollection<T> for T

pub fn into_collection<A>(self) -> SmallVec<A> where
    A: Array<Item = T>, 

Converts self into a collection.

pub fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
    F: FnMut(T) -> U,
    A: Array<Item = U>, 

impl<T> Same<T> for T

type Output = T

Should always be Self

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

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

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

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

The type returned in the event of a conversion error.

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

Performs the conversion.

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

pub fn vzip(self) -> V