use super::super::bound::{self, Incoming, Outgoing};
use super::{FromContent, IntoContent};
use std::borrow::Cow;
#[cfg(feature = "openapi")]
use crate::openapi;
pub struct Json<T: bound::Schema>(pub T);
impl<'req, T: Incoming<'req>> FromContent<'req> for Json<T> {
const MIME_TYPE: &'static str = "application/json";
#[inline]
fn from_content(body: &'req [u8]) -> Result<Self, impl std::fmt::Display> {
serde_json::from_slice(body).map(Json)
}
#[cfg(feature = "openapi")]
fn openapi_requestbody() -> impl Into<openapi::schema::SchemaRef> {
T::schema()
}
}
impl<T: Outgoing> IntoContent for Json<T> {
const CONTENT_TYPE: &'static str = "application/json";
#[inline]
fn into_content(self) -> Result<Cow<'static, [u8]>, impl std::fmt::Display> {
serde_json::to_vec(&self.0).map(Cow::Owned)
}
#[cfg(feature = "openapi")]
fn openapi_responsebody() -> impl Into<openapi::schema::SchemaRef> {
T::schema()
}
}