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

pub struct Json<T>(pub T);

The JSON type: implements FromData and Responder, allowing you to easily consume and respond with JSON.

Receiving JSON

If you're receiving JSON data, simply add a data parameter to your route arguments and ensure the type of the parameter is a Json<T>, where T is some type you'd like to parse from JSON. T must implement Deserialize or from serde. The data is parsed from the HTTP request body.

use rocket_contrib::json::Json;

#[post("/users", 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.

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

Methods

impl<T> Json<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> Deref for Json<T>
[src]

The resulting type after dereferencing.

Dereferences the value.

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

Mutably dereferences the value.

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

Formats the value using the given formatter. Read more

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

The associated error to be returned when the guard fails.

The owned type returned from [FromData::transform()]. Read more

The borrowed type consumed by [FromData::from_data()] when [FromData::transform()] returns a [Transform::Borrowed]. Read more

Transforms data into a value of type Self::Owned. Read more

Validates, parses, and converts the incoming request body data into an instance of Self. Read more

impl<'a, T: Serialize> Responder<'a> 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.

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

Auto Trait Implementations

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

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

Blanket Implementations

impl<T> From for T
[src]

Performs the conversion.

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

Performs the conversion.

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

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

Immutably borrows from an owned value. Read more

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

🔬 This is a nightly-only experimental API. (get_type_id)

this method will likely be replaced by an associated static

Gets the TypeId of self. Read more

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

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

Mutably borrows from an owned value. Read more

impl<T> Typeable for T where
    T: Any

Get the TypeId of this object.

impl<'a, T> FromData for T where
    T: FromDataSimple
[src]

The associated error to be returned when the guard fails.

The owned type returned from [FromData::transform()]. Read more

The borrowed type consumed by [FromData::from_data()] when [FromData::transform()] returns a [Transform::Borrowed]. Read more

Transforms data into a value of type Self::Owned. Read more

Validates, parses, and converts the incoming request body data into an instance of Self. Read more

impl<T> IntoCollection for T

Converts self into a collection.

impl<T, I> AsResult for T where
    I: Input, 

impl<T> IntoSql for T

Convert self to an expression for Diesel's query builder. Read more

Convert &self to an expression for Diesel's query builder. Read more

impl<T> Same for T

Should always be Self

impl<T> Erased for T