poem_grpc/codec/json_codec/
mod.rs1use std::{
2 io::{Error, Result},
3 marker::PhantomData,
4};
5
6use bytes::{BufMut, BytesMut};
7use serde::{Serialize, de::DeserializeOwned};
8
9use crate::codec::{Codec, Decoder, Encoder};
10
11mod i64string_deserializer;
12mod i64string_serializer;
13
14#[cfg_attr(docsrs, doc(cfg(feature = "json-codec")))]
16#[derive(Debug)]
17pub struct JsonCodec<T, U>(PhantomData<(T, U)>);
18
19impl<T, U> Default for JsonCodec<T, U> {
20 fn default() -> Self {
21 Self(PhantomData)
22 }
23}
24
25impl<T, U> Codec for JsonCodec<T, U>
26where
27 T: Serialize + Send + 'static,
28 U: DeserializeOwned + Send + 'static,
29{
30 const CONTENT_TYPES: &'static [&'static str] = &["application/json", "application/grpc+json"];
31
32 type Encode = T;
33 type Decode = U;
34 type Encoder = JsonEncoder<T>;
35 type Decoder = JsonDecoder<U>;
36
37 fn encoder(&mut self) -> Self::Encoder {
38 JsonEncoder(PhantomData)
39 }
40
41 fn decoder(&mut self) -> Self::Decoder {
42 JsonDecoder(PhantomData)
43 }
44}
45
46#[doc(hidden)]
47pub struct JsonEncoder<T>(PhantomData<T>);
48
49impl<T> Encoder for JsonEncoder<T>
50where
51 T: Serialize + Send + 'static,
52{
53 type Item = T;
54
55 fn encode(&mut self, item: Self::Item, buf: &mut BytesMut) -> Result<()> {
56 let mut ser = serde_json::Serializer::new(buf.writer());
57 item.serialize(&mut ser).map_err(Error::other)
58 }
59}
60
61#[doc(hidden)]
62pub struct JsonDecoder<U>(PhantomData<U>);
63
64impl<U: DeserializeOwned + Send + 'static> Decoder for JsonDecoder<U> {
65 type Item = U;
66
67 fn decode(&mut self, buf: &[u8]) -> Result<Self::Item> {
68 let mut de = serde_json::Deserializer::from_slice(buf);
69 U::deserialize(&mut de).map_err(Error::other)
70 }
71}
72
73#[cfg_attr(docsrs, doc(cfg(feature = "json-codec")))]
76#[derive(Debug)]
77pub struct JsonI64ToStringCodec<T, U>(PhantomData<(T, U)>);
78
79impl<T, U> Default for JsonI64ToStringCodec<T, U> {
80 fn default() -> Self {
81 Self(PhantomData)
82 }
83}
84
85impl<T, U> Codec for JsonI64ToStringCodec<T, U>
86where
87 T: Serialize + Send + 'static,
88 U: DeserializeOwned + Send + 'static,
89{
90 const CONTENT_TYPES: &'static [&'static str] = &["application/grpc+json", "application/json"];
91
92 type Encode = T;
93 type Decode = U;
94 type Encoder = JsonI64ToStringEncoder<T>;
95 type Decoder = JsonI64ToStringDecoder<U>;
96
97 fn encoder(&mut self) -> Self::Encoder {
98 JsonI64ToStringEncoder(PhantomData)
99 }
100
101 fn decoder(&mut self) -> Self::Decoder {
102 JsonI64ToStringDecoder(PhantomData)
103 }
104}
105
106#[doc(hidden)]
107pub struct JsonI64ToStringEncoder<T>(PhantomData<T>);
108
109impl<T> Encoder for JsonI64ToStringEncoder<T>
110where
111 T: Serialize + Send + 'static,
112{
113 type Item = T;
114
115 fn encode(&mut self, item: Self::Item, buf: &mut BytesMut) -> Result<()> {
116 let mut ser = serde_json::Serializer::new(buf.writer());
117 item.serialize(i64string_serializer::I64ToStringSerializer(&mut ser))
118 .map_err(Error::other)
119 }
120}
121
122#[doc(hidden)]
123pub struct JsonI64ToStringDecoder<U>(PhantomData<U>);
124
125impl<U: DeserializeOwned + Send + 'static> Decoder for JsonI64ToStringDecoder<U> {
126 type Item = U;
127
128 fn decode(&mut self, buf: &[u8]) -> Result<Self::Item> {
129 let mut de = serde_json::Deserializer::from_slice(buf);
130 U::deserialize(i64string_deserializer::I64ToStringDeserializer(&mut de))
131 .map_err(Error::other)
132 }
133}