tower_web/response/
json.rs1use error;
2use response::{Context, Response, Serializer};
3use util::BufStream;
4
5use bytes::Bytes;
6use http;
7use http::header::{self, HeaderValue};
8use serde_json::{self, Value};
9
10impl Response for Value {
11 type Buf = <Self::Body as BufStream>::Item;
12 type Body = error::Map<Bytes>;
13
14 fn into_http<S>(self, context: &Context<S>) -> Result<http::Response<Self::Body>, ::Error>
15 where
16 S: Serializer,
17 {
18 let body = serde_json::to_vec(&self).unwrap();
20
21 let body = error::Map::new(Bytes::from(body));
23
24 let mut response = http::Response::builder()
25 .status(200)
27 .body(body)
28 .unwrap();
29
30 response
31 .headers_mut()
32 .entry(header::CONTENT_TYPE)
33 .unwrap()
34 .or_insert_with(|| {
35 context.content_type_header()
36 .map(|content_type| content_type.clone())
37 .unwrap_or_else(|| {
38 HeaderValue::from_static("application/json")
39 })
40 });
41
42 Ok(response)
43 }
44}