1use aws_smithy_schema::codec::Codec;
9use aws_smithy_types::date_time::Format as TimestampFormat;
10
11mod deserializer;
12mod serializer;
13
14pub use deserializer::JsonDeserializer;
15pub use serializer::JsonSerializer;
16
17#[derive(Debug, Clone)]
19pub struct JsonCodecSettings {
20 pub use_json_name: bool,
22 pub default_timestamp_format: TimestampFormat,
24 pub allow_unknown_union_members: bool,
26}
27
28impl Default for JsonCodecSettings {
29 fn default() -> Self {
30 Self {
31 use_json_name: true,
32 default_timestamp_format: TimestampFormat::EpochSeconds,
33 allow_unknown_union_members: false,
34 }
35 }
36}
37
38#[derive(Debug, Clone)]
65pub struct JsonCodec {
66 settings: JsonCodecSettings,
67}
68
69impl JsonCodec {
70 pub fn new(settings: JsonCodecSettings) -> Self {
72 Self { settings }
73 }
74
75 pub fn settings(&self) -> &JsonCodecSettings {
77 &self.settings
78 }
79}
80
81impl Default for JsonCodec {
82 fn default() -> Self {
83 Self::new(JsonCodecSettings::default())
84 }
85}
86
87impl Codec for JsonCodec {
88 type Serializer = JsonSerializer;
89 type Deserializer = JsonDeserializer;
90
91 fn create_serializer(&self) -> Self::Serializer {
92 JsonSerializer::new(self.settings.clone())
93 }
94
95 fn create_deserializer(&self, input: &[u8]) -> Self::Deserializer {
96 JsonDeserializer::new(input, self.settings.clone())
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103
104 #[test]
105 fn test_default_settings() {
106 let settings = JsonCodecSettings::default();
107 assert!(settings.use_json_name);
108 assert_eq!(
109 settings.default_timestamp_format,
110 TimestampFormat::EpochSeconds
111 );
112 assert!(!settings.allow_unknown_union_members);
113 }
114
115 #[test]
116 fn test_codec_creation() {
117 let codec = JsonCodec::default();
118 let _serializer = codec.create_serializer();
119 let _deserializer = codec.create_deserializer(b"{}");
120 }
121}