diff --git a/contrib/src/json.rs b/contrib/src/json.rs index f0338e7e..fe00f029 100644 --- a/contrib/src/json.rs +++ b/contrib/src/json.rs @@ -21,14 +21,14 @@ pub use serde_json::error::Error as SerdeError; /// ## 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`, where `T` is +/// arguments and ensure the type of the parameter is a `Json`, where `T` is /// some type you'd like to parse from JSON. `T` must implement `Deserialize` or /// `DeserializeOwned` from [Serde](https://github.com/serde-rs/json). The data /// is parsed from the HTTP request body. /// /// ```rust,ignore /// #[post("/users/", format = "application/json", data = "")] -/// fn new_user(user: JSON) { +/// fn new_user(user: Json) { /// ... /// } /// ``` @@ -40,16 +40,16 @@ pub use serde_json::error::Error as SerdeError; /// /// ## Sending JSON /// -/// If you're responding with JSON data, return a `JSON` type, where `T` +/// If you're responding with JSON data, return a `Json` type, where `T` /// implements `Serialize` from [Serde](https://github.com/serde-rs/json). The /// content type of the response is set to `application/json` automatically. /// /// ```rust,ignore /// #[get("/users/")] -/// fn user(id: usize) -> JSON { +/// fn user(id: usize) -> Json { /// let user_from_id = User::from(id); /// ... -/// JSON(user_from_id) +/// Json(user_from_id) /// } /// ``` /// @@ -67,16 +67,16 @@ pub use serde_json::error::Error as SerdeError; /// json = 5242880 /// ``` #[derive(Debug)] -pub struct JSON(pub T); +pub struct Json(pub T); -impl JSON { +impl Json { /// Consumes the JSON wrapper and returns the wrapped item. /// /// # Example /// ```rust - /// # use rocket_contrib::JSON; + /// # use rocket_contrib::Json; /// let string = "Hello".to_string(); - /// let my_json = JSON(string); + /// let my_json = Json(string); /// assert_eq!(my_json.into_inner(), "Hello".to_string()); /// ``` #[inline(always)] @@ -88,7 +88,7 @@ impl JSON { /// Default limit for JSON is 1MB. const LIMIT: u64 = 1 << 20; -impl FromData for JSON { +impl FromData for Json { type Error = SerdeError; fn from_data(request: &Request, data: Data) -> data::Outcome { @@ -99,7 +99,7 @@ impl FromData for JSON { let size_limit = request.limits().get("json").unwrap_or(LIMIT); serde_json::from_reader(data.open().take(size_limit)) - .map(|val| JSON(val)) + .map(|val| Json(val)) .map_err(|e| { error_!("Couldn't parse JSON body: {:?}", e); e }) .into_outcome(Status::BadRequest) } @@ -108,7 +108,7 @@ impl FromData for JSON { /// 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 Responder<'static> for JSON { +impl Responder<'static> for Json { fn respond_to(self, req: &Request) -> response::Result<'static> { serde_json::to_string(&self.0).map(|string| { content::JSON(string).respond_to(req).unwrap() @@ -119,7 +119,7 @@ impl Responder<'static> for JSON { } } -impl Deref for JSON { +impl Deref for Json { type Target = T; #[inline(always)] @@ -128,7 +128,7 @@ impl Deref for JSON { } } -impl DerefMut for JSON { +impl DerefMut for Json { #[inline(always)] fn deref_mut<'a>(&'a mut self) -> &'a mut T { &mut self.0 @@ -147,17 +147,18 @@ impl DerefMut for JSON { /// ``` /// /// The return type of a macro invocation is -/// [Value](/rocket_contrib/enum.Value.html). This is the default value for the -/// type parameter of [JSON](/rocket_contrib/struct.JSON.html) and as such, you -/// can return `JSON` without specifying the type. A value created with this -/// macro can be returned from a handler as follows: +/// [`Value`](/rocket_contrib/enum.Value.html). This is the default type for the +/// type parameter of [`Json`](/rocket_contrib/struct.Json.html) and as such, +/// you can return `Json` without specifying the type using a `json!` value for +/// `Json`. A value created with this macro can be returned from a handler as +/// follows: /// /// ```rust,ignore -/// use rocket_contrib::JSON; +/// use rocket_contrib::Json; /// /// #[get("/json")] -/// fn get_json() -> JSON { -/// JSON(json!({ +/// fn get_json() -> Json { +/// Json(json!({ /// "key": "value", /// "array": [1, 2, 3, 4] /// })) diff --git a/contrib/src/lib.rs b/contrib/src/lib.rs index 060373bb..74237adf 100644 --- a/contrib/src/lib.rs +++ b/contrib/src/lib.rs @@ -17,7 +17,7 @@ //! common modules exposed by default. The present feature list is below, with //! an asterisk next to the features that are enabled by default: //! -//! * [json*](struct.JSON.html) +//! * [json*](struct.Json.html) //! * [msgpack](struct.MsgPack.html) //! * [handlebars_templates](struct.Template.html) //! * [tera_templates](struct.Template.html) @@ -54,7 +54,7 @@ extern crate serde_json; pub mod json; #[cfg(feature = "json")] -pub use json::{JSON, SerdeError, Value}; +pub use json::{Json, SerdeError, Value}; #[cfg(feature = "msgpack")] #[doc(hidden)] diff --git a/examples/json/src/main.rs b/examples/json/src/main.rs index 4f4d1002..a05c5185 100644 --- a/examples/json/src/main.rs +++ b/examples/json/src/main.rs @@ -8,7 +8,7 @@ extern crate serde_json; #[cfg(test)] mod tests; -use rocket_contrib::{JSON, Value}; +use rocket_contrib::{Json, Value}; use rocket::State; use std::collections::HashMap; use std::sync::Mutex; @@ -27,35 +27,35 @@ struct Message { // TODO: This example can be improved by using `route` with multiple HTTP verbs. #[post("/", format = "application/json", data = "")] -fn new(id: ID, message: JSON, map: State) -> JSON { +fn new(id: ID, message: Json, map: State) -> Json { let mut hashmap = map.lock().expect("map lock."); if hashmap.contains_key(&id) { - JSON(json!({ + Json(json!({ "status": "error", "reason": "ID exists. Try put." })) } else { hashmap.insert(id, message.0.contents); - JSON(json!({ "status": "ok" })) + Json(json!({ "status": "ok" })) } } #[put("/", format = "application/json", data = "")] -fn update(id: ID, message: JSON, map: State) -> Option> { +fn update(id: ID, message: Json, map: State) -> Option> { let mut hashmap = map.lock().unwrap(); if hashmap.contains_key(&id) { hashmap.insert(id, message.0.contents); - Some(JSON(json!({ "status": "ok" }))) + Some(Json(json!({ "status": "ok" }))) } else { None } } #[get("/", format = "application/json")] -fn get(id: ID, map: State) -> Option> { +fn get(id: ID, map: State) -> Option> { let hashmap = map.lock().unwrap(); hashmap.get(&id).map(|contents| { - JSON(Message { + Json(Message { id: Some(id), contents: contents.clone() }) @@ -63,8 +63,8 @@ fn get(id: ID, map: State) -> Option> { } #[error(404)] -fn not_found() -> JSON { - JSON(json!({ +fn not_found() -> Json { + Json(json!({ "status": "error", "reason": "Resource was not found." })) diff --git a/lib/src/data/from_data.rs b/lib/src/data/from_data.rs index 01034afd..22b98362 100644 --- a/lib/src/data/from_data.rs +++ b/lib/src/data/from_data.rs @@ -139,7 +139,7 @@ impl<'a, S, E> IntoOutcome for Result { /// } /// ``` /// -/// `Person` has a custom serialization format, so the built-in `JSON` type +/// `Person` has a custom serialization format, so the built-in `Json` type /// doesn't suffice. The format is `:` with `Content-Type: /// application/x-person`. You'd like to use `Person` as a `FromData` type so /// that you can retrieve it directly from a client's request body: diff --git a/lib/src/request/request.rs b/lib/src/request/request.rs index 95ceb1ce..d58799ed 100644 --- a/lib/src/request/request.rs +++ b/lib/src/request/request.rs @@ -430,12 +430,12 @@ impl<'r> Request<'r> { /// /// # Example /// - /// Invoke the `JSON` request guard. + /// Invoke the `Json` request guard. /// /// ```rust,ignore - /// use rocket_contrib::JSON; + /// use rocket_contrib::Json; /// - /// let outcome = request.guard::>(); + /// let outcome = request.guard::>(); /// ``` /// /// Retrieve managed state inside of a guard implementation: diff --git a/lib/src/response/mod.rs b/lib/src/response/mod.rs index 4d20569c..486f5299 100644 --- a/lib/src/response/mod.rs +++ b/lib/src/response/mod.rs @@ -15,9 +15,9 @@ //! //! # Contrib //! -//! The [contrib crate](/rocket_contrib) contains several useful `Responder`s -//! including [Template](/rocket_contrib/struct.Template.html) and -//! [JSON](/rocket_contrib/struct.JSON.html). +//! The [`contrib` crate](/rocket_contrib) contains several useful `Responder`s +//! including [`Template`](/rocket_contrib/struct.Template.html) and +//! [`Json`](/rocket_contrib/struct.Json.html). mod responder; mod redirect; diff --git a/site/guide/requests.md b/site/guide/requests.md index 9f9fc091..edbce0c9 100644 --- a/site/guide/requests.md +++ b/site/guide/requests.md @@ -233,7 +233,7 @@ As an example, consider the following route: ```rust #[post("/user", format = "application/json", data = "")] -fn new_user(user: JSON) -> T { ... } +fn new_user(user: Json) -> T { ... } ``` The `format` parameter in the `post` attribute declares that only incoming @@ -250,7 +250,7 @@ As an example, consider the following route: ```rust #[get("/user/", format = "application/json")] -fn user(id: usize) -> JSON { ... } +fn user(id: usize) -> Json { ... } ``` The `format` parameter in the `get` attribute declares that only incoming @@ -630,7 +630,7 @@ examples on GitHub provide further illustrations. ### JSON Handling JSON data is no harder: simply use the -[`JSON`](https://api.rocket.rs/rocket_contrib/struct.JSON.html) type: +[`Json`](https://api.rocket.rs/rocket_contrib/struct.Json.html) type: ```rust #[derive(Deserialize)] @@ -640,10 +640,10 @@ struct Task { } #[post("/todo", data = "")] -fn new(task: JSON) -> String { ... } +fn new(task: Json) -> String { ... } ``` -The only condition is that the generic type in `JSON` implements the +The only condition is that the generic type in `Json` implements the `Deserialize` trait from [Serde](https://github.com/serde-rs/json). See the [JSON example] on GitHub for a complete example. diff --git a/site/guide/responses.md b/site/guide/responses.md index 80d879bb..34cdab99 100644 --- a/site/guide/responses.md +++ b/site/guide/responses.md @@ -54,13 +54,13 @@ an `&'static str` to JSON, you can use the [`content::JSON`] type as follows: use rocket::response::content; #[get("/")] -fn json() -> content::JSON<&'static str> { - content::JSON("{ 'hi': 'world' }") +fn json() -> content::Json<&'static str> { + content::Json("{ 'hi': 'world' }") } ``` [`Accepted`]: https://api.rocket.rs/rocket/response/status/struct.Accepted.html -[`content::JSON`]: https://api.rocket.rs/rocket/response/content/struct.JSON.html +[`content::JSON`]: https://api.rocket.rs/rocket/response/content/struct.Json.html ### Errors @@ -210,7 +210,7 @@ fn stream() -> io::Result> { ### JSON The [`JSON`] responder in [`rocket_contrib`] allows you to easily respond with -well-formed JSON data: simply return a value of type `JSON` where `T` is the +well-formed JSON data: simply return a value of type `Json` where `T` is the type of a structure to serialize into JSON. The type `T` must implement the [`Serialize`] trait from [`serde`], which can be automatically derived. @@ -218,13 +218,13 @@ An an example, to respond with the JSON value of a `Task` structure, we might write: ```rust -use rocket_contrib::JSON; +use rocket_contrib::Json; #[derive(Serialize)] struct Task { ... } #[get("/todo")] -fn todo() -> JSON { ... } +fn todo() -> Json { ... } ``` The `JSON` type serializes the structure into JSON, sets the Content-Type to @@ -233,7 +233,7 @@ fails, a **500 - Internal Server Error** is returned. The [JSON example on GitHub] provides further illustration. -[`JSON`]: https://api.rocket.rs/rocket_contrib/struct.JSON.html +[`JSON`]: https://api.rocket.rs/rocket_contrib/struct.Json.html [`Serialize`]: https://docs.serde.rs/serde/trait.Serialize.html [`serde`]: https://docs.serde.rs/serde/ [JSON example on GitHub]: https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/json diff --git a/site/guide/state.md b/site/guide/state.md index e4a1c0b7..4a498e2c 100644 --- a/site/guide/state.md +++ b/site/guide/state.md @@ -287,9 +287,9 @@ array of some `Task` structures that are fetched from a database: ```rust #[get("/tasks")] -fn get_tasks(conn: DbConn) -> QueryResult>> { +fn get_tasks(conn: DbConn) -> QueryResult>> { all_tasks.order(tasks::id.desc()) .load::(&conn) - .map(|tasks| JSON(tasks)) + .map(|tasks| Json(tasks)) } ``` diff --git a/site/index.toml b/site/index.toml index 73cd6531..49a4ff89 100644 --- a/site/index.toml +++ b/site/index.toml @@ -107,12 +107,12 @@ code = ''' } #[put("/", data = "")] - fn update(id: ID, message: JSON) -> JSON { + fn update(id: ID, message: Json) -> Json { if DB.contains_key(&id) { DB.insert(id, &message.contents); - JSON(json!{ "status": "ok" }) + Json(json!{ "status": "ok" }) } else { - JSON(json!{ "status": "error" }) + Json(json!{ "status": "error" }) } } ''' diff --git a/site/overview.toml b/site/overview.toml index c31e571e..44d04ebb 100644 --- a/site/overview.toml +++ b/site/overview.toml @@ -73,9 +73,9 @@ the `user_form` parameter. The Rocket type that knows how to parse web forms into structures. Rocket will automatically attempt to parse the request body into the `Form` and call the `login` handler if parsing succeeds. Other built-in `FromData` types include -[Data](https://api.rocket.rs/rocket/struct.Data.html), -[JSON](https://api.rocket.rs/rocket_contrib/struct.JSON.html), and -[Flash](https://api.rocket.rs/rocket/response/struct.Flash.html) +[`Data`](https://api.rocket.rs/rocket/struct.Data.html), +[`Json`](https://api.rocket.rs/rocket_contrib/struct.Json.html), and +[`Flash`](https://api.rocket.rs/rocket/response/struct.Flash.html) ''' [[panels]] @@ -219,7 +219,7 @@ If the function above is used as a handler, for instance, then the type `T` must implement `Responder`. Rocket provides many useful responder types out of the box. They include: - * `JSON`: Serializes the structure T into JSON and returns it to + * `Json`: Serializes the structure T into JSON and returns it to the client. * `Template`: Renders a template file and returns it to the client. * `Redirect`: Returns a properly formatted HTTP redirect.