1use crate::{BytesLike, Stack, JsonError, Value, JsonDeserialize, JsonSerialize};
2
3impl JsonDeserialize for i8 {
4 fn deserialize<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
5 value: Value<'bytes, 'parent, B, S>,
6 ) -> Result<Self, JsonError<'bytes, B, S>> {
7 value.as_i64()?.try_into().map_err(|_| JsonError::TypeError)
8 }
9}
10impl JsonDeserialize for i16 {
11 fn deserialize<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
12 value: Value<'bytes, 'parent, B, S>,
13 ) -> Result<Self, JsonError<'bytes, B, S>> {
14 value.as_i64()?.try_into().map_err(|_| JsonError::TypeError)
15 }
16}
17impl JsonDeserialize for i32 {
18 fn deserialize<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
19 value: Value<'bytes, 'parent, B, S>,
20 ) -> Result<Self, JsonError<'bytes, B, S>> {
21 value.as_i64()?.try_into().map_err(|_| JsonError::TypeError)
22 }
23}
24impl JsonDeserialize for i64 {
25 fn deserialize<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
26 value: Value<'bytes, 'parent, B, S>,
27 ) -> Result<Self, JsonError<'bytes, B, S>> {
28 value.as_i64()
29 }
30}
31
32impl JsonDeserialize for u8 {
33 fn deserialize<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
34 value: Value<'bytes, 'parent, B, S>,
35 ) -> Result<Self, JsonError<'bytes, B, S>> {
36 value.as_i64()?.try_into().map_err(|_| JsonError::TypeError)
37 }
38}
39impl JsonDeserialize for u16 {
40 fn deserialize<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
41 value: Value<'bytes, 'parent, B, S>,
42 ) -> Result<Self, JsonError<'bytes, B, S>> {
43 value.as_i64()?.try_into().map_err(|_| JsonError::TypeError)
44 }
45}
46impl JsonDeserialize for u32 {
47 fn deserialize<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
48 value: Value<'bytes, 'parent, B, S>,
49 ) -> Result<Self, JsonError<'bytes, B, S>> {
50 value.as_i64()?.try_into().map_err(|_| JsonError::TypeError)
51 }
52}
53impl JsonDeserialize for u64 {
54 fn deserialize<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
55 value: Value<'bytes, 'parent, B, S>,
56 ) -> Result<Self, JsonError<'bytes, B, S>> {
57 value.as_i64()?.try_into().map_err(|_| JsonError::TypeError)
58 }
59}
60
61impl JsonDeserialize for bool {
62 fn deserialize<'bytes, 'parent, B: BytesLike<'bytes>, S: Stack>(
63 value: Value<'bytes, 'parent, B, S>,
64 ) -> Result<Self, JsonError<'bytes, B, S>> {
65 value.as_bool()
66 }
67}
68
69struct IntInterator {
70 value: u64,
71 digits: usize,
72 i: usize,
73}
74impl IntInterator {
75 fn new(value: u64) -> Self {
76 let digits = {
77 let mut digits = 0;
78 let mut value = value;
79 while value > 0 {
80 digits += 1;
81 value /= 10;
82 }
83 digits
84 };
85 IntInterator { value, digits, i: 0 }
86 }
87}
88impl Iterator for IntInterator {
89 type Item = char;
90 fn next(&mut self) -> Option<Self::Item> {
91 if self.i == self.digits {
92 None?;
93 }
94
95 let mut value = self.value;
96 for _ in self.i .. (self.digits - 1) {
98 value /= 10;
99 }
100 self.i += 1;
101
102 let char_offset = (value % 10) as u8;
104 Some((b'0' + char_offset) as char)
106 }
107}
108fn u64_to_str(value: impl Into<u64>) -> impl Iterator<Item = char> {
109 let value = value.into();
110 let zero = value == 0;
111 zero.then(|| core::iter::once('0')).into_iter().flatten().chain(IntInterator::new(value))
112}
113pub(crate) fn i64_to_str(value: impl Into<i64>) -> impl Iterator<Item = char> {
114 let value: i64 = value.into();
115 value
116 .is_negative()
117 .then(|| core::iter::once('-'))
118 .into_iter()
119 .flatten()
120 .chain(u64_to_str(value.unsigned_abs()))
121}
122impl JsonSerialize for i8 {
123 fn serialize(&self) -> impl Iterator<Item = char> {
124 i64_to_str(*self)
125 }
126}
127impl JsonSerialize for i16 {
128 fn serialize(&self) -> impl Iterator<Item = char> {
129 i64_to_str(*self)
130 }
131}
132impl JsonSerialize for i32 {
133 fn serialize(&self) -> impl Iterator<Item = char> {
134 i64_to_str(*self)
135 }
136}
137impl JsonSerialize for i64 {
138 fn serialize(&self) -> impl Iterator<Item = char> {
139 i64_to_str(*self)
140 }
141}
142impl JsonSerialize for u8 {
143 fn serialize(&self) -> impl Iterator<Item = char> {
144 u64_to_str(*self)
145 }
146}
147impl JsonSerialize for u16 {
148 fn serialize(&self) -> impl Iterator<Item = char> {
149 u64_to_str(*self)
150 }
151}
152impl JsonSerialize for u32 {
153 fn serialize(&self) -> impl Iterator<Item = char> {
154 u64_to_str(*self)
155 }
156}
157impl JsonSerialize for u64 {
158 fn serialize(&self) -> impl Iterator<Item = char> {
159 u64_to_str(*self)
160 }
161}
162
163impl JsonSerialize for bool {
164 fn serialize(&self) -> impl Iterator<Item = char> {
165 (if *self { "true" } else { "false" }).chars()
166 }
167}