mirror of
https://github.com/actix/actix-web.git
synced 2024-11-27 12:01:15 +00:00
move json responder to separate module
This commit is contained in:
parent
bf23aa5d4b
commit
33b2be3281
3 changed files with 66 additions and 51 deletions
|
@ -2,8 +2,6 @@ use std::marker::PhantomData;
|
||||||
|
|
||||||
use actix::Actor;
|
use actix::Actor;
|
||||||
use futures::future::{Future, ok, err};
|
use futures::future::{Future, ok, err};
|
||||||
use serde_json;
|
|
||||||
use serde::Serialize;
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use http::{header, StatusCode, Error as HttpError};
|
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
|
/// Path normalization helper
|
||||||
///
|
///
|
||||||
/// By normalizing it means:
|
/// By normalizing it means:
|
||||||
|
@ -436,18 +398,6 @@ mod tests {
|
||||||
use http::{header, Method};
|
use http::{header, Method};
|
||||||
use application::Application;
|
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 {
|
fn index(_req: HttpRequest) -> HttpResponse {
|
||||||
HttpResponse::new(StatusCode::OK, Body::Empty)
|
HttpResponse::new(StatusCode::OK, Body::Empty)
|
||||||
}
|
}
|
||||||
|
|
63
src/json.rs
Normal file
63
src/json.rs
Normal 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -94,6 +94,7 @@ mod encoding;
|
||||||
mod httprequest;
|
mod httprequest;
|
||||||
mod httpresponse;
|
mod httpresponse;
|
||||||
mod info;
|
mod info;
|
||||||
|
mod json;
|
||||||
mod route;
|
mod route;
|
||||||
mod router;
|
mod router;
|
||||||
mod param;
|
mod param;
|
||||||
|
@ -119,10 +120,11 @@ pub mod pred;
|
||||||
pub mod payload;
|
pub mod payload;
|
||||||
pub use error::{Error, Result, ResponseError};
|
pub use error::{Error, Result, ResponseError};
|
||||||
pub use body::{Body, Binary};
|
pub use body::{Body, Binary};
|
||||||
|
pub use json::{Json};
|
||||||
pub use application::Application;
|
pub use application::Application;
|
||||||
pub use httprequest::HttpRequest;
|
pub use httprequest::HttpRequest;
|
||||||
pub use httpresponse::HttpResponse;
|
pub use httpresponse::HttpResponse;
|
||||||
pub use handler::{Reply, Responder, Json, NormalizePath};
|
pub use handler::{Reply, Responder, NormalizePath};
|
||||||
pub use route::Route;
|
pub use route::Route;
|
||||||
pub use resource::Resource;
|
pub use resource::Resource;
|
||||||
pub use server::HttpServer;
|
pub use server::HttpServer;
|
||||||
|
|
Loading…
Reference in a new issue