rhai/src/serde/serialize.rs

88 lines
3.1 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 {
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
#[cfg(not(feature = "only_i32"))]
2021-06-14 06:02:22 +02:00
Union::Int(x, _, _) => ser.serialize_i64(x),
#[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
#[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),
#[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())
}
}
#[cfg(not(feature = "no_index"))]
2021-06-14 06:02:22 +02:00
Union::Array(ref a, _, _) => (**a).serialize(ser),
#[cfg(not(feature = "no_object"))]
2021-06-14 06:02:22 +02: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()
}
2021-06-14 06:02:22 +02:00
Union::FnPtr(ref f, _, _) => ser.serialize_str(f.fn_name()),
#[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-06-14 06:02:22 +02:00
Union::Variant(ref v, _, _) => ser.serialize_str((***v).type_name()),
#[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),
#[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),
}
}
}
impl Serialize for ImmutableString {
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
ser.serialize_str(self.as_str())
}
}