diff --git a/src/handler.rs b/src/handler.rs index 186237b4..934345da 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -2,8 +2,6 @@ use std::marker::PhantomData; use actix::Actor; use futures::future::{Future, ok, err}; -use serde_json; -use serde::Serialize; use regex::Regex; use http::{header, StatusCode, Error as HttpError}; @@ -280,42 +278,6 @@ impl RouteHandler for AsyncHandler } } -/// Json response helper -/// -/// The `Json` type allows you to respond with 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*. -/// -/// ```rust -/// # extern crate actix_web; -/// # #[macro_use] extern crate serde_derive; -/// # use actix_web::*; -/// # -/// #[derive(Serialize)] -/// struct MyObj { -/// name: String, -/// } -/// -/// fn index(req: HttpRequest) -> Result> { -/// Ok(Json(MyObj{name: req.match_info().query("name")?})) -/// } -/// # fn main() {} -/// ``` -pub struct Json (pub T); - -impl Responder for Json { - type Item = HttpResponse; - type Error = Error; - - fn respond_to(self, _: HttpRequest) -> Result { - let body = serde_json::to_string(&self.0)?; - - Ok(HttpResponse::Ok() - .content_type("application/json") - .body(body)?) - } -} - /// Path normalization helper /// /// By normalizing it means: @@ -436,18 +398,6 @@ mod tests { use http::{header, Method}; use application::Application; - #[derive(Serialize)] - struct MyObj { - name: &'static str, - } - - #[test] - fn test_json() { - let json = Json(MyObj{name: "test"}); - let resp = json.respond_to(HttpRequest::default()).unwrap(); - assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "application/json"); - } - fn index(_req: HttpRequest) -> HttpResponse { HttpResponse::new(StatusCode::OK, Body::Empty) } diff --git a/src/json.rs b/src/json.rs new file mode 100644 index 00000000..fd9d2d9f --- /dev/null +++ b/src/json.rs @@ -0,0 +1,63 @@ +use serde_json; +use serde::Serialize; + +use error::Error; +use handler::Responder; +use httprequest::HttpRequest; +use httpresponse::HttpResponse; + +/// Json response helper +/// +/// The `Json` type allows you to respond with 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*. +/// +/// ```rust +/// # extern crate actix_web; +/// # #[macro_use] extern crate serde_derive; +/// # use actix_web::*; +/// # +/// #[derive(Serialize)] +/// struct MyObj { +/// name: String, +/// } +/// +/// fn index(req: HttpRequest) -> Result> { +/// Ok(Json(MyObj{name: req.match_info().query("name")?})) +/// } +/// # fn main() {} +/// ``` +pub struct Json (pub T); + +impl Responder for Json { + type Item = HttpResponse; + type Error = Error; + + fn respond_to(self, _: HttpRequest) -> Result { + let body = serde_json::to_string(&self.0)?; + + Ok(HttpResponse::Ok() + .content_type("application/json") + .body(body)?) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use http::{header, Method}; + use application::Application; + + #[derive(Serialize)] + struct MyObj { + name: &'static str, + } + + #[test] + fn test_json() { + let json = Json(MyObj{name: "test"}); + let resp = json.respond_to(HttpRequest::default()).unwrap(); + assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "application/json"); + } + +} diff --git a/src/lib.rs b/src/lib.rs index 9a83907a..81b9fc2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,6 +94,7 @@ mod encoding; mod httprequest; mod httpresponse; mod info; +mod json; mod route; mod router; mod param; @@ -119,10 +120,11 @@ pub mod pred; pub mod payload; pub use error::{Error, Result, ResponseError}; pub use body::{Body, Binary}; +pub use json::{Json}; pub use application::Application; pub use httprequest::HttpRequest; pub use httpresponse::HttpResponse; -pub use handler::{Reply, Responder, Json, NormalizePath}; +pub use handler::{Reply, Responder, NormalizePath}; pub use route::Route; pub use resource::Resource; pub use server::HttpServer;