rhai/src/serde/serialize.rs

86 lines
3.1 KiB
Rust
Raw Normal View History

//! Implementations of [`serde::Serialize`].
2021-01-21 03:32:05 +01:00
use crate::dynamic::{Union, Variant};
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;
impl Serialize for Dynamic {
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
match &self.0 {
2021-05-02 17:57:35 +02:00
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
#[cfg(not(feature = "only_i32"))]
2021-05-02 17:57:35 +02:00
Union::Int(x, _, _) => ser.serialize_i64(*x),
#[cfg(feature = "only_i32")]
2021-05-02 17:57:35 +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-05-02 17:57:35 +02:00
Union::Float(x, _, _) => ser.serialize_f64(**x),
#[cfg(not(feature = "no_float"))]
#[cfg(feature = "f32_float")]
2021-05-02 17:57:35 +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-05-02 17:57:35 +02:00
Union::Decimal(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-05-02 17:57:35 +02:00
Union::Decimal(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-05-02 17:57:35 +02:00
Union::Array(a, _, _) => (**a).serialize(ser),
#[cfg(not(feature = "no_object"))]
2021-05-02 17:57:35 +02:00
Union::Map(m, _, _) => {
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)?;
}
map.end()
}
2021-05-02 17:57:35 +02:00
Union::FnPtr(f, _, _) => ser.serialize_str(f.fn_name()),
#[cfg(not(feature = "no_std"))]
2021-05-02 17:57:35 +02:00
Union::TimeStamp(x, _, _) => ser.serialize_str(x.as_ref().type_name()),
2021-05-02 17:57:35 +02:00
Union::Variant(v, _, _) => ser.serialize_str((***v).type_name()),
#[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "sync"))]
2021-05-02 17:57:35 +02:00
Union::Shared(cell, _, _) => cell.borrow().serialize(ser),
#[cfg(not(feature = "no_closure"))]
#[cfg(feature = "sync")]
2021-05-02 17:57:35 +02:00
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())
}
}