//! Json extractor/responder use std::rc::Rc; use std::{fmt, ops}; use bytes::Bytes; use futures::{Future, Stream}; use serde::de::DeserializeOwned; use serde::Serialize; use serde_json; use actix_http::dev::JsonBody; use actix_http::error::{Error, JsonPayloadError}; use actix_http::http::StatusCode; use actix_http::Response; use crate::extract::FromRequest; use crate::request::HttpRequest; use crate::responder::Responder; use crate::service::ServiceFromRequest; /// Json helper /// /// Json can be used for two different purpose. First is for json response /// generation and second is for extracting typed information from request's /// payload. /// /// To extract typed information from request's body, the type `T` must /// implement the `Deserialize` trait from *serde*. /// /// [**JsonConfig**](struct.JsonConfig.html) allows to configure extraction /// process. /// /// ## Example /// /// ```rust /// #[macro_use] extern crate serde_derive; /// use actix_web::{web, App}; /// /// #[derive(Deserialize)] /// struct Info { /// username: String, /// } /// /// /// deserialize `Info` from request's body /// fn index(info: web::Json) -> String { /// format!("Welcome {}!", info.username) /// } /// /// fn main() { /// let app = App::new().service( /// web::resource("/index.html").route( /// web::post().to(index)) /// ); /// } /// ``` /// /// 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 /// # #[macro_use] extern crate serde_derive; /// # use actix_web::*; /// # /// #[derive(Serialize)] /// struct MyObj { /// name: String, /// } /// /// fn index(req: HttpRequest) -> Result> { /// Ok(web::Json(MyObj { /// name: req.match_info().get("name").unwrap().to_string(), /// })) /// } /// # fn main() {} /// ``` pub struct Json(pub T); impl Json { /// Deconstruct to an inner value pub fn into_inner(self) -> T { self.0 } } impl ops::Deref for Json { type Target = T; fn deref(&self) -> &T { &self.0 } } impl ops::DerefMut for Json { fn deref_mut(&mut self) -> &mut T { &mut self.0 } } impl fmt::Debug for Json where T: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Json: {:?}", self.0) } } impl fmt::Display for Json where T: fmt::Display, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) } } impl Responder for Json { type Error = Error; type Future = Result; fn respond_to(self, _: &HttpRequest) -> Self::Future { let body = match serde_json::to_string(&self.0) { Ok(body) => body, Err(e) => return Err(e.into()), }; Ok(Response::build(StatusCode::OK) .content_type("application/json") .body(body)) } } /// Json extractor. Allow to extract typed information from request's /// payload. /// /// To extract typed information from request's body, the type `T` must /// implement the `Deserialize` trait from *serde*. /// /// [**JsonConfig**](struct.JsonConfig.html) allows to configure extraction /// process. /// /// ## Example /// /// ```rust /// #[macro_use] extern crate serde_derive; /// use actix_web::{web, App}; /// /// #[derive(Deserialize)] /// struct Info { /// username: String, /// } /// /// /// deserialize `Info` from request's body /// fn index(info: web::Json) -> String { /// format!("Welcome {}!", info.username) /// } /// /// fn main() { /// let app = App::new().service( /// web::resource("/index.html").route( /// web::post().to(index)) /// ); /// } /// ``` impl FromRequest

for Json where T: DeserializeOwned + 'static, P: Stream + 'static, { type Error = Error; type Future = Box>; #[inline] fn from_request(req: &mut ServiceFromRequest

) -> Self::Future { let req2 = req.clone(); let (limit, err) = req .load_config::() .map(|c| (c.limit, c.ehandler.clone())) .unwrap_or((32768, None)); Box::new( JsonBody::new(req) .limit(limit) .map_err(move |e| { if let Some(err) = err { (*err)(e, &req2) } else { e.into() } }) .map(Json), ) } } /// Json extractor configuration /// /// ```rust /// #[macro_use] extern crate serde_derive; /// use actix_web::{error, web, App, HttpResponse}; /// /// #[derive(Deserialize)] /// struct Info { /// username: String, /// } /// /// /// deserialize `Info` from request's body, max payload size is 4kb /// fn index(info: web::Json) -> String { /// format!("Welcome {}!", info.username) /// } /// /// fn main() { /// let app = App::new().service( /// web::resource("/index.html").route( /// web::post().config( /// // change json extractor configuration /// web::JsonConfig::default().limit(4096) /// .error_handler(|err, req| { // <- create custom error response /// error::InternalError::from_response( /// err, HttpResponse::Conflict().finish()).into() /// })) /// .to(index)) /// ); /// } /// ``` #[derive(Clone)] pub struct JsonConfig { limit: usize, ehandler: Option Error>>, } impl JsonConfig { /// Change max size of payload. By default max size is 32Kb pub fn limit(mut self, limit: usize) -> Self { self.limit = limit; self } /// Set custom error handler pub fn error_handler(mut self, f: F) -> Self where F: Fn(JsonPayloadError, &HttpRequest) -> Error + 'static, { self.ehandler = Some(Rc::new(f)); self } } impl Default for JsonConfig { fn default() -> Self { JsonConfig { limit: 32768, ehandler: None, } } }