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;
|
2021-01-18 03:30:58 +01:00
|
|
|
use crate::{Dynamic, ImmutableString};
|
2021-03-31 04:16:38 +02:00
|
|
|
use serde::ser::{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
|
|
|
|
2021-07-10 04:11:14 +02:00
|
|
|
#[cfg(not(feature = "no_std"))]
|
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 {
|
2021-05-02 17:57:35 +02:00
|
|
|
Union::Unit(_, _, _) => ser.serialize_unit(),
|
2021-06-14 06:02:22 +02:00
|
|
|
Union::Bool(x, _, _) => ser.serialize_bool(x),
|
|
|
|
Union::Str(ref s, _, _) => ser.serialize_str(s.as_str()),
|
2021-05-02 17:57:35 +02:00
|
|
|
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"))]
|
2021-06-14 06:02:22 +02:00
|
|
|
Union::Int(x, _, _) => ser.serialize_i64(x),
|
2021-01-18 03:30:58 +01:00
|
|
|
#[cfg(feature = "only_i32")]
|
2021-06-14 06:02:22 +02: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"))]
|
2021-06-14 06:02:22 +02: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")]
|
2021-06-14 06:02:22 +02:00
|
|
|
Union::Float(x, _, _) => ser.serialize_f32(*x),
|
2021-02-13 13:57:56 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "decimal")]
|
|
|
|
#[cfg(not(feature = "f32_float"))]
|
2021-06-16 13:44:54 +02:00
|
|
|
Union::Decimal(ref x, _, _) => {
|
2021-02-13 13:57:56 +01:00
|
|
|
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")]
|
2021-06-16 13:44:54 +02:00
|
|
|
Union::Decimal(ref x, _, _) => {
|
2021-02-13 13:57:56 +01:00
|
|
|
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"))]
|
2021-06-14 06:02:22 +02:00
|
|
|
Union::Array(ref a, _, _) => (**a).serialize(ser),
|
2021-01-18 03:30:58 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2021-06-14 06:02:22 +02:00
|
|
|
Union::Map(ref m, _, _) => {
|
2021-01-18 03:30:58 +01:00
|
|
|
let mut map = ser.serialize_map(Some(m.len()))?;
|
|
|
|
for (k, v) in m.iter() {
|
2021-03-29 12:46:32 +02:00
|
|
|
map.serialize_entry(k.as_str(), v)?;
|
2021-01-18 03:30:58 +01:00
|
|
|
}
|
|
|
|
map.end()
|
|
|
|
}
|
2021-06-14 06:02:22 +02:00
|
|
|
Union::FnPtr(ref f, _, _) => ser.serialize_str(f.fn_name()),
|
2021-01-18 03:30:58 +01:00
|
|
|
#[cfg(not(feature = "no_std"))]
|
2021-06-14 06:02:22 +02:00
|
|
|
Union::TimeStamp(ref x, _, _) => ser.serialize_str(x.as_ref().type_name()),
|
2021-01-18 03:30:58 +01:00
|
|
|
|
2021-06-14 06:02:22 +02: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"))]
|
2021-06-14 06:02:22 +02: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")]
|
2021-06-14 06:02:22 +02:00
|
|
|
Union::Shared(ref cell, _, _) => cell.read().unwrap().serialize(ser),
|
2021-01-18 03:30:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for ImmutableString {
|
|
|
|
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
|
|
|
|
ser.serialize_str(self.as_str())
|
|
|
|
}
|
|
|
|
}
|