use std::{mem::MaybeUninit, result::Result as StdResult, slice};
use ::serde::{
de::{
self, Deserialize, DeserializeSeed, EnumAccess, Expected, IntoDeserializer, MapAccess,
SeqAccess, Unexpected, VariantAccess, Visitor,
},
forward_to_deserialize_any,
};
use super::node::ValueRef;
use crate::{
error::{Error, ErrorCode},
reader::Reader,
serde::{number::N, tri},
value::{node::Value, Object},
};
pub fn from_value<'de, T>(value: &'de Value) -> Result<T, Error>
where
T: Deserialize<'de>,
{
T::deserialize(value)
}
impl<'de> Deserialize<'de> for Value {
fn deserialize<D>(deserializer: D) -> StdResult<Self, D::Error>
where
D: ::serde::Deserializer<'de>,
{
deserializer.deserialize_newtype_struct(TOKEN, ValueVisitor)
}
}
pub(crate) const TOKEN: &str = "$sonic_rs::private::Value";
pub(crate) struct ValueVisitor;
impl<'de> Visitor<'de> for ValueVisitor {
type Value = Value;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("a valid json")
}
fn visit_bytes<E>(self, value_binary: &[u8]) -> StdResult<Value, E>
where
E: de::Error,
{
unsafe {
assert!(
value_binary.len() == std::mem::size_of::<Value>(),
"invalid value size {}",
value_binary.len()
);
let mut dom: MaybeUninit<Value> = MaybeUninit::zeroed();
std::ptr::copy_nonoverlapping(
value_binary.as_ptr() as *const Value,
dom.as_mut_ptr(),
1,
);
Ok(dom.assume_init())
}
}
}
struct SeqRefDeserializer<'de> {
iter: slice::Iter<'de, Value>,
}
impl<'de> SeqRefDeserializer<'de> {
fn new(slice: &'de [Value]) -> Self {
SeqRefDeserializer { iter: slice.iter() }
}
}
impl<'de> SeqAccess<'de> for SeqRefDeserializer<'de> {
type Error = Error;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, 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 MapRefDeserializer<'de> {
iter: <&'de Object as IntoIterator>::IntoIter,
value: Option<&'de Value>,
}
impl<'de> MapRefDeserializer<'de> {
fn new(map: &'de Object) -> Self {
MapRefDeserializer {
iter: map.into_iter(),
value: None,
}
}
}
impl<'de> MapAccess<'de> for MapRefDeserializer<'de> {
type Error = Error;
fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error>
where
T: DeserializeSeed<'de>,
{
match self.iter.next() {
Some((key, value)) => {
self.value = Some(value);
let key_de = MapKeyDeserializer { key };
seed.deserialize(key_de).map(Some)
}
None => Ok(None),
}
}
fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, 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,
}
}
}
struct MapKeyDeserializer<'de> {
key: &'de str,
}
macro_rules! deserialize_numeric_key {
($method:ident) => {
deserialize_numeric_key!($method, deserialize_number);
};
($method:ident, $using:ident) => {
fn $method<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
let mut de =
crate::Deserializer::new(crate::reader::Read::new(self.key.as_bytes(), false));
match de.parser.read.peek() {
Some(b'0'..=b'9' | b'-') => {}
_ => return Err(Error::syntax(ErrorCode::ExpectedNumericKey, b"", 0)),
}
let number = tri!(de.$using(visitor));
Ok(number)
}
};
}
impl<'de> serde::Deserializer<'de> for MapKeyDeserializer<'de> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
visitor.visit_borrowed_str(self.key)
}
deserialize_numeric_key!(deserialize_i8);
deserialize_numeric_key!(deserialize_i16);
deserialize_numeric_key!(deserialize_i32);
deserialize_numeric_key!(deserialize_i64);
deserialize_numeric_key!(deserialize_u8);
deserialize_numeric_key!(deserialize_u16);
deserialize_numeric_key!(deserialize_u32);
deserialize_numeric_key!(deserialize_u64);
deserialize_numeric_key!(deserialize_f32);
deserialize_numeric_key!(deserialize_f64);
deserialize_numeric_key!(deserialize_i128, deserialize_i128);
deserialize_numeric_key!(deserialize_u128, deserialize_u128);
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
if self.key == "true" {
visitor.visit_bool(true)
} else if self.key == "false" {
visitor.visit_bool(false)
} else {
Err(serde::de::Error::invalid_type(
Unexpected::Str(self.key),
&visitor,
))
}
}
#[inline]
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
visitor.visit_some(self)
}
#[inline]
fn deserialize_newtype_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, 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, Error>
where
V: Visitor<'de>,
{
self.key
.into_deserializer()
.deserialize_enum(name, variants, visitor)
}
forward_to_deserialize_any! {
char str string bytes byte_buf unit unit_struct seq tuple tuple_struct
map struct identifier ignored_any
}
}
fn visit_array_ref<'de, V>(array: &'de [Value], visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
let len = array.len();
let mut deserializer = SeqRefDeserializer::new(array);
let seq = tri!(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_ref<'de, V>(object: &'de Object, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
let len = object.len();
let mut deserializer = MapRefDeserializer::new(object);
let map = tri!(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",
))
}
}
macro_rules! deserialize_number {
($method:ident) => {
fn $method<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.as_ref() {
ValueRef::Number(n) => n.deserialize_any(visitor),
_ => Err(self.invalid_type(&visitor)),
}
}
};
}
impl Value {
#[cold]
fn invalid_type<E>(&self, exp: &dyn Expected) -> E
where
E: serde::de::Error,
{
serde::de::Error::invalid_type(self.unexpected(), exp)
}
#[cold]
fn unexpected(&self) -> Unexpected<'_> {
self.as_ref().unexpected()
}
}
impl<'a> ValueRef<'a> {
#[cold]
fn unexpected(&self) -> Unexpected<'a> {
match self {
ValueRef::Null => Unexpected::Unit,
ValueRef::Bool(b) => Unexpected::Bool(*b),
ValueRef::Number(n) => match n.n {
N::PosInt(u) => Unexpected::Unsigned(u),
N::NegInt(i) => Unexpected::Signed(i),
N::Float(f) => Unexpected::Float(f),
},
ValueRef::String(s) => Unexpected::Str(s),
ValueRef::Array(_) => Unexpected::Seq,
ValueRef::Object(_) => Unexpected::Map,
}
}
}
struct EnumRefDeserializer<'de> {
variant: &'de str,
value: Option<&'de Value>,
}
impl<'de> EnumAccess<'de> for EnumRefDeserializer<'de> {
type Error = Error;
type Variant = VariantRefDeserializer<'de>;
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Error>
where
V: DeserializeSeed<'de>,
{
let variant = self.variant.into_deserializer();
let visitor = VariantRefDeserializer { value: self.value };
seed.deserialize(variant).map(|v| (v, visitor))
}
}
struct VariantRefDeserializer<'de> {
value: Option<&'de Value>,
}
impl<'de> VariantAccess<'de> for VariantRefDeserializer<'de> {
type Error = Error;
fn unit_variant(self) -> Result<(), Error> {
match self.value {
Some(value) => Deserialize::deserialize(value),
None => Ok(()),
}
}
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Error>
where
T: DeserializeSeed<'de>,
{
match self.value {
Some(value) => seed.deserialize(value),
None => Err(serde::de::Error::invalid_type(
Unexpected::UnitVariant,
&"newtype variant",
)),
}
}
fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.value.map(|v| v.as_ref()) {
Some(ValueRef::Array(v)) => {
if v.is_empty() {
visitor.visit_unit()
} else {
visit_array_ref(v, visitor)
}
}
Some(other) => Err(serde::de::Error::invalid_type(
other.unexpected(),
&"tuple variant",
)),
None => Err(serde::de::Error::invalid_type(
Unexpected::UnitVariant,
&"tuple variant",
)),
}
}
fn struct_variant<V>(
self,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.value.map(|v| v.as_ref()) {
Some(ValueRef::Object(v)) => visit_object_ref(v, visitor),
Some(other) => Err(serde::de::Error::invalid_type(
other.unexpected(),
&"struct variant",
)),
None => Err(serde::de::Error::invalid_type(
Unexpected::UnitVariant,
&"struct variant",
)),
}
}
}
impl<'de> IntoDeserializer<'de, Error> for &'de Value {
type Deserializer = Self;
fn into_deserializer(self) -> Self::Deserializer {
self
}
}
impl<'de> serde::Deserializer<'de> for &'de Value {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.as_ref() {
ValueRef::Null => visitor.visit_unit(),
ValueRef::Bool(v) => visitor.visit_bool(v),
ValueRef::Number(n) => n.deserialize_any(visitor),
ValueRef::String(v) => visitor.visit_borrowed_str(v),
ValueRef::Array(v) => visit_array_ref(v, visitor),
ValueRef::Object(v) => visit_object_ref(v, visitor),
}
}
deserialize_number!(deserialize_i8);
deserialize_number!(deserialize_i16);
deserialize_number!(deserialize_i32);
deserialize_number!(deserialize_i64);
deserialize_number!(deserialize_i128);
deserialize_number!(deserialize_u8);
deserialize_number!(deserialize_u16);
deserialize_number!(deserialize_u32);
deserialize_number!(deserialize_u64);
deserialize_number!(deserialize_u128);
deserialize_number!(deserialize_f32);
deserialize_number!(deserialize_f64);
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.as_ref() {
ValueRef::Null => visitor.visit_none(),
_ => visitor.visit_some(self),
}
}
fn deserialize_enum<V>(
self,
_name: &str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
let (variant, value) = match self.as_ref() {
ValueRef::Object(value) => {
let mut iter = value.into_iter();
let (variant, value) = match iter.next() {
Some(v) => v,
None => {
return Err(serde::de::Error::invalid_value(
Unexpected::Map,
&"map with a single key",
));
}
};
if iter.next().is_some() {
return Err(serde::de::Error::invalid_value(
Unexpected::Map,
&"map with a single key",
));
}
(variant, Some(value))
}
ValueRef::String(variant) => (variant, None),
other => {
return Err(serde::de::Error::invalid_type(
other.unexpected(),
&"string or map",
));
}
};
visitor.visit_enum(EnumRefDeserializer { variant, value })
}
#[inline]
fn deserialize_newtype_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
visitor.visit_newtype_struct(self)
}
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.as_ref() {
ValueRef::Bool(v) => visitor.visit_bool(v),
_ => Err(self.invalid_type(&visitor)),
}
}
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
self.deserialize_str(visitor)
}
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.as_ref() {
ValueRef::String(v) => visitor.visit_borrowed_str(v),
_ => Err(self.invalid_type(&visitor)),
}
}
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
self.deserialize_str(visitor)
}
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.as_ref() {
ValueRef::String(v) => visitor.visit_borrowed_str(v),
ValueRef::Array(v) => visit_array_ref(v, visitor),
_ => Err(self.invalid_type(&visitor)),
}
}
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
self.deserialize_bytes(visitor)
}
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.as_ref() {
ValueRef::Null => visitor.visit_unit(),
_ => Err(self.invalid_type(&visitor)),
}
}
fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
self.deserialize_unit(visitor)
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.as_ref() {
ValueRef::Array(v) => visit_array_ref(v, visitor),
_ => Err(self.invalid_type(&visitor)),
}
}
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
self.deserialize_seq(visitor)
}
fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
_len: usize,
visitor: V,
) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
self.deserialize_seq(visitor)
}
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.as_ref() {
ValueRef::Object(v) => visit_object_ref(v, visitor),
_ => Err(self.invalid_type(&visitor)),
}
}
fn deserialize_struct<V>(
self,
_name: &'static str,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.as_ref() {
ValueRef::Array(v) => visit_array_ref(v, visitor),
ValueRef::Object(v) => visit_object_ref(v, visitor),
_ => Err(self.invalid_type(&visitor)),
}
}
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
self.deserialize_str(visitor)
}
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
visitor.visit_unit()
}
}
#[cfg(test)]
mod test {
#[test]
fn test_value_as_deserializer() {
}
}