use serde::Serialize;
use serde::ser::SerializeStructVariant;
use serde_json::{
Map,
Value,
};
use crate::strict_json::{
Result,
StrictJsonError,
to_value,
};
pub(in crate::strict_json) struct StructVariantSerializer {
pub(in crate::strict_json) variant: String,
pub(in crate::strict_json) values: Map<String, Value>,
}
impl SerializeStructVariant for StructVariantSerializer {
type Ok = Value;
type Error = StrictJsonError;
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
to_value(value).map(|value| {
self.values.insert(key.to_string(), value);
})
}
fn end(self) -> Result<Value> {
let mut object = Map::new();
object.insert(self.variant, Value::Object(self.values));
Ok(Value::Object(object))
}
}