1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

move json responder to separate module

This commit is contained in:
Nikolay Kim 2017-12-20 17:51:28 -08:00
parent bf23aa5d4b
commit 33b2be3281
3 changed files with 66 additions and 51 deletions

View File

@ -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<S, H, F, R, E> RouteHandler<S> for AsyncHandler<S, H, F, R, E>
}
}
/// Json response helper
///
/// The `Json` type allows you to respond with well-formed JSON data: simply return a value of
/// type Json<T> 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<Json<MyObj>> {
/// Ok(Json(MyObj{name: req.match_info().query("name")?}))
/// }
/// # fn main() {}
/// ```
pub struct Json<T: Serialize> (pub T);
impl<T: Serialize> Responder for Json<T> {
type Item = HttpResponse;
type Error = Error;
fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, Error> {
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)
}

63
src/json.rs Normal file
View File

@ -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<T> 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<Json<MyObj>> {
/// Ok(Json(MyObj{name: req.match_info().query("name")?}))
/// }
/// # fn main() {}
/// ```
pub struct Json<T: Serialize> (pub T);
impl<T: Serialize> Responder for Json<T> {
type Item = HttpResponse;
type Error = Error;
fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, Error> {
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");
}
}

View File

@ -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;