1use super::{Patch, Post, Put};
2use crate::{ContentType, Decodes, Encodes, Format, FormatType};
3use bytes::Bytes;
4use serde::{de::DeserializeOwned, Serialize};
5
6pub struct JsonEncoding;
8
9impl ContentType for JsonEncoding {
10 const CONTENT_TYPE: &'static str = "application/json";
11}
12
13impl FormatType for JsonEncoding {
14 const FORMAT_TYPE: Format = Format::Text;
15}
16
17impl<T> Encodes<T> for JsonEncoding
18where
19 T: Serialize,
20{
21 type Error = serde_json::Error;
22
23 fn encode(output: &T) -> Result<Bytes, Self::Error> {
24 serde_json::to_vec(output).map(Bytes::from)
25 }
26}
27
28impl<T> Decodes<T> for JsonEncoding
29where
30 T: DeserializeOwned,
31{
32 type Error = serde_json::Error;
33
34 fn decode(bytes: Bytes) -> Result<T, Self::Error> {
35 serde_json::from_slice(&bytes)
36 }
37}
38
39pub type Json = Post<JsonEncoding>;
41
42pub type PatchJson = Patch<JsonEncoding>;
46
47pub type PutJson = Put<JsonEncoding>;