2021-01-18 03:30:58 +01:00
|
|
|
//! Implementations of [`serde::Serialize`].
|
|
|
|
|
2021-11-13 15:36:23 +01:00
|
|
|
use crate::types::dynamic::Union;
|
2022-09-26 12:14:45 +02:00
|
|
|
use crate::{Dynamic, ImmutableString, Scope};
|
|
|
|
use serde::{ser::SerializeSeq, Serialize, Serializer};
|
2021-04-17 09:15:54 +02:00
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
2021-03-31 04:16:38 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
use serde::ser::SerializeMap;
|
2021-01-18 03:30:58 +01:00
|
|
|
|
2022-10-15 06:37:42 +02:00
|
|
|
#[cfg(not(feature = "no_time"))]
|
2021-11-13 15:36:23 +01:00
|
|
|
use crate::types::dynamic::Variant;
|
2021-07-10 04:11:14 +02:00
|
|
|
|
2021-01-18 03:30:58 +01:00
|
|
|
impl Serialize for Dynamic {
|
|
|
|
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
|
2021-06-14 06:02:22 +02:00
|
|
|
match self.0 {
|
2022-11-08 09:16:42 +01:00
|
|
|
Union::Null => unreachable!(),
|
|
|
|
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Unit(..) => ser.serialize_unit(),
|
|
|
|
Union::Bool(x, ..) => ser.serialize_bool(x),
|
|
|
|
Union::Str(ref s, ..) => ser.serialize_str(s.as_str()),
|
2022-11-22 08:38:16 +01:00
|
|
|
Union::Char(c, ..) => ser.serialize_char(c),
|
2021-02-13 13:57:56 +01:00
|
|
|
|
2021-01-18 03:30:58 +01:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Int(x, ..) => ser.serialize_i64(x),
|
2021-01-18 03:30:58 +01:00
|
|
|
#[cfg(feature = "only_i32")]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Int(x, ..) => ser.serialize_i32(x),
|
2021-02-13 13:57:56 +01:00
|
|
|
|
2021-01-18 03:30:58 +01:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
#[cfg(not(feature = "f32_float"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Float(x, ..) => ser.serialize_f64(*x),
|
2021-01-18 03:30:58 +01:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
#[cfg(feature = "f32_float")]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Float(x, ..) => ser.serialize_f32(*x),
|
2021-02-13 13:57:56 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "decimal")]
|
|
|
|
#[cfg(not(feature = "f32_float"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Decimal(ref x, ..) => {
|
2021-02-13 13:57:56 +01:00
|
|
|
use rust_decimal::prelude::ToPrimitive;
|
|
|
|
|
2022-10-10 10:46:35 +02:00
|
|
|
match x.to_f64() {
|
|
|
|
Some(v) => ser.serialize_f64(v),
|
|
|
|
None => ser.serialize_str(&x.to_string()),
|
2021-02-13 13:57:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(feature = "decimal")]
|
|
|
|
#[cfg(feature = "f32_float")]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Decimal(ref x, ..) => {
|
2021-02-13 13:57:56 +01:00
|
|
|
use rust_decimal::prelude::ToPrimitive;
|
|
|
|
|
2022-10-10 10:46:35 +02:00
|
|
|
match x.to_f32() {
|
|
|
|
Some(v) => ser.serialize_f32(v),
|
|
|
|
_ => ser.serialize_str(&x.to_string()),
|
2021-02-13 13:57:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 03:30:58 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Array(ref a, ..) => (**a).serialize(ser),
|
2021-11-23 07:58:54 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-11-23 09:13:57 +01:00
|
|
|
Union::Blob(ref a, ..) => ser.serialize_bytes(a),
|
2021-01-18 03:30:58 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Map(ref m, ..) => {
|
2021-01-18 03:30:58 +01:00
|
|
|
let mut map = ser.serialize_map(Some(m.len()))?;
|
2021-11-15 07:30:00 +01:00
|
|
|
m.iter()
|
|
|
|
.try_for_each(|(k, v)| map.serialize_entry(k.as_str(), v))?;
|
2021-01-18 03:30:58 +01:00
|
|
|
map.end()
|
|
|
|
}
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::FnPtr(ref f, ..) => ser.serialize_str(f.fn_name()),
|
2022-10-15 06:37:42 +02:00
|
|
|
#[cfg(not(feature = "no_time"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::TimeStamp(ref x, ..) => ser.serialize_str(x.as_ref().type_name()),
|
2021-01-18 03:30:58 +01:00
|
|
|
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Variant(ref v, ..) => ser.serialize_str((***v).type_name()),
|
2021-01-18 03:30:58 +01:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
#[cfg(not(feature = "sync"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Shared(ref cell, ..) => cell.borrow().serialize(ser),
|
2021-01-18 03:30:58 +01:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
#[cfg(feature = "sync")]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Shared(ref cell, ..) => cell.read().unwrap().serialize(ser),
|
2021-01-18 03:30:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for ImmutableString {
|
2022-09-25 10:20:36 +02:00
|
|
|
#[inline(always)]
|
2021-01-18 03:30:58 +01:00
|
|
|
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
|
|
|
|
ser.serialize_str(self.as_str())
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 12:14:45 +02:00
|
|
|
|
|
|
|
impl Serialize for Scope<'_> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
|
|
|
|
#[derive(Debug, Clone, Hash, Serialize)]
|
|
|
|
struct ScopeEntry<'a> {
|
|
|
|
pub name: &'a str,
|
|
|
|
pub value: &'a Dynamic,
|
|
|
|
#[serde(default, skip_serializing_if = "is_false")]
|
|
|
|
pub is_constant: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_false(value: &bool) -> bool {
|
|
|
|
!value
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut ser = ser.serialize_seq(Some(self.len()))?;
|
|
|
|
|
|
|
|
for (name, is_constant, value) in self.iter_raw() {
|
|
|
|
let entry = ScopeEntry {
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
is_constant,
|
|
|
|
};
|
|
|
|
ser.serialize_element(&entry)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
ser.end()
|
|
|
|
}
|
|
|
|
}
|