use crate::value::owned::{Object, Value};
use crate::StaticNode;
use crate::{stry, Error};
use serde::de::{
self, Deserialize, DeserializeSeed, Deserializer, MapAccess, SeqAccess, Unexpected, Visitor,
};
use serde::forward_to_deserialize_any;
use serde_ext::de::IntoDeserializer;
use std::borrow::Cow;
use std::fmt;
impl<'de> de::Deserializer<'de> for Value {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self {
Self::Static(StaticNode::Null) => visitor.visit_unit(),
Self::Static(StaticNode::Bool(b)) => visitor.visit_bool(b),
Self::Static(StaticNode::I64(n)) => visitor.visit_i64(n),
#[cfg(feature = "128bit")]
Self::Static(StaticNode::I128(n)) => visitor.visit_i128(n),
Self::Static(StaticNode::U64(n)) => visitor.visit_u64(n),
#[cfg(feature = "128bit")]
Self::Static(StaticNode::U128(n)) => visitor.visit_u128(n),
Self::Static(StaticNode::F64(n)) => visitor.visit_f64(n),
Self::String(s) => visitor.visit_string(s),
Self::Array(a) => visit_array(a, visitor),
Self::Object(o) => visit_object(o, visitor),
}
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}
}
fn visit_array<'de, V>(array: Vec<Value>, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
let len = array.len();
let mut deserializer = SeqDeserializer::new(array);
let seq = stry!(visitor.visit_seq(&mut deserializer));
let remaining = deserializer.iter.len();
if remaining == 0 {
Ok(seq)
} else {
Err(serde::de::Error::invalid_length(
len,
&"fewer elements in array",
))
}
}
fn visit_object<'de, V>(object: Box<Object>, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
let len = object.len();
let mut deserializer = ObjectDeserializer::new(object);
let map = stry!(visitor.visit_map(&mut deserializer));
let remaining = deserializer.iter.len();
if remaining == 0 {
Ok(map)
} else {
Err(serde::de::Error::invalid_length(
len,
&"fewer elements in map",
))
}
}
struct SeqDeserializer {
iter: std::vec::IntoIter<Value>,
}
impl SeqDeserializer {
fn new(vec: Vec<Value>) -> Self {
Self {
iter: vec.into_iter(),
}
}
}
impl<'de> serde::Deserializer<'de> for SeqDeserializer {
type Error = Error;
#[inline]
fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
let len = self.iter.len();
if len == 0 {
visitor.visit_unit()
} else {
let ret = stry!(visitor.visit_seq(&mut self));
let remaining = self.iter.len();
if remaining == 0 {
Ok(ret)
} else {
Err(serde::de::Error::invalid_length(
len,
&"fewer elements in array",
))
}
}
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}
}
impl<'de> SeqAccess<'de> for SeqDeserializer {
type Error = Error;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
where
T: DeserializeSeed<'de>,
{
match self.iter.next() {
Some(value) => seed.deserialize(value).map(Some),
None => Ok(None),
}
}
fn size_hint(&self) -> Option<usize> {
match self.iter.size_hint() {
(lower, Some(upper)) if lower == upper => Some(upper),
_ => None,
}
}
}
struct ObjectDeserializer {
iter: <Object as IntoIterator>::IntoIter,
value: Option<Value>,
}
impl ObjectDeserializer {
#[allow(clippy::boxed_local, clippy::needless_pass_by_value)]
fn new(map: Box<Object>) -> Self {
Self {
iter: map.into_iter(),
value: None,
}
}
}
impl<'de> MapAccess<'de> for ObjectDeserializer {
type Error = Error;
fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
where
T: DeserializeSeed<'de>,
{
match self.iter.next() {
Some((key, value)) => {
self.value = Some(value);
let key_de = MapKeyDeserializer {
key: Cow::Owned(key),
};
seed.deserialize(key_de).map(Some)
}
None => Ok(None),
}
}
fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
where
T: DeserializeSeed<'de>,
{
match self.value.take() {
Some(value) => seed.deserialize(value),
None => Err(serde::de::Error::custom("value is missing")),
}
}
fn size_hint(&self) -> Option<usize> {
match self.iter.size_hint() {
(lower, Some(upper)) if lower == upper => Some(upper),
_ => None,
}
}
}
impl<'de> serde::Deserializer<'de> for ObjectDeserializer {
type Error = Error;
#[inline]
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_map(self)
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}
}
struct MapKeyDeserializer<'de> {
key: Cow<'de, str>,
}
macro_rules! deserialize_integer_key {
($method:ident => $visit:ident) => {
fn $method<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
match (self.key.parse(), self.key) {
(Ok(integer), _) => visitor.$visit(integer),
(Err(_), Cow::Borrowed(s)) => visitor.visit_borrowed_str(s),
(Err(_), Cow::Owned(s)) => visitor.visit_string(s),
}
}
}
}
impl<'de> serde::Deserializer<'de> for MapKeyDeserializer<'de> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
BorrowedCowStrDeserializer::new(self.key).deserialize_any(visitor)
}
deserialize_integer_key!(deserialize_i8 => visit_i8);
deserialize_integer_key!(deserialize_i16 => visit_i16);
deserialize_integer_key!(deserialize_i32 => visit_i32);
deserialize_integer_key!(deserialize_i64 => visit_i64);
#[cfg(feature = "128bit")]
deserialize_integer_key!(deserialize_i128 => visit_i128);
deserialize_integer_key!(deserialize_u8 => visit_u8);
deserialize_integer_key!(deserialize_u16 => visit_u16);
deserialize_integer_key!(deserialize_u32 => visit_u32);
deserialize_integer_key!(deserialize_u64 => visit_u64);
#[cfg(feature = "128bit")]
deserialize_integer_key!(deserialize_u128 => visit_u128);
#[inline]
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_some(self)
}
#[inline]
fn deserialize_newtype_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_newtype_struct(self)
}
fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
self.key
.into_deserializer()
.deserialize_enum(name, variants, visitor)
}
forward_to_deserialize_any! {
bool f32 f64 char str string bytes byte_buf unit unit_struct seq tuple
tuple_struct map struct identifier ignored_any
}
}
struct BorrowedCowStrDeserializer<'de> {
value: Cow<'de, str>,
}
impl<'de> BorrowedCowStrDeserializer<'de> {
fn new(value: Cow<'de, str>) -> Self {
BorrowedCowStrDeserializer { value }
}
}
impl<'de> de::Deserializer<'de> for BorrowedCowStrDeserializer<'de> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
match self.value {
Cow::Borrowed(string) => visitor.visit_borrowed_str(string),
Cow::Owned(string) => visitor.visit_string(string),
}
}
fn deserialize_enum<V>(
self,
_name: &str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_enum(self)
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct identifier ignored_any
}
}
impl<'de> de::EnumAccess<'de> for BorrowedCowStrDeserializer<'de> {
type Error = Error;
type Variant = UnitOnly;
fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error>
where
T: de::DeserializeSeed<'de>,
{
let value = seed.deserialize(self)?;
Ok((value, UnitOnly))
}
}
struct UnitOnly;
impl<'de> de::VariantAccess<'de> for UnitOnly {
type Error = Error;
fn unit_variant(self) -> Result<(), Self::Error> {
Ok(())
}
fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error>
where
T: de::DeserializeSeed<'de>,
{
Err(de::Error::invalid_type(
Unexpected::UnitVariant,
&"newtype variant",
))
}
fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
Err(de::Error::invalid_type(
Unexpected::UnitVariant,
&"tuple variant",
))
}
fn struct_variant<V>(
self,
_fields: &'static [&'static str],
_visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
Err(de::Error::invalid_type(
Unexpected::UnitVariant,
&"struct variant",
))
}
}
impl<'de> Deserialize<'de> for Value {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(ValueVisitor)
}
}
struct ValueVisitor;
impl<'de> Visitor<'de> for ValueVisitor {
type Value = Value;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a JSONesque value")
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_unit<E>(self) -> Result<Self::Value, E> {
Ok(Value::Static(StaticNode::Null))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E> {
Ok(Value::Static(StaticNode::Bool(value)))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_none<E>(self) -> Result<Self::Value, E> {
Ok(Value::Static(StaticNode::Null))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(self)
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_i8<E>(self, value: i8) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::Static(StaticNode::I64(i64::from(value))))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_i16<E>(self, value: i16) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::Static(StaticNode::I64(i64::from(value))))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_i32<E>(self, value: i32) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::Static(StaticNode::I64(i64::from(value))))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::Static(StaticNode::I64(value)))
}
#[cfg(feature = "128bit")]
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_i128<E>(self, value: i128) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::Static(StaticNode::I128(value)))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_u8<E>(self, value: u8) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::Static(StaticNode::U64(u64::from(value))))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_u16<E>(self, value: u16) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::Static(StaticNode::U64(u64::from(value))))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_u32<E>(self, value: u32) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::Static(StaticNode::U64(u64::from(value))))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::Static(StaticNode::U64(value)))
}
#[cfg(feature = "128bit")]
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::Static(StaticNode::U128(value)))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_f32<E>(self, value: f32) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::Static(StaticNode::F64(f64::from(value))))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::Static(StaticNode::F64(value)))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_char<E>(self, _value: char) -> Result<Self::Value, E>
where
E: de::Error,
{
unimplemented!()
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_borrowed_str<E>(self, value: &'de str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::from(value))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::String(value.to_owned()))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::String(value))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let size = map.size_hint().unwrap_or_default();
let mut m = Object::with_capacity(size);
while let Some(k) = map.next_key()? {
let v = map.next_value()?;
m.insert(k, v);
}
Ok(Value::from(m))
}
#[cfg_attr(not(feature = "no-inline"), inline)]
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let size = seq.size_hint().unwrap_or_default();
let mut v = Vec::with_capacity(size);
while let Some(e) = seq.next_element()? {
v.push(e);
}
Ok(Value::Array(v))
}
}