Struct rocket_contrib::JSON [] [src]

pub struct JSON<T>(pub T);

The JSON type, which implements FromData and Responder. This type allows you to trivially consume and respond with JSON in your Rocket application.

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

#[post("/users/", format = "application/json", data = "<user>")]
fn new_user(user: JSON<User>) {
    ...
}

You don't need to use format = "application/json", but it may be what you want. Using format = application/json means that any request that doesn't specify "application/json" as its first Content-Type: header parameter will not be routed to this handler.

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.

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

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.unwrap(), "Hello".to_string());

Trait Implementations

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

Formats the value using the given formatter.

impl<T: Deserialize> FromData for JSON<T>
[src]

The associated error to be returned when parsing fails.

Parses an instance of Self from the incoming request body data. Read more

impl<T: Serialize> Responder<'static> for JSON<T>
[src]

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

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

The resulting type after dereferencing

The method called to dereference a value

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

The method called to mutably dereference a value