1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
//! Automatic JSON (de)serialization support.
//!
//! See [`Json`] for details.
//!
//! # Enabling
//!
//! This module is only available when the `json` feature is enabled. Enable it
//! in `Cargo.toml` as follows:
//!
//! ```toml
//! [dependencies.rocket]
//! version = "0.6.0-dev"
//! features = ["json"]
//! ```
//!
//! # Testing
//!
//! The [`LocalRequest`] and [`LocalResponse`] types provide [`json()`] and
//! [`into_json()`] methods to create a request with serialized JSON and
//! deserialize a response as JSON, respectively.
//!
//! [`LocalRequest`]: crate::local::blocking::LocalRequest
//! [`LocalResponse`]: crate::local::blocking::LocalResponse
//! [`json()`]: crate::local::blocking::LocalRequest::json()
//! [`into_json()`]: crate::local::blocking::LocalResponse::into_json()
use std::{io, fmt, error};
use std::ops::{Deref, DerefMut};
use crate::request::{Request, local_cache};
use crate::data::{Limits, Data, FromData, Outcome};
use crate::response::{self, Responder, content};
use crate::form::prelude as form;
use crate::http::uri::fmt::{UriDisplay, FromUriParam, Query, Formatter as UriFormatter};
use crate::http::Status;
use serde::{Serialize, Deserialize};
#[doc(hidden)]
pub use serde_json;
/// 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.
///
/// ```rust
/// # #[macro_use] extern crate rocket;
/// # type User = usize;
/// 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`].
///
/// ```rust
/// # #[macro_use] extern crate rocket;
/// # type User = usize;
/// 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>`:
///
/// ```rust
/// # #[macro_use] extern crate rocket;
/// # type Metadata = usize;
/// 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`:
///
/// ```toml
/// [global.limits]
/// json = 5242880
/// ```
#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Json<T>(pub T);
/// Error returned by the [`Json`] guard when JSON deserialization fails.
#[derive(Debug)]
pub enum Error<'a> {
/// An I/O error occurred while reading the incoming request data.
Io(io::Error),
/// The client's data was received successfully but failed to parse as valid
/// JSON or as the requested type. The `&str` value in `.0` is the raw data
/// received from the user, while the `Error` in `.1` is the deserialization
/// error from `serde`.
Parse(&'a str, serde_json::error::Error),
}
impl<'a> fmt::Display for Error<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(err) => write!(f, "i/o error: {}", err),
Self::Parse(_, err) => write!(f, "parse error: {}", err),
}
}
}
impl<'a> error::Error for Error<'a> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Self::Io(err) => Some(err),
Self::Parse(_, err) => Some(err),
}
}
}
impl<T> Json<T> {
/// Consumes the JSON wrapper and returns the wrapped item.
///
/// # Example
/// ```rust
/// # use rocket::serde::json::Json;
/// let string = "Hello".to_string();
/// let my_json = Json(string);
/// assert_eq!(my_json.into_inner(), "Hello".to_string());
/// ```
#[inline(always)]
pub fn into_inner(self) -> T {
self.0
}
}
impl<'r, T: Deserialize<'r>> Json<T> {
fn from_str(s: &'r str) -> Result<Self, Error<'r>> {
serde_json::from_str(s).map(Json).map_err(|e| Error::Parse(s, e))
}
async fn from_data(req: &'r Request<'_>, data: Data<'r>) -> Result<Self, Error<'r>> {
let limit = req.limits().get("json").unwrap_or(Limits::JSON);
let string = match data.open(limit).into_string().await {
Ok(s) if s.is_complete() => s.into_inner(),
Ok(_) => {
let eof = io::ErrorKind::UnexpectedEof;
return Err(Error::Io(io::Error::new(eof, "data limit exceeded")));
},
Err(e) => return Err(Error::Io(e)),
};
Self::from_str(local_cache!(req, string))
}
}
#[crate::async_trait]
impl<'r, T: Deserialize<'r>> FromData<'r> for Json<T> {
type Error = Error<'r>;
async fn from_data(req: &'r Request<'_>, data: Data<'r>) -> Outcome<'r, Self> {
match Self::from_data(req, data).await {
Ok(value) => Outcome::Success(value),
Err(Error::Io(e)) if e.kind() == io::ErrorKind::UnexpectedEof => {
Outcome::Error((Status::PayloadTooLarge, Error::Io(e)))
},
Err(Error::Parse(s, e)) if e.classify() == serde_json::error::Category::Data => {
Outcome::Error((Status::UnprocessableEntity, Error::Parse(s, e)))
},
Err(e) => Outcome::Error((Status::BadRequest, e)),
}
}
}
/// 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<'r, T: Serialize> Responder<'r, 'static> for Json<T> {
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
let string = serde_json::to_string(&self.0)
.map_err(|e| {
error!("JSON serialize failure: {}", e);
Status::InternalServerError
})?;
content::RawJson(string).respond_to(req)
}
}
impl<T: Serialize> UriDisplay<Query> for Json<T> {
fn fmt(&self, f: &mut UriFormatter<'_, Query>) -> fmt::Result {
let string = to_string(&self.0).map_err(|_| fmt::Error)?;
f.write_value(&string)
}
}
macro_rules! impl_from_uri_param_from_inner_type {
($($lt:lifetime)?, $T:ty) => (
impl<$($lt,)? T: Serialize> FromUriParam<Query, $T> for Json<T> {
type Target = Json<$T>;
#[inline(always)]
fn from_uri_param(param: $T) -> Self::Target {
Json(param)
}
}
)
}
impl_from_uri_param_from_inner_type!(, T);
impl_from_uri_param_from_inner_type!('a, &'a T);
impl_from_uri_param_from_inner_type!('a, &'a mut T);
crate::http::impl_from_uri_param_identity!([Query] (T: Serialize) Json<T>);
impl<T> From<T> for Json<T> {
fn from(value: T) -> Self {
Json(value)
}
}
impl<T> Deref for Json<T> {
type Target = T;
#[inline(always)]
fn deref(&self) -> &T {
&self.0
}
}
impl<T> DerefMut for Json<T> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl From<Error<'_>> for form::Error<'_> {
fn from(e: Error<'_>) -> Self {
match e {
Error::Io(e) => e.into(),
Error::Parse(_, e) => form::Error::custom(e)
}
}
}
#[crate::async_trait]
impl<'v, T: Deserialize<'v> + Send> form::FromFormField<'v> for Json<T> {
fn from_value(field: form::ValueField<'v>) -> Result<Self, form::Errors<'v>> {
Ok(Self::from_str(field.value)?)
}
async fn from_data(f: form::DataField<'v, '_>) -> Result<Self, form::Errors<'v>> {
Ok(Self::from_data(f.request, f.data).await?)
}
}
/// Serializes the value into JSON. Returns a response with Content-Type JSON
/// and a fixed-size body with the serialized value.
impl<'r> Responder<'r, 'static> for Value {
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
content::RawJson(self.to_string()).respond_to(req)
}
}
crate::export! {
/// A macro to create ad-hoc JSON serializable values using JSON syntax.
///
/// The return type of a `json!` invocation is [`Value`](Value). A value
/// created with this macro can be returned from a handler as follows:
///
/// ```rust
/// # #[macro_use] extern crate rocket;
/// use rocket::serde::json::{json, Value};
///
/// #[get("/json")]
/// fn get_json() -> Value {
/// json!({
/// "key": "value",
/// "array": [1, 2, 3, 4]
/// })
/// }
/// ```
///
/// The [`Responder`](crate::response::Responder) implementation for
/// `Value` serializes the value into a JSON string and sets it as the body
/// of the response with a `Content-Type` of `application/json`.
///
/// # Examples
///
/// Create a simple JSON object with two keys: `"username"` and `"id"`:
///
/// ```rust
/// use rocket::serde::json::json;
///
/// let value = json!({
/// "username": "mjordan",
/// "id": 23
/// });
/// ```
///
/// Create a more complex object with a nested object and array:
///
/// ```rust
/// # use rocket::serde::json::json;
/// let value = json!({
/// "code": 200,
/// "success": true,
/// "payload": {
/// "features": ["serde", "json"],
/// "ids": [12, 121],
/// },
/// });
/// ```
///
/// Variables or expressions can be interpolated into the JSON literal. Any type
/// interpolated into an array element or object value must implement serde's
/// `Serialize` trait, while any type interpolated into a object key must
/// implement `Into<String>`.
///
/// ```rust
/// # use rocket::serde::json::json;
/// let code = 200;
/// let features = vec!["serde", "json"];
///
/// let value = json!({
/// "code": code,
/// "success": code == 200,
/// "payload": {
/// features[0]: features[1]
/// }
/// });
/// ```
///
/// Trailing commas are allowed inside both arrays and objects.
///
/// ```rust
/// # use rocket::serde::json::json;
/// let value = json!([
/// "notice",
/// "the",
/// "trailing",
/// "comma -->",
/// ]);
/// ```
macro_rules! json {
($($json:tt)+) => ($crate::serde::json::serde_json::json!($($json)*));
}
}
/// An arbitrary JSON value as returned by [`json!`].
///
/// # `Responder`
///
/// `Value` is a `Responder` that serializes the represented value into a JSON
/// string and sets the string as the body of a fixed-sized response with a
/// `Content-Type` of `application/json`.
///
/// # Usage
///
/// A value of this type is returned by [`json!`]. The macro and this type are
/// typically used to construct JSON values in an ad-hoc fashion during request
/// handling. This looks something like:
///
/// ```rust
/// # #[macro_use] extern crate rocket;
/// use rocket::serde::json::{json, Value};
///
/// #[get("/json")]
/// fn get_json() -> Value {
/// json!({
/// "id": 83,
/// "values": [1, 2, 3, 4]
/// })
/// }
/// ```
#[doc(inline)]
pub use serde_json::Value;
/// Deserialize an instance of type `T` from bytes of JSON text.
///
/// **_Always_ use [`Json`] to deserialize JSON request data.**
///
/// # Example
///
/// ```
/// use rocket::serde::{Deserialize, json};
///
/// #[derive(Debug, PartialEq, Deserialize)]
/// #[serde(crate = "rocket::serde")]
/// struct Data<'r> {
/// framework: &'r str,
/// stars: usize,
/// }
///
/// let bytes = br#"
/// {
/// "framework": "Rocket",
/// "stars": 5
/// }
/// "#;
///
/// let data: Data = json::from_slice(bytes).unwrap();
/// assert_eq!(data, Data { framework: "Rocket", stars: 5, });
/// ```
///
/// # Errors
///
/// This conversion can fail if the structure of the input does not match the
/// structure expected by `T`, for example if `T` is a struct type but the input
/// contains something other than a JSON map. It can also fail if the structure
/// is correct but `T`'s implementation of `Deserialize` decides that something
/// is wrong with the data, for example required struct fields are missing from
/// the JSON map or some number is too big to fit in the expected primitive
/// type.
#[inline(always)]
pub fn from_slice<'a, T>(slice: &'a [u8]) -> Result<T, serde_json::error::Error>
where T: Deserialize<'a>,
{
serde_json::from_slice(slice)
}
/// Deserialize an instance of type `T` from a string of JSON text.
///
/// **_Always_ use [`Json`] to deserialize JSON request data.**
///
/// # Example
///
/// ```
/// use rocket::serde::{Deserialize, json};
///
/// #[derive(Debug, PartialEq, Deserialize)]
/// #[serde(crate = "rocket::serde")]
/// struct Data<'r> {
/// framework: &'r str,
/// stars: usize,
/// }
///
/// let string = r#"
/// {
/// "framework": "Rocket",
/// "stars": 5
/// }
/// "#;
///
/// let data: Data = json::from_str(string).unwrap();
/// assert_eq!(data, Data { framework: "Rocket", stars: 5, });
/// ```
///
/// # Errors
///
/// This conversion can fail if the structure of the input does not match the
/// structure expected by `T`, for example if `T` is a struct type but the input
/// contains something other than a JSON map. It can also fail if the structure
/// is correct but `T`'s implementation of `Deserialize` decides that something
/// is wrong with the data, for example required struct fields are missing from
/// the JSON map or some number is too big to fit in the expected primitive
/// type.
#[inline(always)]
pub fn from_str<'a, T>(string: &'a str) -> Result<T, serde_json::error::Error>
where T: Deserialize<'a>,
{
serde_json::from_str(string)
}
/// Serialize a `T` into a JSON string with compact representation.
///
/// **_Always_ use [`Json`] to serialize JSON response data.**
///
/// # Example
///
/// ```
/// use rocket::serde::{Deserialize, Serialize, json};
///
/// #[derive(Debug, PartialEq, Deserialize, Serialize)]
/// #[serde(crate = "rocket::serde")]
/// struct Data<'r> {
/// framework: &'r str,
/// stars: usize,
/// }
///
/// let data = Data {
/// framework: "Rocket",
/// stars: 5,
/// };
///
/// let string = json::to_string(&data).unwrap();
/// let data: Data = json::from_str(&string).unwrap();
/// assert_eq!(data, Data { framework: "Rocket", stars: 5, });
/// ```
///
/// # Errors
///
/// Serialization fails if `T`'s `Serialize` implementation fails or if `T`
/// contains a map with non-string keys.
#[inline(always)]
pub fn to_string<T>(value: &T) -> Result<String, serde_json::error::Error>
where T: Serialize
{
serde_json::to_string(value)
}
/// Serialize a `T` into a JSON string with "pretty" formatted representation.
///
/// **_Always_ use [`Json`] to serialize JSON response data.**
///
/// # Example
///
/// ```
/// use rocket::serde::{Deserialize, Serialize, json};
///
/// #[derive(Debug, PartialEq, Deserialize, Serialize)]
/// #[serde(crate = "rocket::serde")]
/// struct Data<'r> {
/// framework: &'r str,
/// stars: usize,
/// }
///
/// let data = Data {
/// framework: "Rocket",
/// stars: 5,
/// };
///
/// let string = json::to_pretty_string(&data).unwrap();
/// # let compact = json::to_string(&data).unwrap();
/// # assert_ne!(compact, string);
/// let data: Data = json::from_str(&string).unwrap();
/// assert_eq!(data, Data { framework: "Rocket", stars: 5, });
/// ```
///
/// # Errors
///
/// Serialization fails if `T`'s `Serialize` implementation fails or if `T`
/// contains a map with non-string keys.
#[inline(always)]
pub fn to_pretty_string<T>(value: &T) -> Result<String, serde_json::error::Error>
where T: Serialize
{
serde_json::to_string_pretty(value)
}
/// Interpret a [`Value`] as an instance of type `T`.
///
/// # Example
///
/// ```
/// use rocket::serde::{Deserialize, json};
///
/// #[derive(Debug, PartialEq, Deserialize)]
/// #[serde(crate = "rocket::serde")]
/// struct Data {
/// framework: String ,
/// stars: usize,
/// }
///
/// let value = json::json!({
/// "framework": "Rocket",
/// "stars": 5
/// });
///
/// let data: Data = json::from_value(value).unwrap();
/// assert_eq!(data, Data { framework: "Rocket".into(), stars: 5, });
/// ```
///
/// # Errors
///
/// This conversion can fail if the structure of the input does not match the
/// structure expected by `T`, for example if `T` is a struct type but the input
/// contains something other than a JSON map. It can also fail if the structure
/// is correct but `T`'s implementation of `Deserialize` decides that something
/// is wrong with the data, for example required struct fields are missing from
/// the JSON map or some number is too big to fit in the expected primitive
/// type.
#[inline(always)]
pub fn from_value<T>(value: Value) -> Result<T, serde_json::error::Error>
where T: crate::serde::DeserializeOwned
{
serde_json::from_value(value)
}
/// Convert a `T` into a [`Value`], an opaque value representing JSON data.
///
/// # Example
///
/// ```
/// use rocket::serde::{Deserialize, Serialize, json};
///
/// #[derive(Deserialize, Serialize)]
/// #[serde(crate = "rocket::serde")]
/// struct Data {
/// framework: String ,
/// stars: usize,
/// }
///
/// let value = json::json!({
/// "framework": "Rocket",
/// "stars": 5
/// });
///
/// let data: Data = json::from_value(value.clone()).unwrap();
/// let data_value = json::to_value(data).unwrap();
/// assert_eq!(value, data_value);
/// ```
///
/// # Errors
///
/// This conversion fails if `T`’s implementation of `Serialize` decides to fail
/// or if `T` contains a map with non-string keys.
#[inline(always)]
pub fn to_value<T>(item: T) -> Result<Value, serde_json::error::Error>
where T: Serialize
{
serde_json::to_value(item)
}