pub struct Json<D>(pub D);json only.Expand description
A type that represents JSON content.
Note that this is just a newtype wrapper around data and does not provide any content validation. It is primarily useful as a request extractor and response type for REST-ful endpoints.
§Examples
use cot::json::Json;
let Json(data) = Json("content");
assert_eq!(data, "content");Tuple Fields§
§0: DTrait Implementations§
Source§impl<D: JsonSchema> ApiOperationPart for Json<D>
Available on crate feature openapi only.
impl<D: JsonSchema> ApiOperationPart for Json<D>
openapi only.Source§fn modify_api_operation(
operation: &mut Operation,
_route_context: &RouteContext<'_>,
schema_generator: &mut SchemaGenerator,
)
fn modify_api_operation( operation: &mut Operation, _route_context: &RouteContext<'_>, schema_generator: &mut SchemaGenerator, )
Source§impl<S: JsonSchema> ApiOperationResponse for Json<S>
Available on crate feature openapi only.
impl<S: JsonSchema> ApiOperationResponse for Json<S>
openapi only.Source§fn api_operation_responses(
_operation: &mut Operation,
_route_context: &RouteContext<'_>,
schema_generator: &mut SchemaGenerator,
) -> Vec<(Option<StatusCode>, Response)>
fn api_operation_responses( _operation: &mut Operation, _route_context: &RouteContext<'_>, schema_generator: &mut SchemaGenerator, ) -> Vec<(Option<StatusCode>, Response)>
Source§impl<D: DeserializeOwned> FromRequest for Json<D>
Extractor that gets the request body as JSON and deserializes it into a type
T implementing serde::de::DeserializeOwned.
impl<D: DeserializeOwned> FromRequest for Json<D>
Extractor that gets the request body as JSON and deserializes it into a type
T implementing serde::de::DeserializeOwned.
The content type of the request must be application/json.
§Errors
Throws an error if the content type is not application/json.
Throws an error if the request body could not be read.
Throws an error if the request body could not be deserialized - either
because the JSON is invalid or because the deserialization to the target
structure failed.
§Example
use cot::RequestHandler;
use cot::json::Json;
use cot::test::TestRequestBuilder;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct MyData {
hello: String,
}
async fn my_handler(Json(data): Json<MyData>) -> Json<MyData> {
Json(data)
}
let request = TestRequestBuilder::get("/")
.json(&MyData {
hello: "world".to_string(),
})
.build();
assert_eq!(
my_handler
.handle(request)
.await?
.into_body()
.into_bytes()
.await?,
"{\"hello\":\"world\"}"
);Source§async fn from_request(head: &RequestHead, body: Body) -> Result<Self>
async fn from_request(head: &RequestHead, body: Body) -> Result<Self>
Source§impl<D: Serialize> IntoResponse for Json<D>
impl<D: Serialize> IntoResponse for Json<D>
Source§fn into_response(self) -> Result<Response>
fn into_response(self) -> Result<Response>
Create a new JSON response.
This creates a new Response object with a content type of
application/json and given body.
§Examples
use std::collections::HashMap;
use cot::json::Json;
use cot::response::IntoResponse;
let data = HashMap::from([("hello", "world")]);
let json = Json(data);
let response = json.into_response();Source§fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self>
fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self>
Source§fn with_content_type<V>(self, content_type: V) -> WithContentType<Self>
fn with_content_type<V>(self, content_type: V) -> WithContentType<Self>
Content-Type header. Read moreSource§fn with_status(self, status: StatusCode) -> WithStatus<Self>where
Self: Sized,
fn with_status(self, status: StatusCode) -> WithStatus<Self>where
Self: Sized,
Source§fn with_body(self, body: impl Into<Body>) -> WithBody<Self>where
Self: Sized,
fn with_body(self, body: impl Into<Body>) -> WithBody<Self>where
Self: Sized,
Source§fn with_extension<T>(self, extension: T) -> WithExtension<Self, T>
fn with_extension<T>(self, extension: T) -> WithExtension<Self, T>
Source§impl<D: Ord> Ord for Json<D>
impl<D: Ord> Ord for Json<D>
Source§impl<D: PartialOrd> PartialOrd for Json<D>
impl<D: PartialOrd> PartialOrd for Json<D>
impl<D: Copy> Copy for Json<D>
impl<D: Eq> Eq for Json<D>
impl<D> StructuralPartialEq for Json<D>
Auto Trait Implementations§
impl<D> Freeze for Json<D>where
D: Freeze,
impl<D> RefUnwindSafe for Json<D>where
D: RefUnwindSafe,
impl<D> Send for Json<D>where
D: Send,
impl<D> Sync for Json<D>where
D: Sync,
impl<D> Unpin for Json<D>where
D: Unpin,
impl<D> UnwindSafe for Json<D>where
D: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoField<Auto<T>> for T
impl<T> IntoField<Auto<T>> for T
Source§fn into_field(self) -> Auto<T>
fn into_field(self) -> Auto<T>
db only.