Struct rocket_contrib::Json [−][src]
pub struct Json<T = Value>(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
DeserializeOwned
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 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.
#[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]
impl<T> Json<T>
pub fn into_inner(self) -> T
[src]
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
impl<T: Debug> Debug for Json<T>
[src]
impl<T: Debug> Debug for Json<T>
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<T: DeserializeOwned> FromData for Json<T>
[src]
impl<T: DeserializeOwned> FromData for Json<T>
type Error = SerdeError
The associated error to be returned when the guard fails.
fn from_data(request: &Request, data: Data) -> Outcome<Self, SerdeError>
[src]
fn from_data(request: &Request, data: Data) -> Outcome<Self, SerdeError>
Validates, parses, and converts an instance of Self
from the incoming request body data. Read more
impl<T: Serialize> Responder<'static> for Json<T>
[src]
impl<T: Serialize> Responder<'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.
fn respond_to(self, req: &Request) -> Result<'static>
[src]
fn respond_to(self, req: &Request) -> Result<'static>
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]
impl<T> Deref for Json<T>
type Target = T
The resulting type after dereferencing.
ⓘImportant traits for &'a mut Rfn deref<'a>(&'a self) -> &'a T
[src]
fn deref<'a>(&'a self) -> &'a T
Dereferences the value.
impl<T> DerefMut for Json<T>
[src]
impl<T> DerefMut for Json<T>