rhai/src/serde/serialize.rs

90 lines
3.2 KiB
Rust
Raw Normal View History

//! Implementations of [`serde::Serialize`].
2021-11-13 15:36:23 +01:00
use crate::types::dynamic::Union;
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-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
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-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()),
Union::Char(c, ..) => ser.serialize_str(&c.to_string()),
2021-02-13 13:57:56 +01:00
#[cfg(not(feature = "only_i32"))]
2022-02-08 02:02:15 +01:00
Union::Int(x, ..) => ser.serialize_i64(x),
#[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
#[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),
#[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;
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")]
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;
if let Some(v) = x.to_f32() {
ser.serialize_f32(v)
} else {
ser.serialize_str(&x.to_string())
}
}
#[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-02-08 02:02:15 +01:00
Union::Blob(ref a, ..) => (**a).serialize(ser),
#[cfg(not(feature = "no_object"))]
2022-02-08 02:02:15 +01:00
Union::Map(ref m, ..) => {
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))?;
map.end()
}
2022-02-08 02:02:15 +01:00
Union::FnPtr(ref f, ..) => ser.serialize_str(f.fn_name()),
#[cfg(not(feature = "no_std"))]
2022-02-08 02:02:15 +01:00
Union::TimeStamp(ref x, ..) => ser.serialize_str(x.as_ref().type_name()),
2022-02-08 02:02:15 +01:00
Union::Variant(ref v, ..) => ser.serialize_str((***v).type_name()),
#[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),
#[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),
}
}
}
impl Serialize for ImmutableString {
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
ser.serialize_str(self.as_str())
}
}