Make Scope serializable.

This commit is contained in:
Stephen Chung
2022-09-26 18:14:45 +08:00
parent 335d12e182
commit 8d1310c0f3
6 changed files with 208 additions and 62 deletions

View File

@@ -1,8 +1,8 @@
//! Implementations of [`serde::Serialize`].
use crate::types::dynamic::Union;
use crate::{Dynamic, ImmutableString};
use serde::ser::{Serialize, Serializer};
use crate::{Dynamic, ImmutableString, Scope};
use serde::{ser::SerializeSeq, Serialize, Serializer};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -88,3 +88,33 @@ impl Serialize for ImmutableString {
ser.serialize_str(self.as_str())
}
}
impl Serialize for Scope<'_> {
#[inline(always)]
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
#[derive(Debug, Clone, Hash, Serialize)]
struct ScopeEntry<'a> {
pub name: &'a str,
pub value: &'a Dynamic,
#[serde(default, skip_serializing_if = "is_false")]
pub is_constant: bool,
}
fn is_false(value: &bool) -> bool {
!value
}
let mut ser = ser.serialize_seq(Some(self.len()))?;
for (name, is_constant, value) in self.iter_raw() {
let entry = ScopeEntry {
name,
value,
is_constant,
};
ser.serialize_element(&entry)?;
}
ser.end()
}
}