use bytes::Bytes;
use http_body_util::Full;
use mime::APPLICATION_JSON;
use crate::http::HeaderValue;
use super::TypedBody;
pub struct Json(Bytes);
impl Json {
pub fn new<T>(value: T) -> Result<Self, JsonSerializationError>
where
T: serde::Serialize,
{
let bytes = serde_json::to_vec(&value).map_err(JsonSerializationError)?;
Ok(Self(bytes.into()))
}
}
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct JsonSerializationError(serde_json::Error);
impl TypedBody for Json {
type Body = Full<Bytes>;
fn content_type(&self) -> HeaderValue {
HeaderValue::from_static(APPLICATION_JSON.as_ref())
}
fn body(self) -> Self::Body {
Full::new(self.0)
}
}