#[repr(transparent)]pub struct Json<T>(pub T);
json
only.Expand description
The JSON guard: easily consume and return JSON.
Sending JSON
To respond with serialized 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;
#[get("/users/<id>")]
fn user(id: usize) -> Json<User> {
let user_from_id = User::from(id);
/* ... */
Json(user_from_id)
}
Receiving JSON
Json
is both a data guard and a form guard.
Data Guard
To deserialize 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;
#[derive(FromForm)]
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
:
[global.limits]
json = 5242880
Tuple Fields§
§0: T
Implementations§
Trait Implementations§
source§impl<'r, T: Deserialize<'r>> FromData<'r> for Json<T>
impl<'r, T: Deserialize<'r>> FromData<'r> for Json<T>
source§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
Self: 'async_trait,
'r: 'async_trait,
'life0: 'async_trait,
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 Self: 'async_trait, 'r: 'async_trait, 'life0: 'async_trait,
Self
from the incoming request body data. Read moresource§impl<'v, T: Deserialize<'v> + Send> FromFormField<'v> for Json<T>
impl<'v, T: Deserialize<'v> + Send> FromFormField<'v> for Json<T>
source§fn from_value(field: ValueField<'v>) -> Result<Self, Errors<'v>>
fn from_value(field: ValueField<'v>) -> Result<Self, Errors<'v>>
T
from a form value field. Read moresource§impl<'a, T: Serialize> FromUriParam<Query, &'a T> for Json<T>
impl<'a, T: Serialize> FromUriParam<Query, &'a T> for Json<T>
source§fn from_uri_param(param: &'a T) -> Self::Target
fn from_uri_param(param: &'a T) -> Self::Target
T
into a value of type Self::Target
. The
resulting value of type Self::Target
will be rendered into a URI using
its UriDisplay
implementation.source§impl<'a, T: Serialize> FromUriParam<Query, &'a mut T> for Json<T>
impl<'a, T: Serialize> FromUriParam<Query, &'a mut T> for Json<T>
source§fn from_uri_param(param: &'a mut T) -> Self::Target
fn from_uri_param(param: &'a mut T) -> Self::Target
T
into a value of type Self::Target
. The
resulting value of type Self::Target
will be rendered into a URI using
its UriDisplay
implementation.source§impl<'x, T: Serialize> FromUriParam<Query, &'x Json<T>> for Json<T>
impl<'x, T: Serialize> FromUriParam<Query, &'x Json<T>> for Json<T>
source§fn from_uri_param(param: &'x Json<T>) -> &'x Json<T>
fn from_uri_param(param: &'x Json<T>) -> &'x Json<T>
T
into a value of type Self::Target
. The
resulting value of type Self::Target
will be rendered into a URI using
its UriDisplay
implementation.source§impl<'x, T: Serialize> FromUriParam<Query, &'x mut Json<T>> for Json<T>
impl<'x, T: Serialize> FromUriParam<Query, &'x mut Json<T>> for Json<T>
source§fn from_uri_param(param: &'x mut Json<T>) -> &'x mut Json<T>
fn from_uri_param(param: &'x mut Json<T>) -> &'x mut Json<T>
T
into a value of type Self::Target
. The
resulting value of type Self::Target
will be rendered into a URI using
its UriDisplay
implementation.source§impl<T: Serialize> FromUriParam<Query, Json<T>> for Json<T>
impl<T: Serialize> FromUriParam<Query, Json<T>> for Json<T>
source§fn from_uri_param(param: Json<T>) -> Json<T>
fn from_uri_param(param: Json<T>) -> Json<T>
T
into a value of type Self::Target
. The
resulting value of type Self::Target
will be rendered into a URI using
its UriDisplay
implementation.source§impl<T: Serialize> FromUriParam<Query, T> for Json<T>
impl<T: Serialize> FromUriParam<Query, T> for Json<T>
source§fn from_uri_param(param: T) -> Self::Target
fn from_uri_param(param: T) -> Self::Target
T
into a value of type Self::Target
. The
resulting value of type Self::Target
will be rendered into a URI using
its UriDisplay
implementation.source§impl<T: Ord> Ord for Json<T>
impl<T: Ord> Ord for Json<T>
source§impl<T: PartialEq> PartialEq<Json<T>> for Json<T>
impl<T: PartialEq> PartialEq<Json<T>> for Json<T>
source§impl<T: PartialOrd> PartialOrd<Json<T>> for Json<T>
impl<T: PartialOrd> PartialOrd<Json<T>> for Json<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<'r, T: Serialize> Responder<'r, 'static> for Json<T>
impl<'r, T: Serialize> Responder<'r, 'static> 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.
impl<T: Eq> Eq for Json<T>
impl<T> StructuralEq for Json<T>
impl<T> StructuralPartialEq for Json<T>
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<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedExplicit<'a> for Twhere T: 'a,
§impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedImplicit<'a> for Twhere T: 'a,
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> FromFd for Twhere
T: From<OwnedFd>,
impl<T> FromFd for Twhere T: From<OwnedFd>,
§impl<T> FromFilelike for Twhere
T: From<OwnedFd>,
impl<T> FromFilelike for Twhere T: From<OwnedFd>,
§fn from_filelike(owned: OwnedFd) -> T
fn from_filelike(owned: OwnedFd) -> T
Self
from the given filelike object. Read more§fn from_into_filelike<Owned>(owned: Owned) -> Twhere
Owned: IntoFilelike,
fn from_into_filelike<Owned>(owned: Owned) -> Twhere Owned: IntoFilelike,
Self
from the given filelike object
converted from into_owned
. Read more§impl<T> FromSocketlike for Twhere
T: From<OwnedFd>,
impl<T> FromSocketlike for Twhere T: From<OwnedFd>,
§fn from_socketlike(owned: OwnedFd) -> T
fn from_socketlike(owned: OwnedFd) -> T
Self
from the given socketlike object.§fn from_into_socketlike<Owned>(owned: Owned) -> Twhere
Owned: IntoSocketlike,
fn from_into_socketlike<Owned>(owned: Owned) -> Twhere Owned: IntoSocketlike,
Self
from the given socketlike object
converted from into_owned
.