1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
use serde::{Deserialize, Serialize};
use serde_json::json;

use crate::body::Body;
use crate::http::StatusCode;

#[derive(Debug, Deserialize, Serialize)]
pub struct Json<T>(pub T);

impl<T: Serialize> Body for Json<T> {
    fn content_type(&self) -> String {
        "application/json".to_string()
    }

    fn status(&self) -> StatusCode {
        StatusCode::Ok
    }

    fn contents(&self) -> String {
        json!(self.0).to_string()
    }
}