1use format_ende::FormatDecoder;
32use format_ende::FormatEncoder;
33use format_ende::FormatInfo;
34
35#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
37pub struct JsonFormat {
38 pub pretty: bool,
39}
40
41impl JsonFormat {
42 pub const fn new() -> Self {
43 Self { pretty: false }
44 }
45}
46
47impl Default for JsonFormat {
48 fn default() -> Self {
49 Self::new()
50 }
51}
52
53impl FormatInfo for JsonFormat {
54 fn file_extension(&self) -> &str {
55 "json"
56 }
57
58 fn is_utf8(&self) -> bool {
59 true
60 }
61}
62
63impl<V> FormatEncoder<V> for JsonFormat
64where
65 V: serde::Serialize + ?Sized,
66{
67 type EncodeError = serde_json::Error;
68
69 fn encode(
70 &mut self,
71 mut writer: impl std::io::Write,
72 value: &V,
73 ) -> Result<(), Self::EncodeError> {
74 if self.pretty {
75 serde_json::to_writer_pretty(&mut writer, value)
76 } else {
77 serde_json::to_writer(&mut writer, value)
78 }
79 }
80}
81
82impl<V> FormatDecoder<V> for JsonFormat
83where
84 V: serde::de::DeserializeOwned,
85{
86 type DecodeError = serde_json::Error;
87
88 fn decode(&mut self, reader: impl std::io::Read) -> Result<V, Self::DecodeError> {
89 serde_json::from_reader(reader)
90 }
91}