Struct rocket_contrib::json::Json

source ·
pub struct Json<T>(pub T);
Expand description

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

Tuple Fields§

§0: T

Implementations§

source§

impl<T> Json<T>

source

pub fn into_inner(self) -> T

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§

source§

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

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<'a, T: Deserialize<'a>> FromData<'a> for Json<T>

source§

type Error = JsonError<'a>

The associated error to be returned when the guard fails.
source§

type Owned = String

The owned type returned from FromData::transform(). Read more
source§

type Borrowed = str

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

fn transform( r: &Request<'_>, d: Data, ) -> Transform<Outcome<Self::Owned, Self::Error>>

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

fn from_data( _: &Request<'_>, o: Transformed<'a, Self>, ) -> Outcome<Self, Self::Error>

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

impl<'a, T: Serialize> Responder<'a> for Json<T>

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.

source§

fn respond_to(self, req: &Request<'_>) -> Result<'a>

Returns Ok if a Response could be generated successfully. Otherwise, returns an Err with a failing Status. 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> 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, I> AsResult<T, I> for T
where I: Input,

source§

fn as_result(self) -> Result<T, ParseErr<I>>

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
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> IntoCollection<T> for T

§

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

Converts self into a collection.
§

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

source§

impl<T> IntoSql for T

source§

fn into_sql<T>(self) -> Self::Expression
where Self: Sized + AsExpression<T>,

Convert self to an expression for Diesel’s query builder. Read more
source§

fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
where &'a Self: AsExpression<T>,

Convert &self to an expression for Diesel’s query builder. Read more
source§

impl<T> Same for T

source§

type Output = T

Should always be Self
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.
source§

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

source§

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

source§

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

source§

impl<T> Typeable for T
where T: Any,

source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.
source§

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

source§

fn vzip(self) -> V