use super::{
error::{Error, Result},
serialize_to_value, IntoValue, Object, Value,
};
use serde::ser::{Impossible, Serialize};
use std::{
borrow::ToOwned,
fmt::Display,
result,
string::{String, ToString},
vec::Vec,
};
impl Serialize for Value {
#[inline]
fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
match self {
Self::Bool(b) => serializer.serialize_bool(*b),
Self::Number(n) => n.serialize(serializer),
Self::String(s) => serializer.serialize_str(s),
Self::Array(v) => v.serialize(serializer),
Self::Object(m) => {
use serde::ser::SerializeMap;
let mut map = serializer.serialize_map(Some(m.len()))?;
for (k, v) in m {
map.serialize_entry(k, v)?;
}
map.end()
}
Self::Null | Self::Function(_) | Self::Range(_, _) => serializer.serialize_unit(),
}
}
}
pub struct Serializer;
impl serde::Serializer for Serializer {
type Ok = Value;
type Error = Error;
type SerializeSeq = SerializeVec;
type SerializeTuple = SerializeVec;
type SerializeTupleStruct = SerializeVec;
type SerializeTupleVariant = SerializeTupleVariant;
type SerializeMap = SerializeMap;
type SerializeStruct = SerializeMap;
type SerializeStructVariant = SerializeStructVariant;
#[inline]
fn serialize_bool(self, value: bool) -> Result<Value> {
Ok(Value::Bool(value))
}
#[inline]
fn serialize_i8(self, value: i8) -> Result<Value> {
self.serialize_i64(i64::from(value))
}
#[inline]
fn serialize_i16(self, value: i16) -> Result<Value> {
self.serialize_i64(i64::from(value))
}
#[inline]
fn serialize_i32(self, value: i32) -> Result<Value> {
self.serialize_i64(i64::from(value))
}
#[inline]
fn serialize_i64(self, value: i64) -> Result<Value> {
#[allow(clippy::cast_precision_loss)]
self.serialize_f64(value as f64)
}
#[inline]
fn serialize_u8(self, value: u8) -> Result<Value> {
self.serialize_u64(u64::from(value))
}
#[inline]
fn serialize_u16(self, value: u16) -> Result<Value> {
self.serialize_u64(u64::from(value))
}
#[inline]
fn serialize_u32(self, value: u32) -> Result<Value> {
self.serialize_u64(u64::from(value))
}
#[inline]
fn serialize_u64(self, value: u64) -> Result<Value> {
#[allow(clippy::cast_precision_loss)]
Ok(Value::Number(value as f64))
}
#[inline]
fn serialize_f32(self, float: f32) -> Result<Value> {
Ok(float.into_value())
}
#[inline]
fn serialize_f64(self, float: f64) -> Result<Value> {
Ok(float.into_value())
}
#[inline]
fn serialize_char(self, value: char) -> Result<Value> {
let mut s = String::new();
s.push(value);
Ok(Value::String(s))
}
#[inline]
fn serialize_str(self, value: &str) -> Result<Value> {
Ok(Value::String(value.to_owned()))
}
fn serialize_bytes(self, value: &[u8]) -> Result<Value> {
let vec = value.iter().map(|&b| Value::Number(b.into())).collect();
Ok(Value::Array(vec))
}
#[inline]
fn serialize_none(self) -> Result<Value> {
self.serialize_unit()
}
#[inline]
fn serialize_some<T>(self, value: &T) -> Result<Value>
where
T: ?Sized + Serialize,
{
value.serialize(self)
}
#[inline]
fn serialize_unit(self) -> Result<Value> {
Ok(Value::Null)
}
#[inline]
fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> {
self.serialize_unit()
}
#[inline]
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
) -> Result<Value> {
self.serialize_str(variant)
}
#[inline]
fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<Value>
where
T: ?Sized + Serialize,
{
value.serialize(self)
}
fn serialize_newtype_variant<T>(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
value: &T,
) -> Result<Value>
where
T: ?Sized + Serialize,
{
let mut values = Object::new();
values.insert(String::from(variant), serialize_to_value(value)?);
Ok(Value::Object(values))
}
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
Ok(SerializeVec {
vec: Vec::with_capacity(len.unwrap_or(0)),
})
}
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
self.serialize_seq(Some(len))
}
fn serialize_tuple_struct(
self,
_name: &'static str,
len: usize,
) -> Result<Self::SerializeTupleStruct> {
self.serialize_seq(Some(len))
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
len: usize,
) -> Result<Self::SerializeTupleVariant> {
Ok(SerializeTupleVariant {
name: String::from(variant),
vec: Vec::with_capacity(len),
})
}
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
Ok(SerializeMap {
map: Object::new(),
next_key: None,
})
}
fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
self.serialize_map(Some(len))
}
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant> {
Ok(SerializeStructVariant {
name: String::from(variant),
map: Object::new(),
})
}
fn collect_str<T>(self, value: &T) -> Result<Value>
where
T: ?Sized + Display,
{
Ok(Value::String(value.to_string()))
}
}
pub struct SerializeVec {
vec: Vec<Value>,
}
impl serde::ser::SerializeSeq for SerializeVec {
type Ok = Value;
type Error = Error;
fn serialize_element<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
self.vec.push(serialize_to_value(value)?);
Ok(())
}
fn end(self) -> Result<Value> {
Ok(Value::Array(self.vec))
}
}
impl serde::ser::SerializeTuple for SerializeVec {
type Ok = Value;
type Error = Error;
fn serialize_element<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
serde::ser::SerializeSeq::serialize_element(self, value)
}
fn end(self) -> Result<Value> {
serde::ser::SerializeSeq::end(self)
}
}
impl serde::ser::SerializeTupleStruct for SerializeVec {
type Ok = Value;
type Error = Error;
fn serialize_field<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
serde::ser::SerializeSeq::serialize_element(self, value)
}
fn end(self) -> Result<Value> {
serde::ser::SerializeSeq::end(self)
}
}
pub struct SerializeTupleVariant {
name: String,
vec: Vec<Value>,
}
impl serde::ser::SerializeTupleVariant for SerializeTupleVariant {
type Ok = Value;
type Error = Error;
fn serialize_field<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
self.vec.push(serialize_to_value(value)?);
Ok(())
}
fn end(self) -> Result<Value> {
let mut object = Object::new();
object.insert(self.name, Value::Array(self.vec));
Ok(Value::Object(object))
}
}
pub struct SerializeMap {
map: Object,
next_key: Option<String>,
}
impl serde::ser::SerializeMap for SerializeMap {
type Ok = Value;
type Error = Error;
fn serialize_key<T>(&mut self, key: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
self.next_key = Some(key.serialize(MapKeySerializer)?);
Ok(())
}
fn serialize_value<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
let key = self.next_key.take();
let key = key.expect("serialize_value called before serialize_key");
self.map.insert(key, serialize_to_value(value)?);
Ok(())
}
fn end(self) -> Result<Value> {
Ok(Value::Object(self.map))
}
}
struct MapKeySerializer;
impl serde::Serializer for MapKeySerializer {
type Ok = String;
type Error = Error;
type SerializeSeq = Impossible<String, Error>;
type SerializeTuple = Impossible<String, Error>;
type SerializeTupleStruct = Impossible<String, Error>;
type SerializeTupleVariant = Impossible<String, Error>;
type SerializeMap = Impossible<String, Error>;
type SerializeStruct = Impossible<String, Error>;
type SerializeStructVariant = Impossible<String, Error>;
#[inline]
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
) -> Result<String> {
Ok(variant.to_owned())
}
#[inline]
fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<String>
where
T: ?Sized + Serialize,
{
value.serialize(self)
}
fn serialize_bool(self, _value: bool) -> Result<String> {
Err(Error::Serialize("key must be a string".to_string()))
}
fn serialize_i8(self, value: i8) -> Result<String> {
Ok(value.to_string())
}
fn serialize_i16(self, value: i16) -> Result<String> {
Ok(value.to_string())
}
fn serialize_i32(self, value: i32) -> Result<String> {
Ok(value.to_string())
}
fn serialize_i64(self, value: i64) -> Result<String> {
Ok(value.to_string())
}
fn serialize_u8(self, value: u8) -> Result<String> {
Ok(value.to_string())
}
fn serialize_u16(self, value: u16) -> Result<String> {
Ok(value.to_string())
}
fn serialize_u32(self, value: u32) -> Result<String> {
Ok(value.to_string())
}
fn serialize_u64(self, value: u64) -> Result<String> {
Ok(value.to_string())
}
fn serialize_f32(self, _value: f32) -> Result<String> {
Err(Error::Serialize("key must be a string".to_string()))
}
fn serialize_f64(self, _value: f64) -> Result<String> {
Err(Error::Serialize("key must be a string".to_string()))
}
#[inline]
fn serialize_char(self, value: char) -> Result<String> {
Ok(value.to_string())
}
#[inline]
fn serialize_str(self, value: &str) -> Result<String> {
Ok(value.to_owned())
}
fn serialize_bytes(self, _value: &[u8]) -> Result<String> {
Err(Error::Serialize("key must be a string".to_string()))
}
fn serialize_unit(self) -> Result<String> {
Err(Error::Serialize("key must be a string".to_string()))
}
fn serialize_unit_struct(self, _name: &'static str) -> Result<String> {
Err(Error::Serialize("key must be a string".to_string()))
}
fn serialize_newtype_variant<T>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T,
) -> Result<String>
where
T: ?Sized + Serialize,
{
Err(Error::Serialize("key must be a string".to_string()))
}
fn serialize_none(self) -> Result<String> {
Err(Error::Serialize("key must be a string".to_string()))
}
fn serialize_some<T>(self, _value: &T) -> Result<String>
where
T: ?Sized + Serialize,
{
Err(Error::Serialize("key must be a string".to_string()))
}
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
Err(Error::Serialize("key must be a string".to_string()))
}
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
Err(Error::Serialize("key must be a string".to_string()))
}
fn serialize_tuple_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleStruct> {
Err(Error::Serialize("key must be a string".to_string()))
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant> {
Err(Error::Serialize("key must be a string".to_string()))
}
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
Err(Error::Serialize("key must be a string".to_string()))
}
fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
Err(Error::Serialize("key must be a string".to_string()))
}
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant> {
Err(Error::Serialize("key must be a string".to_string()))
}
fn collect_str<T>(self, value: &T) -> Result<String>
where
T: ?Sized + Display,
{
Ok(value.to_string())
}
}
impl serde::ser::SerializeStruct for SerializeMap {
type Ok = Value;
type Error = Error;
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
serde::ser::SerializeMap::serialize_entry(self, key, value)
}
fn end(self) -> Result<Value> {
serde::ser::SerializeMap::end(self)
}
}
pub struct SerializeStructVariant {
name: String,
map: Object,
}
impl serde::ser::SerializeStructVariant for SerializeStructVariant {
type Ok = Value;
type Error = Error;
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
self.map
.insert(String::from(key), serialize_to_value(value)?);
Ok(())
}
fn end(self) -> Result<Value> {
let mut object = Object::new();
object.insert(self.name, Value::Object(self.map));
Ok(Value::Object(object))
}
}