1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use ;
use ;
use DeserializeOwned;
use Serialize;
use serde_json;
use ;
/// A wrapper, indicating a type should be automatically (de)serialized from/to a HTTP
/// request/response body.
///
/// This feature requires a dependency on [`serde`]. This dependency is enabled by default, but can
/// be disabled by removing the optional `serde` feature on the `aitch` crate.
///
/// To use a type with this wrapper, it must implement both [`serde::de::DeserializeOwned`] and
/// [`serde::Serialize`]. This is because `Json<T>` can be used in both request/response bodies, and
/// because chains of handlers may choose to serialize/deserialize the type back into a stream of
/// bytes in order to process the requests. (See the [`Body`] trait for more information).
///
/// [`serde`]: https://serde.rs
/// [`Body`]: trait.Body.html
/// [`serde::de::DeserializeOwned`]: https://docs.serde.rs/serde/de/trait.DeserializeOwned.html
/// [`serde::Serialize`]: https://docs.serde.rs/serde/trait.Serialize.html
///
/// # Examples
///
/// The following example accepts a request with a JSON payload, and uses this to construct a JSON
/// response:
///
/// ```
/// extern crate aitch;
/// extern crate http;
/// extern crate serde;
/// #[macro_use] extern crate serde_derive;
///
/// use aitch::servers::hyper::Server;
/// use aitch::{middlewares, Json, Responder, ResponseBuilder, Result};
/// use http::Request;
///
/// #[derive(Serialize, Deserialize)]
/// struct ReqParameters {
/// message: String,
/// }
///
/// #[derive(Serialize, Deserialize)]
/// struct RespBody {
/// message: String,
/// }
///
/// fn handler(req: Request<Json<ReqParameters>>, mut resp: ResponseBuilder) -> impl Responder {
/// let req_params = req.into_body().json();
/// let message = format!("You sent '{}'", req_params.message);
/// let body = RespBody { message };
///
/// resp.header(http::header::CONTENT_TYPE, "application/json")
/// .body(Json(body))
/// }
/// ```
;
type JsonFuture<T> =
AndThen;