Use new 'JsonValue' type as return type of 'json!'.

Prior to this commit, a 'json!' invocation returned a value of type
'Value' from 'serde_json'. Because 'Value' does not implement
'Responder', most uses of 'json!' were wrapped in 'Json':
'Json(json!(..))`. By returning a crate-local 'JsonValue' type that
implements 'Responder', this repetition is resolved, and a 'json!' can
appear unwrapped.

This commit also removes the reexport of 'Value' from 'rocket_contrib'
as well as the default type of 'Value' for 'T' in 'Json<T>'.
This commit is contained in:
Sergio Benitez 2017-08-25 23:14:42 -07:00
parent 31fd24d027
commit a019f0d1f3
4 changed files with 109 additions and 27 deletions

View File

@ -12,7 +12,6 @@ use serde::de::DeserializeOwned;
use serde_json; use serde_json;
pub use serde_json::Value;
pub use serde_json::error::Error as SerdeError; pub use serde_json::error::Error as SerdeError;
/// The JSON type: implements `FromData` and `Responder`, allowing you to easily /// The JSON type: implements `FromData` and `Responder`, allowing you to easily
@ -67,7 +66,7 @@ pub use serde_json::error::Error as SerdeError;
/// json = 5242880 /// json = 5242880
/// ``` /// ```
#[derive(Debug)] #[derive(Debug)]
pub struct Json<T = Value>(pub T); pub struct Json<T>(pub T);
impl<T> Json<T> { impl<T> Json<T> {
/// Consumes the JSON wrapper and returns the wrapped item. /// Consumes the JSON wrapper and returns the wrapped item.
@ -135,6 +134,88 @@ impl<T> DerefMut for Json<T> {
} }
} }
/// An arbitrary JSON value.
///
/// This structure wraps `serde`'s [`Value`] type. Importantly, unlike `Value`,
/// this type implements [`Responder`], allowing a value of this type to be
/// returned directly from a handler.
///
/// [`Value`]: https://docs.rs/serde_json/1.0.2/serde_json/value/enum.Value.html
/// [`Responder`]: /rocket/response/trait.Responder.html
///
/// # `Responder`
///
/// The `Responder` implementation for `JsonValue` 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 constructed via the
/// [`json!`](/rocket_contrib/macro.json.html) macro. 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,ignore
/// use rocket_contrib::JsonValue;
///
/// #[get("/item")]
/// fn get_item() -> JsonValue {
/// json!({
/// "id": 83,
/// "values": [1, 2, 3, 4]
/// })
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Default)]
pub struct JsonValue(pub serde_json::Value);
impl JsonValue {
#[inline(always)]
fn into_inner(self) -> serde_json::Value {
self.0
}
}
impl Deref for JsonValue {
type Target = serde_json::Value;
#[inline(always)]
fn deref<'a>(&'a self) -> &'a Self::Target {
&self.0
}
}
impl DerefMut for JsonValue {
#[inline(always)]
fn deref_mut<'a>(&'a mut self) -> &'a mut Self::Target {
&mut self.0
}
}
impl Into<serde_json::Value> for JsonValue {
#[inline(always)]
fn into(self) -> serde_json::Value {
self.into_inner()
}
}
impl From<serde_json::Value> for JsonValue {
#[inline(always)]
fn from(value: serde_json::Value) -> JsonValue {
JsonValue(value)
}
}
/// Serializes the value into JSON. Returns a response with Content-Type JSON
/// and a fixed-size body with the serialized value.
impl<'a> Responder<'a> for JsonValue {
#[inline]
fn respond_to(self, req: &Request) -> response::Result<'a> {
content::Json(self.0.to_string()).respond_to(req)
}
}
/// A macro to create ad-hoc JSON serializable values using JSON syntax. /// A macro to create ad-hoc JSON serializable values using JSON syntax.
/// ///
/// # Usage /// # Usage
@ -146,25 +227,26 @@ impl<T> DerefMut for Json<T> {
/// #[macro_use] extern crate rocket_contrib; /// #[macro_use] extern crate rocket_contrib;
/// ``` /// ```
/// ///
/// The return type of a macro invocation is /// The return type of a `json!` invocation is
/// [`Value`](/rocket_contrib/enum.Value.html). This is the default type for the /// [`JsonValue`](/rocket_contrib/struct.JsonValue.html). A value created with
/// type parameter of [`Json`](/rocket_contrib/struct.Json.html) and as such, /// this macro can be returned from a handler as follows:
/// 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 /// ```rust,ignore
/// use rocket_contrib::Json; /// use rocket_contrib::JsonValue;
/// ///
/// #[get("/json")] /// #[get("/json")]
/// fn get_json() -> Json { /// fn get_json() -> JsonValue {
/// Json(json!({ /// json!({
/// "key": "value", /// "key": "value",
/// "array": [1, 2, 3, 4] /// "array": [1, 2, 3, 4]
/// })) /// })
/// } /// }
/// ``` /// ```
/// ///
/// The `Responder` implementation for `JsonValue` serializes the value into a
/// JSON string and sets it as the body of the response with a `Content-Type` of
/// `application/json`.
///
/// # Examples /// # Examples
/// ///
/// Create a simple JSON object with two keys: `"username"` and `"id"`: /// Create a simple JSON object with two keys: `"username"` and `"id"`:
@ -236,6 +318,6 @@ impl<T> DerefMut for Json<T> {
#[macro_export] #[macro_export]
macro_rules! json { macro_rules! json {
($($json:tt)+) => { ($($json:tt)+) => {
json_internal!($($json)+) $crate::JsonValue(json_internal!($($json)+))
}; };
} }

View File

@ -54,7 +54,7 @@ extern crate serde_json;
pub mod json; pub mod json;
#[cfg(feature = "json")] #[cfg(feature = "json")]
pub use json::{Json, SerdeError, Value}; pub use json::{Json, SerdeError, JsonValue};
#[cfg(feature = "msgpack")] #[cfg(feature = "msgpack")]
#[doc(hidden)] #[doc(hidden)]

View File

@ -8,7 +8,7 @@ extern crate serde_json;
#[cfg(test)] mod tests; #[cfg(test)] mod tests;
use rocket_contrib::{Json, Value}; use rocket_contrib::{Json, JsonValue};
use rocket::State; use rocket::State;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Mutex; use std::sync::Mutex;
@ -27,25 +27,25 @@ struct Message {
// TODO: This example can be improved by using `route` with multiple HTTP verbs. // TODO: This example can be improved by using `route` with multiple HTTP verbs.
#[post("/<id>", format = "application/json", data = "<message>")] #[post("/<id>", format = "application/json", data = "<message>")]
fn new(id: ID, message: Json<Message>, map: State<MessageMap>) -> Json<Value> { fn new(id: ID, message: Json<Message>, map: State<MessageMap>) -> JsonValue {
let mut hashmap = map.lock().expect("map lock."); let mut hashmap = map.lock().expect("map lock.");
if hashmap.contains_key(&id) { if hashmap.contains_key(&id) {
Json(json!({ json!({
"status": "error", "status": "error",
"reason": "ID exists. Try put." "reason": "ID exists. Try put."
})) })
} else { } else {
hashmap.insert(id, message.0.contents); hashmap.insert(id, message.0.contents);
Json(json!({ "status": "ok" })) json!({ "status": "ok" })
} }
} }
#[put("/<id>", format = "application/json", data = "<message>")] #[put("/<id>", format = "application/json", data = "<message>")]
fn update(id: ID, message: Json<Message>, map: State<MessageMap>) -> Option<Json<Value>> { fn update(id: ID, message: Json<Message>, map: State<MessageMap>) -> Option<JsonValue> {
let mut hashmap = map.lock().unwrap(); let mut hashmap = map.lock().unwrap();
if hashmap.contains_key(&id) { if hashmap.contains_key(&id) {
hashmap.insert(id, message.0.contents); hashmap.insert(id, message.0.contents);
Some(Json(json!({ "status": "ok" }))) Some(json!({ "status": "ok" }))
} else { } else {
None None
} }
@ -63,11 +63,11 @@ fn get(id: ID, map: State<MessageMap>) -> Option<Json<Message>> {
} }
#[error(404)] #[error(404)]
fn not_found() -> Json<Value> { fn not_found() -> JsonValue {
Json(json!({ json!({
"status": "error", "status": "error",
"reason": "Resource was not found." "reason": "Resource was not found."
})) })
} }
fn rocket() -> rocket::Rocket { fn rocket() -> rocket::Rocket {

View File

@ -107,12 +107,12 @@ code = '''
} }
#[put("/<id>", data = "<message>")] #[put("/<id>", data = "<message>")]
fn update(id: ID, message: Json<Message>) -> Json<Value> { fn update(id: ID, message: Json<Message>) -> JsonValue {
if DB.contains_key(&id) { if DB.contains_key(&id) {
DB.insert(id, &message.contents); DB.insert(id, &message.contents);
Json(json!{ "status": "ok" }) json!({ "status": "ok" })
} else { } else {
Json(json!{ "status": "error" }) json!({ "status": "error" })
} }
} }
''' '''