arrow_json/reader/
serializer.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::reader::tape::TapeElement;
19use lexical_core::FormattedSize;
20use serde::ser::{
21    Impossible, SerializeMap, SerializeSeq, SerializeStruct, SerializeTuple, SerializeTupleStruct,
22};
23use serde::{Serialize, Serializer};
24
25#[derive(Debug)]
26pub struct SerializerError(String);
27
28impl std::error::Error for SerializerError {}
29
30impl std::fmt::Display for SerializerError {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        write!(f, "{}", self.0)
33    }
34}
35
36impl serde::ser::Error for SerializerError {
37    fn custom<T>(msg: T) -> Self
38    where
39        T: std::fmt::Display,
40    {
41        Self(msg.to_string())
42    }
43}
44
45/// [`Serializer`] for [`TapeElement`]
46///
47/// Heavily based on <https://serde.rs/impl-serializer.html>
48pub struct TapeSerializer<'a> {
49    elements: &'a mut Vec<TapeElement>,
50
51    /// A buffer of parsed string data
52    bytes: &'a mut Vec<u8>,
53
54    /// Offsets into `data`
55    offsets: &'a mut Vec<usize>,
56}
57
58impl<'a> TapeSerializer<'a> {
59    pub fn new(
60        elements: &'a mut Vec<TapeElement>,
61        bytes: &'a mut Vec<u8>,
62        offsets: &'a mut Vec<usize>,
63    ) -> Self {
64        Self {
65            elements,
66            bytes,
67            offsets,
68        }
69    }
70
71    fn serialize_number(&mut self, v: &[u8]) {
72        self.bytes.extend_from_slice(v);
73        let idx = self.offsets.len() - 1;
74        self.elements.push(TapeElement::Number(idx as _));
75        self.offsets.push(self.bytes.len());
76    }
77}
78
79impl<'a, 'b> Serializer for &'a mut TapeSerializer<'b> {
80    type Ok = ();
81
82    type Error = SerializerError;
83
84    type SerializeSeq = ListSerializer<'a, 'b>;
85    type SerializeTuple = ListSerializer<'a, 'b>;
86    type SerializeTupleStruct = ListSerializer<'a, 'b>;
87    type SerializeTupleVariant = Impossible<(), SerializerError>;
88    type SerializeMap = ObjectSerializer<'a, 'b>;
89    type SerializeStruct = ObjectSerializer<'a, 'b>;
90    type SerializeStructVariant = Impossible<(), SerializerError>;
91
92    fn serialize_bool(self, v: bool) -> Result<(), SerializerError> {
93        self.elements.push(match v {
94            true => TapeElement::True,
95            false => TapeElement::False,
96        });
97        Ok(())
98    }
99
100    fn serialize_i8(self, v: i8) -> Result<(), SerializerError> {
101        self.serialize_i32(v as _)
102    }
103
104    fn serialize_i16(self, v: i16) -> Result<(), SerializerError> {
105        self.serialize_i32(v as _)
106    }
107
108    fn serialize_i32(self, v: i32) -> Result<(), SerializerError> {
109        self.elements.push(TapeElement::I32(v));
110        Ok(())
111    }
112
113    fn serialize_i64(self, v: i64) -> Result<(), SerializerError> {
114        let low = v as i32;
115        let high = (v >> 32) as i32;
116        self.elements.push(TapeElement::I64(high));
117        self.elements.push(TapeElement::I32(low));
118        Ok(())
119    }
120
121    fn serialize_u8(self, v: u8) -> Result<(), SerializerError> {
122        self.serialize_i32(v as _)
123    }
124
125    fn serialize_u16(self, v: u16) -> Result<(), SerializerError> {
126        self.serialize_i32(v as _)
127    }
128
129    fn serialize_u32(self, v: u32) -> Result<(), SerializerError> {
130        match i32::try_from(v) {
131            Ok(v) => self.serialize_i32(v),
132            Err(_) => self.serialize_i64(v as _),
133        }
134    }
135
136    fn serialize_u64(self, v: u64) -> Result<(), SerializerError> {
137        match i64::try_from(v) {
138            Ok(v) => self.serialize_i64(v),
139            Err(_) => {
140                let mut buffer = [0_u8; u64::FORMATTED_SIZE];
141                let s = lexical_core::write(v, &mut buffer);
142                self.serialize_number(s);
143                Ok(())
144            }
145        }
146    }
147
148    fn serialize_f32(self, v: f32) -> Result<(), SerializerError> {
149        self.elements.push(TapeElement::F32(v.to_bits()));
150        Ok(())
151    }
152
153    fn serialize_f64(self, v: f64) -> Result<(), SerializerError> {
154        let bits = v.to_bits();
155        self.elements.push(TapeElement::F64((bits >> 32) as u32));
156        self.elements.push(TapeElement::F32(bits as u32));
157        Ok(())
158    }
159
160    fn serialize_char(self, v: char) -> Result<(), SerializerError> {
161        self.serialize_str(&v.to_string())
162    }
163
164    fn serialize_str(self, v: &str) -> Result<(), SerializerError> {
165        self.serialize_bytes(v.as_bytes())
166    }
167
168    fn serialize_bytes(self, v: &[u8]) -> Result<(), SerializerError> {
169        self.bytes.extend_from_slice(v);
170        let idx = self.offsets.len() - 1;
171        self.elements.push(TapeElement::String(idx as _));
172        self.offsets.push(self.bytes.len());
173        Ok(())
174    }
175
176    fn serialize_none(self) -> Result<(), SerializerError> {
177        self.serialize_unit()
178    }
179
180    fn serialize_some<T>(self, value: &T) -> Result<(), SerializerError>
181    where
182        T: ?Sized + Serialize,
183    {
184        value.serialize(self)
185    }
186
187    fn serialize_unit(self) -> Result<(), SerializerError> {
188        self.elements.push(TapeElement::Null);
189        Ok(())
190    }
191
192    fn serialize_unit_struct(self, _name: &'static str) -> Result<(), SerializerError> {
193        self.serialize_unit()
194    }
195
196    fn serialize_unit_variant(
197        self,
198        _name: &'static str,
199        _variant_index: u32,
200        variant: &'static str,
201    ) -> Result<(), SerializerError> {
202        self.serialize_str(variant)
203    }
204
205    fn serialize_newtype_struct<T>(
206        self,
207        _name: &'static str,
208        value: &T,
209    ) -> Result<(), SerializerError>
210    where
211        T: ?Sized + Serialize,
212    {
213        value.serialize(self)
214    }
215
216    fn serialize_newtype_variant<T>(
217        self,
218        _name: &'static str,
219        _variant_index: u32,
220        variant: &'static str,
221        value: &T,
222    ) -> Result<(), SerializerError>
223    where
224        T: ?Sized + Serialize,
225    {
226        let mut serializer = self.serialize_map(Some(1))?;
227        serializer.serialize_key(variant)?;
228        serializer.serialize_value(value)?;
229        serializer.finish();
230        Ok(())
231    }
232
233    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, SerializerError> {
234        Ok(ListSerializer::new(self))
235    }
236
237    fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, SerializerError> {
238        self.serialize_seq(Some(len))
239    }
240
241    fn serialize_tuple_struct(
242        self,
243        _name: &'static str,
244        len: usize,
245    ) -> Result<Self::SerializeTupleStruct, SerializerError> {
246        self.serialize_seq(Some(len))
247    }
248
249    fn serialize_tuple_variant(
250        self,
251        name: &'static str,
252        _variant_index: u32,
253        variant: &'static str,
254        _len: usize,
255    ) -> Result<Self::SerializeTupleVariant, SerializerError> {
256        Err(SerializerError(format!(
257            "serializing tuple variants is not currently supported: {name}::{variant}"
258        )))
259    }
260
261    // Maps are represented in JSON as `{ K: V, K: V, ... }`.
262    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, SerializerError> {
263        Ok(ObjectSerializer::new(self))
264    }
265
266    fn serialize_struct(
267        self,
268        _name: &'static str,
269        len: usize,
270    ) -> Result<Self::SerializeStruct, SerializerError> {
271        self.serialize_map(Some(len))
272    }
273
274    fn serialize_struct_variant(
275        self,
276        name: &'static str,
277        _variant_index: u32,
278        variant: &'static str,
279        _len: usize,
280    ) -> Result<Self::SerializeStructVariant, SerializerError> {
281        Err(SerializerError(format!(
282            "serializing struct variants is not currently supported: {name}::{variant}"
283        )))
284    }
285}
286
287pub struct ObjectSerializer<'a, 'b> {
288    serializer: &'a mut TapeSerializer<'b>,
289    start: usize,
290}
291
292impl<'a, 'b> ObjectSerializer<'a, 'b> {
293    fn new(serializer: &'a mut TapeSerializer<'b>) -> Self {
294        let start = serializer.elements.len();
295        serializer.elements.push(TapeElement::StartObject(0));
296        Self { serializer, start }
297    }
298
299    fn finish(self) {
300        let end = self.serializer.elements.len() as _;
301        self.serializer.elements[self.start] = TapeElement::StartObject(end);
302
303        let end = TapeElement::EndObject(self.start as _);
304        self.serializer.elements.push(end);
305    }
306}
307
308impl SerializeMap for ObjectSerializer<'_, '_> {
309    type Ok = ();
310    type Error = SerializerError;
311
312    fn serialize_key<T>(&mut self, key: &T) -> Result<(), Self::Error>
313    where
314        T: Serialize + ?Sized,
315    {
316        key.serialize(&mut *self.serializer)
317    }
318
319    fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error>
320    where
321        T: Serialize + ?Sized,
322    {
323        value.serialize(&mut *self.serializer)
324    }
325
326    fn end(self) -> Result<(), Self::Error> {
327        self.finish();
328        Ok(())
329    }
330}
331
332impl SerializeStruct for ObjectSerializer<'_, '_> {
333    type Ok = ();
334    type Error = SerializerError;
335
336    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
337    where
338        T: Serialize + ?Sized,
339    {
340        key.serialize(&mut *self.serializer)?;
341        value.serialize(&mut *self.serializer)
342    }
343
344    fn end(self) -> Result<(), Self::Error> {
345        self.finish();
346        Ok(())
347    }
348}
349
350pub struct ListSerializer<'a, 'b> {
351    serializer: &'a mut TapeSerializer<'b>,
352    start: usize,
353}
354
355impl<'a, 'b> ListSerializer<'a, 'b> {
356    fn new(serializer: &'a mut TapeSerializer<'b>) -> Self {
357        let start = serializer.elements.len();
358        serializer.elements.push(TapeElement::StartList(0));
359        Self { serializer, start }
360    }
361
362    fn finish(self) {
363        let end = self.serializer.elements.len() as _;
364        self.serializer.elements[self.start] = TapeElement::StartList(end);
365
366        let end = TapeElement::EndList(self.start as _);
367        self.serializer.elements.push(end);
368    }
369}
370
371impl SerializeSeq for ListSerializer<'_, '_> {
372    type Ok = ();
373    type Error = SerializerError;
374
375    fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
376    where
377        T: Serialize + ?Sized,
378    {
379        value.serialize(&mut *self.serializer)
380    }
381
382    fn end(self) -> Result<(), Self::Error> {
383        self.finish();
384        Ok(())
385    }
386}
387
388impl SerializeTuple for ListSerializer<'_, '_> {
389    type Ok = ();
390    type Error = SerializerError;
391
392    fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
393    where
394        T: Serialize + ?Sized,
395    {
396        value.serialize(&mut *self.serializer)
397    }
398
399    fn end(self) -> Result<(), Self::Error> {
400        self.finish();
401        Ok(())
402    }
403}
404
405impl SerializeTupleStruct for ListSerializer<'_, '_> {
406    type Ok = ();
407    type Error = SerializerError;
408
409    fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
410    where
411        T: Serialize + ?Sized,
412    {
413        value.serialize(&mut *self.serializer)
414    }
415
416    fn end(self) -> Result<(), Self::Error> {
417        self.finish();
418        Ok(())
419    }
420}