1use o3::buffer::{Owned, Shared};
2
3use crate::Result;
4use crate::encode::Writer;
5
6pub trait JsonDecode: Sized {
7 fn decode_json(input: Shared) -> Result<Self>;
8
9 fn decode_json_borrowed(input: &[u8]) -> Result<Self> {
10 Self::decode_json(Shared::copy_from_slice(input))
11 }
12}
13
14pub trait JsonScan: Sized {
15 fn scan_json<'a, I>(chunks: I) -> Result<Self>
16 where
17 I: IntoIterator<Item = &'a [u8]>;
18}
19
20pub trait JsonEncode: Sized {
21 fn json_len(&self) -> usize;
22
23 fn write_into(&self, w: &mut Writer<'_>);
24
25 fn write_json(&self, out: &mut Owned) {
26 let mut w = Writer::new(out, self.json_len());
27 self.write_into(&mut w);
28 w.finish();
29 }
30
31 fn encode_json(&self) -> Owned {
32 let mut out = Owned::new();
33 self.write_json(&mut out);
34 out
35 }
36}
37
38pub trait JsonPreserve {
39 fn raw_json(&self) -> Option<&Shared>;
40}
41
42impl<T> JsonEncode for &T
43where
44 T: JsonEncode,
45{
46 fn json_len(&self) -> usize {
47 (*self).json_len()
48 }
49
50 fn write_into(&self, w: &mut Writer<'_>) {
51 (*self).write_into(w)
52 }
53
54 fn write_json(&self, out: &mut Owned) {
55 (*self).write_json(out)
56 }
57}
58
59impl<T> JsonPreserve for &T
60where
61 T: JsonPreserve + ?Sized,
62{
63 fn raw_json(&self) -> Option<&Shared> {
64 (*self).raw_json()
65 }
66}