2021-01-18 03:30:58 +01:00
|
|
|
//! Implementations of [`serde::Serialize`].
|
|
|
|
|
2021-01-21 03:32:05 +01:00
|
|
|
use crate::dynamic::{Union, Variant};
|
2021-01-18 03:30:58 +01:00
|
|
|
use crate::stdlib::string::ToString;
|
|
|
|
use crate::{Dynamic, ImmutableString};
|
|
|
|
use serde::ser::{Serialize, SerializeMap, Serializer};
|
|
|
|
|
|
|
|
impl Serialize for Dynamic {
|
|
|
|
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
|
|
|
|
match &self.0 {
|
|
|
|
Union::Unit(_, _) => ser.serialize_unit(),
|
|
|
|
Union::Bool(x, _) => ser.serialize_bool(*x),
|
|
|
|
Union::Str(s, _) => ser.serialize_str(s.as_str()),
|
|
|
|
Union::Char(c, _) => ser.serialize_str(&c.to_string()),
|
2021-02-13 13:57:56 +01:00
|
|
|
|
2021-01-18 03:30:58 +01:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
Union::Int(x, _) => ser.serialize_i64(*x),
|
|
|
|
#[cfg(feature = "only_i32")]
|
|
|
|
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"))]
|
|
|
|
Union::Float(x, _) => ser.serialize_f64(**x),
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
#[cfg(feature = "f32_float")]
|
|
|
|
Union::Float(x, _) => ser.serialize_f32(*x),
|
2021-02-13 13:57:56 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "decimal")]
|
|
|
|
#[cfg(not(feature = "f32_float"))]
|
|
|
|
Union::Decimal(x, _) => {
|
|
|
|
use rust_decimal::prelude::ToPrimitive;
|
|
|
|
|
|
|
|
if let Some(v) = x.to_f64() {
|
|
|
|
ser.serialize_f64(v)
|
|
|
|
} else {
|
|
|
|
ser.serialize_str(&x.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(feature = "decimal")]
|
|
|
|
#[cfg(feature = "f32_float")]
|
|
|
|
Union::Decimal(x, _) => {
|
|
|
|
use rust_decimal::prelude::ToPrimitive;
|
|
|
|
|
|
|
|
if let Some(v) = x.to_f32() {
|
|
|
|
ser.serialize_f32(v)
|
|
|
|
} else {
|
|
|
|
ser.serialize_str(&x.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 03:30:58 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
Union::Array(a, _) => (**a).serialize(ser),
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
Union::Map(m, _) => {
|
|
|
|
let mut map = ser.serialize_map(Some(m.len()))?;
|
|
|
|
for (k, v) in m.iter() {
|
|
|
|
map.serialize_entry(k, v)?;
|
|
|
|
}
|
|
|
|
map.end()
|
|
|
|
}
|
|
|
|
Union::FnPtr(f, _) => ser.serialize_str(f.fn_name()),
|
|
|
|
#[cfg(not(feature = "no_std"))]
|
2021-01-21 03:32:05 +01:00
|
|
|
Union::TimeStamp(x, _) => ser.serialize_str(x.as_ref().type_name()),
|
2021-01-18 03:30:58 +01:00
|
|
|
|
|
|
|
Union::Variant(v, _) => ser.serialize_str((***v).type_name()),
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
Union::Shared(cell, _) => cell.borrow().serialize(ser),
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
Union::Shared(cell, _) => cell.read().unwrap().serialize(ser),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for ImmutableString {
|
|
|
|
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
|
|
|
|
ser.serialize_str(self.as_str())
|
|
|
|
}
|
|
|
|
}
|