Struct rocket_contrib::json::Json[][src]

pub struct Json<T>(pub T);

The JSON data guard: easily consume and respond with 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_contrib::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_contrib::json::Json;

#[derive(FromForm)]
struct User<'r> {
    name: &'r str,
    metadata: Json<Metadata>
}

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

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_contrib::json::Json;

#[get("/users/<id>")]
fn user(id: usize) -> Json<User> {
    let user_from_id = User::from(id);
    /* ... */
    Json(user_from_id)
}

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:

[global.limits]
json = 5242880

Implementations

impl<T> Json<T>[src]

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

Consumes the JSON wrapper and returns the wrapped item.

Example

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]

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

type Target = T

The resulting type after dereferencing.

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

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

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

type Error = JsonError<'r>

The associated error to be returned when the guard fails.

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

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.

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<!> for T[src]

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

impl<'v, T> FromForm<'v> for T where
    T: FromFormField<'v>, 
[src]

type Context = FromFieldContext<'v, T>

The form guard’s parsing context.

impl<T> Instrument for T[src]

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

impl<T> IntoCollection<T> for T

impl<T> IntoSql for T

impl<T> Same<T> for T

type Output = T

Should always be Self

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>,