quickjs_rusty/serde/
ser.rs

1use libquickjs_ng_sys::{JSContext, JSValue};
2use serde::{ser, Serialize};
3
4use crate::utils::{
5    create_bigint, create_bool, create_empty_array, create_empty_object, create_float, create_int,
6    create_null, create_string, create_undefined, own_raw_value,
7};
8use crate::value::{OwnedJsArray, OwnedJsObject, OwnedJsValue};
9
10use super::error::{Error, Result};
11
12const MAX_SAFE_INTEGER: u64 = 9007199254740991;
13
14/// A structure that serializes Rust values into JS values.
15pub struct Serializer {
16    context: *mut JSContext,
17    root: OwnedJsValue,
18    paths: Vec<OwnedJsValue>,
19    current: Option<OwnedJsValue>,
20    current_is_key: bool,
21    current_key: Option<String>,
22}
23
24/// convert from rust type to OwnedJsValue
25pub fn to_js<T>(context: *mut JSContext, value: &T) -> Result<OwnedJsValue>
26where
27    T: Serialize,
28{
29    let root = own_raw_value(context, create_undefined());
30    let mut serializer = Serializer {
31        context,
32        root,
33        paths: Vec::new(),
34        current: None,
35        current_is_key: false,
36        current_key: None,
37    };
38    value.serialize(&mut serializer)?;
39
40    Ok(serializer.root)
41}
42
43impl Serializer {
44    #[inline]
45    pub(self) fn set_node_value(&mut self, value: JSValue) -> Result<()> {
46        // if current is None, then set root value
47        if let Some(current) = self.current.as_mut() {
48            if current.is_undefined() {
49                current.replace(value);
50            } else if current.is_array() {
51                let current = OwnedJsArray::try_from_value(current.clone())?;
52                current.push(OwnedJsValue::new(self.context, value))?;
53            } else if current.is_object() {
54                if let Some(key) = self.current_key.take() {
55                    let current = OwnedJsObject::try_from_value(current.clone())?;
56                    current.set_property(&key, OwnedJsValue::new(self.context, value))?;
57                } else {
58                    #[cfg(debug_assertions)]
59                    {
60                        let v = OwnedJsValue::new(self.context, value);
61                        println!("tag: {:?}", v.tag());
62                        println!("tag: {}", v.to_json_string(0)?);
63                    }
64                    return Err(Error::Message("current key is None".to_string()));
65                }
66            } else {
67                #[cfg(debug_assertions)]
68                {
69                    println!("current tag: {:?}", current.tag());
70                    println!("current value: {}", current.to_json_string(0)?);
71                }
72                return Err(Error::Message(
73                    "current is not undefined, object or array.".to_string(),
74                ));
75            }
76        } else {
77            self.root.replace(value);
78            // this only increase the ref count
79            let current = self.root.clone();
80            self.current = Some(current);
81        }
82
83        Ok(())
84    }
85
86    #[inline]
87    pub(self) fn push_object(&mut self) -> Result<()> {
88        let value = create_empty_object(self.context).unwrap();
89        self.set_node_value(value)?;
90
91        if let Some(current) = self.current.take() {
92            self.paths.push(current);
93        }
94
95        self.current = Some(OwnedJsValue::own(self.context, &value));
96
97        Ok(())
98    }
99
100    #[inline]
101    pub(self) fn push_array(&mut self) -> Result<()> {
102        let value = create_empty_array(self.context).unwrap();
103        self.set_node_value(value)?;
104
105        if let Some(current) = self.current.take() {
106            self.paths.push(current);
107        }
108
109        self.current = Some(OwnedJsValue::own(self.context, &value));
110
111        Ok(())
112    }
113
114    #[inline]
115    pub(self) fn pop(&mut self) -> Result<()> {
116        self.current = self.paths.pop();
117        Ok(())
118    }
119}
120
121impl<'a> ser::Serializer for &'a mut Serializer {
122    // The output type produced by this `Serializer` during successful
123    // serialization. Most serializers that produce text or binary output should
124    // set `Ok = ()` and serialize into an `io::Write` or buffer contained
125    // within the `Serializer` instance, as happens here. Serializers that build
126    // in-memory data structures may be simplified by using `Ok` to propagate
127    // the data structure around.
128    type Ok = ();
129
130    // The error type when some error occurs during serialization.
131    type Error = Error;
132
133    // Associated types for keeping track of additional state while serializing
134    // compound data structures like sequences and maps. In this case no
135    // additional state is required beyond what is already stored in the
136    // Serializer struct.
137    type SerializeSeq = Self;
138    type SerializeTuple = Self;
139    type SerializeTupleStruct = Self;
140    type SerializeTupleVariant = Self;
141    type SerializeMap = Self;
142    type SerializeStruct = Self;
143    type SerializeStructVariant = Self;
144
145    // Here we go with the simple methods. The following 12 methods receive one
146    // of the primitive types of the data model and map it to JSON by appending
147    // into the output string.
148    fn serialize_bool(self, v: bool) -> Result<()> {
149        let value = create_bool(self.context, v);
150        self.set_node_value(value)
151    }
152
153    // JSON does not distinguish between different sizes of integers, so all
154    // signed integers will be serialized the same and all unsigned integers
155    // will be serialized the same. Other formats, especially compact binary
156    // formats, may need independent logic for the different sizes.
157    fn serialize_i8(self, v: i8) -> Result<()> {
158        self.serialize_i32(i32::from(v))
159    }
160
161    fn serialize_i16(self, v: i16) -> Result<()> {
162        self.serialize_i32(i32::from(v))
163    }
164
165    fn serialize_i32(self, v: i32) -> Result<()> {
166        let value = create_int(self.context, v as i32);
167        self.set_node_value(value)
168    }
169
170    // Not particularly efficient but this is example code anyway. A more
171    // performant approach would be to use the `itoa` crate.
172    fn serialize_i64(self, v: i64) -> Result<()> {
173        if v > MAX_SAFE_INTEGER as i64 {
174            let value = create_bigint(self.context, (v as i64).into())?;
175            self.set_node_value(value)
176        } else if v > i32::MAX as i64 {
177            let value = create_float(self.context, v as f64);
178            self.set_node_value(value)
179        } else {
180            let value = create_int(self.context, v as i32);
181            self.set_node_value(value)
182        }
183    }
184
185    fn serialize_u8(self, v: u8) -> Result<()> {
186        self.serialize_i32(i32::from(v))
187    }
188
189    fn serialize_u16(self, v: u16) -> Result<()> {
190        self.serialize_i32(i32::from(v))
191    }
192
193    fn serialize_u32(self, v: u32) -> Result<()> {
194        if v > i32::MAX as u32 {
195            let value = create_float(self.context, v as f64);
196            self.set_node_value(value)
197        } else {
198            let value = create_int(self.context, v as i32);
199            self.set_node_value(value)
200        }
201    }
202
203    fn serialize_u64(self, v: u64) -> Result<()> {
204        if v > i64::MAX as u64 {
205            let value = create_bigint(self.context, num_bigint::BigInt::from(v).into())?;
206            self.set_node_value(value)
207        } else if v > MAX_SAFE_INTEGER {
208            let value = create_bigint(self.context, (v as i64).into())?;
209            self.set_node_value(value)
210        } else if v > i32::MAX as u64 {
211            let value = create_float(self.context, v as f64);
212            self.set_node_value(value)
213        } else {
214            let value = create_int(self.context, v as i32);
215            self.set_node_value(value)
216        }
217    }
218
219    fn serialize_f32(self, v: f32) -> Result<()> {
220        self.serialize_f64(f64::from(v))
221    }
222
223    fn serialize_f64(self, v: f64) -> Result<()> {
224        let value = create_float(self.context, v);
225        self.set_node_value(value)
226    }
227
228    // Serialize a char as a single-character string. Other formats may
229    // represent this differently.
230    fn serialize_char(self, v: char) -> Result<()> {
231        self.serialize_str(&v.to_string())
232    }
233
234    // This only works for strings that don't require escape sequences but you
235    // get the idea. For example it would emit invalid JSON if the input string
236    // contains a '"' character.
237    fn serialize_str(self, v: &str) -> Result<()> {
238        if self.current_is_key {
239            self.current_key = Some(v.to_string());
240            self.current_is_key = false;
241            Ok(())
242        } else {
243            let value = create_string(self.context, v).unwrap();
244            self.set_node_value(value)
245        }
246    }
247
248    // Serialize a byte array as an array of bytes. Could also use a base64
249    // string here. Binary formats will typically represent byte arrays more
250    // compactly.
251    fn serialize_bytes(self, v: &[u8]) -> Result<()> {
252        use serde::ser::SerializeSeq;
253        let mut seq = self.serialize_seq(Some(v.len()))?;
254        for byte in v {
255            seq.serialize_element(byte)?;
256        }
257        seq.end()
258    }
259
260    // An absent optional is represented as the JSON `null`.
261    fn serialize_none(self) -> Result<()> {
262        self.serialize_unit()
263    }
264
265    // A present optional is represented as just the contained value. Note that
266    // this is a lossy representation. For example the values `Some(())` and
267    // `None` both serialize as just `null`. Unfortunately this is typically
268    // what people expect when working with JSON. Other formats are encouraged
269    // to behave more intelligently if possible.
270    fn serialize_some<T>(self, value: &T) -> Result<()>
271    where
272        T: ?Sized + Serialize,
273    {
274        value.serialize(self)
275    }
276
277    // In Serde, unit means an anonymous value containing no data. Map this to
278    // JSON as `null`.
279    fn serialize_unit(self) -> Result<()> {
280        let value = create_null();
281        self.set_node_value(value)
282    }
283
284    // Unit struct means a named value containing no data. Again, since there is
285    // no data, map this to JSON as `null`. There is no need to serialize the
286    // name in most formats.
287    fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
288        self.serialize_unit()
289    }
290
291    // When serializing a unit variant (or any other kind of variant), formats
292    // can choose whether to keep track of it by index or by name. Binary
293    // formats typically use the index of the variant and human-readable formats
294    // typically use the name.
295    fn serialize_unit_variant(
296        self,
297        _name: &'static str,
298        _variant_index: u32,
299        variant: &'static str,
300    ) -> Result<()> {
301        self.serialize_str(variant)
302    }
303
304    // As is done here, serializers are encouraged to treat newtype structs as
305    // insignificant wrappers around the data they contain.
306    fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<()>
307    where
308        T: ?Sized + Serialize,
309    {
310        value.serialize(self)
311    }
312
313    // Note that newtype variant (and all of the other variant serialization
314    // methods) refer exclusively to the "externally tagged" enum
315    // representation.
316    //
317    // Serialize this to JSON in externally tagged form as `{ NAME: VALUE }`.
318    fn serialize_newtype_variant<T>(
319        self,
320        _name: &'static str,
321        _variant_index: u32,
322        variant: &'static str,
323        value: &T,
324    ) -> Result<()>
325    where
326        T: ?Sized + Serialize,
327    {
328        self.push_object()?;
329
330        self.current_is_key = true;
331        variant.serialize(&mut *self)?;
332        value.serialize(&mut *self)?;
333
334        self.pop()?;
335
336        Ok(())
337    }
338
339    // Now we get to the serialization of compound types.
340    //
341    // The start of the sequence, each value, and the end are three separate
342    // method calls. This one is responsible only for serializing the start,
343    // which in JSON is `[`.
344    //
345    // The length of the sequence may or may not be known ahead of time. This
346    // doesn't make a difference in JSON because the length is not represented
347    // explicitly in the serialized form. Some serializers may only be able to
348    // support sequences for which the length is known up front.
349    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
350        self.push_array()?;
351        Ok(self)
352    }
353
354    // Tuples look just like sequences in JSON. Some formats may be able to
355    // represent tuples more efficiently by omitting the length, since tuple
356    // means that the corresponding `Deserialize implementation will know the
357    // length without needing to look at the serialized data.
358    fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
359        self.serialize_seq(Some(len))
360    }
361
362    // Tuple structs look just like sequences in JSON.
363    fn serialize_tuple_struct(
364        self,
365        _name: &'static str,
366        len: usize,
367    ) -> Result<Self::SerializeTupleStruct> {
368        self.serialize_seq(Some(len))
369    }
370
371    // Tuple variants are represented in JSON as `{ NAME: [DATA...] }`. Again
372    // this method is only responsible for the externally tagged representation.
373    fn serialize_tuple_variant(
374        self,
375        _name: &'static str,
376        _variant_index: u32,
377        variant: &'static str,
378        _len: usize,
379    ) -> Result<Self::SerializeTupleVariant> {
380        self.push_object()?;
381        self.current_is_key = true;
382        variant.serialize(&mut *self)?;
383        self.push_array()?;
384        Ok(self)
385    }
386
387    // Maps are represented in JSON as `{ K: V, K: V, ... }`.
388    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
389        self.push_object()?;
390        Ok(self)
391    }
392
393    // Structs look just like maps in JSON. In particular, JSON requires that we
394    // serialize the field names of the struct. Other formats may be able to
395    // omit the field names when serializing structs because the corresponding
396    // Deserialize implementation is required to know what the keys are without
397    // looking at the serialized data.
398    fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
399        self.serialize_map(Some(len))
400    }
401
402    // Struct variants are represented in JSON as `{ NAME: { K: V, ... } }`.
403    // This is the externally tagged representation.
404    fn serialize_struct_variant(
405        self,
406        _name: &'static str,
407        _variant_index: u32,
408        variant: &'static str,
409        _len: usize,
410    ) -> Result<Self::SerializeStructVariant> {
411        self.push_object()?;
412        self.current_is_key = true;
413        variant.serialize(&mut *self)?;
414        self.push_object()?;
415
416        Ok(self)
417    }
418}
419
420// The following 7 impls deal with the serialization of compound types like
421// sequences and maps. Serialization of such types is begun by a Serializer
422// method and followed by zero or more calls to serialize individual elements of
423// the compound type and one call to end the compound type.
424//
425// This impl is SerializeSeq so these methods are called after `serialize_seq`
426// is called on the Serializer.
427impl<'a> ser::SerializeSeq for &'a mut Serializer {
428    // Must match the `Ok` type of the serializer.
429    type Ok = ();
430    // Must match the `Error` type of the serializer.
431    type Error = Error;
432
433    // Serialize a single element of the sequence.
434    fn serialize_element<T>(&mut self, value: &T) -> Result<()>
435    where
436        T: ?Sized + Serialize,
437    {
438        value.serialize(&mut **self)
439    }
440
441    // Close the sequence.
442    fn end(self) -> Result<()> {
443        self.pop()?;
444        Ok(())
445    }
446}
447
448// Same thing but for tuples.
449impl<'a> ser::SerializeTuple for &'a mut Serializer {
450    type Ok = ();
451    type Error = Error;
452
453    fn serialize_element<T>(&mut self, value: &T) -> Result<()>
454    where
455        T: ?Sized + Serialize,
456    {
457        value.serialize(&mut **self)
458    }
459
460    fn end(self) -> Result<()> {
461        self.pop()?;
462        Ok(())
463    }
464}
465
466// Same thing but for tuple structs.
467impl<'a> ser::SerializeTupleStruct for &'a mut Serializer {
468    type Ok = ();
469    type Error = Error;
470
471    fn serialize_field<T>(&mut self, value: &T) -> Result<()>
472    where
473        T: ?Sized + Serialize,
474    {
475        value.serialize(&mut **self)
476    }
477
478    fn end(self) -> Result<()> {
479        self.pop()?;
480        Ok(())
481    }
482}
483
484// Tuple variants are a little different. Refer back to the
485// `serialize_tuple_variant` method above:
486//
487//    self.output += "{";
488//    variant.serialize(&mut *self)?;
489//    self.output += ":[";
490//
491// So the `end` method in this impl is responsible for closing both the `]` and
492// the `}`.
493impl<'a> ser::SerializeTupleVariant for &'a mut Serializer {
494    type Ok = ();
495    type Error = Error;
496
497    fn serialize_field<T>(&mut self, value: &T) -> Result<()>
498    where
499        T: ?Sized + Serialize,
500    {
501        value.serialize(&mut **self)
502    }
503
504    fn end(self) -> Result<()> {
505        self.pop()?;
506        self.pop()?;
507        Ok(())
508    }
509}
510
511// Some `Serialize` types are not able to hold a key and value in memory at the
512// same time so `SerializeMap` implementations are required to support
513// `serialize_key` and `serialize_value` individually.
514//
515// There is a third optional method on the `SerializeMap` trait. The
516// `serialize_entry` method allows serializers to optimize for the case where
517// key and value are both available simultaneously. In JSON it doesn't make a
518// difference so the default behavior for `serialize_entry` is fine.
519impl<'a> ser::SerializeMap for &'a mut Serializer {
520    type Ok = ();
521    type Error = Error;
522
523    // The Serde data model allows map keys to be any serializable type. JSON
524    // only allows string keys so the implementation below will produce invalid
525    // JSON if the key serializes as something other than a string.
526    //
527    // A real JSON serializer would need to validate that map keys are strings.
528    // This can be done by using a different Serializer to serialize the key
529    // (instead of `&mut **self`) and having that other serializer only
530    // implement `serialize_str` and return an error on any other data type.
531    fn serialize_key<T>(&mut self, key: &T) -> Result<()>
532    where
533        T: ?Sized + Serialize,
534    {
535        if self.current_key.is_none() {
536            self.current_is_key = true;
537        } else {
538            return Err(Error::Message(
539                "Cannot serialize map with more than one key".to_string(),
540            ));
541        }
542
543        key.serialize(&mut **self)
544    }
545
546    // It doesn't make a difference whether the colon is printed at the end of
547    // `serialize_key` or at the beginning of `serialize_value`. In this case
548    // the code is a bit simpler having it here.
549    fn serialize_value<T>(&mut self, value: &T) -> Result<()>
550    where
551        T: ?Sized + Serialize,
552    {
553        value.serialize(&mut **self)
554    }
555
556    fn end(self) -> Result<()> {
557        self.pop()?;
558        Ok(())
559    }
560}
561
562// Structs are like maps in which the keys are constrained to be compile-time
563// constant strings.
564impl<'a> ser::SerializeStruct for &'a mut Serializer {
565    type Ok = ();
566    type Error = Error;
567
568    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
569    where
570        T: ?Sized + Serialize,
571    {
572        if self.current_key.is_none() {
573            self.current_is_key = true;
574        } else {
575            return Err(Error::Message(
576                "Cannot serialize map with more than one key".to_string(),
577            ));
578        }
579        key.serialize(&mut **self)?;
580        value.serialize(&mut **self)
581    }
582
583    fn end(self) -> Result<()> {
584        self.pop()?;
585        Ok(())
586    }
587}
588
589// Similar to `SerializeTupleVariant`, here the `end` method is responsible for
590// closing both of the curly braces opened by `serialize_struct_variant`.
591impl<'a> ser::SerializeStructVariant for &'a mut Serializer {
592    type Ok = ();
593    type Error = Error;
594
595    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
596    where
597        T: ?Sized + Serialize,
598    {
599        if self.current_key.is_none() {
600            self.current_is_key = true;
601        } else {
602            return Err(Error::Message(
603                "Cannot serialize map with more than one key".to_string(),
604            ));
605        }
606        key.serialize(&mut **self)?;
607        value.serialize(&mut **self)
608    }
609
610    fn end(self) -> Result<()> {
611        self.pop()?;
612        self.pop()?;
613        Ok(())
614    }
615}
616
617////////////////////////////////////////////////////////////////////////////////