simd_json_derive/impls/
simdjson.rs1use crate::{de, Deserialize, Serialize};
2use simd_json::{BorrowedValue, Node, OwnedValue};
3use value_trait::{base::Writable, ValueBuilder};
4
5impl Serialize for OwnedValue {
6 fn json_write<W>(&self, writer: &mut W) -> crate::Result
7 where
8 W: std::io::Write,
9 {
10 self.write(writer)
11 }
12}
13impl Serialize for BorrowedValue<'_> {
14 fn json_write<W>(&self, writer: &mut W) -> crate::Result
15 where
16 W: std::io::Write,
17 {
18 self.write(writer)
19 }
20}
21
22struct OwnedDeser<'input, 'tape>(&'tape mut crate::Tape<'input>);
23
24impl OwnedDeser<'_, '_> {
25 fn parse(&mut self) -> OwnedValue {
26 match self.0.next() {
27 Some(Node::Static(s)) => OwnedValue::Static(s),
28 Some(Node::String(s)) => OwnedValue::from(s),
29 Some(Node::Array { len, .. }) => self.parse_array(len),
30 Some(Node::Object { len, .. }) => self.parse_map(len),
31 None => unreachable!("We have validated the tape in the second stage of parsing, this should never happen"),
32 }
33 }
34 fn parse_array(&mut self, len: usize) -> OwnedValue {
35 let mut res: Vec<OwnedValue> = Vec::with_capacity(len);
36 for _ in 0..len {
40 res.push(self.parse());
41 }
42 OwnedValue::Array(Box::new(res))
43 }
44
45 fn parse_map(&mut self, len: usize) -> OwnedValue {
46 let mut res = OwnedValue::object_with_capacity(len);
47
48 if let OwnedValue::Object(ref mut res) = res {
51 for _ in 0..len {
52 if let Some(Node::String(key)) = self.0.next() {
53 unsafe { res.insert_nocheck(key.into(), self.parse()) };
54 } else {
55 unreachable!("We have validated the tape in the second stage of parsing, this should never happen")
56 }
57 }
58 } else {
59 unreachable!("We have generated this object and know it is nothing else")
60 }
61 res
62 }
63}
64impl<'input> Deserialize<'input> for OwnedValue {
65 fn from_tape(tape: &mut crate::Tape<'input>) -> de::Result<Self>
66 where
67 Self: Sized + 'input,
68 {
69 Ok(OwnedDeser(tape).parse())
70 }
71}
72
73struct BorrowedDeser<'input, 'tape>(&'tape mut crate::Tape<'input>);
74
75impl<'input> BorrowedDeser<'input, '_> {
76 fn parse(&mut self) -> BorrowedValue<'input> {
77 match self.0.next() {
78 Some(Node::Static(s)) => BorrowedValue::Static(s),
79 Some(Node::String(s)) => BorrowedValue::from(s),
80 Some(Node::Array { len, .. }) => self.parse_array(len),
81 Some(Node::Object { len, .. }) => self.parse_map(len),
82 None => unreachable!("We have validated the tape in the second stage of parsing, this should never happen"),
83 }
84 }
85 fn parse_array(&mut self, len: usize) -> BorrowedValue<'input> {
86 let mut res: Vec<BorrowedValue<'input>> = Vec::with_capacity(len);
87 for _ in 0..len {
88 res.push(self.parse());
89 }
90 BorrowedValue::Array(Box::new(res))
91 }
92
93 fn parse_map(&mut self, len: usize) -> BorrowedValue<'input> {
94 let mut res = BorrowedValue::object_with_capacity(len);
95
96 if let BorrowedValue::Object(ref mut res) = res {
99 for _ in 0..len {
100 if let Some(Node::String(key)) = self.0.next() {
101 unsafe { res.insert_nocheck(key.into(), self.parse()) };
102 } else {
103 unreachable!("We have validated the tape in the second stage of parsing, this should never happen")
104 }
105 }
106 } else {
107 unreachable!("We have generated this object and know it is nothing else")
108 }
109
110 res
111 }
112}
113impl<'input> Deserialize<'input> for BorrowedValue<'input> {
114 fn from_tape(tape: &mut crate::Tape<'input>) -> de::Result<Self>
115 where
116 Self: Sized + 'input,
117 {
118 Ok(BorrowedDeser(tape).parse())
119 }
120}