Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer/boolean tags for internally/adjacently tagged enums #2056

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2654,6 +2654,28 @@ where
}
}

impl<'de, E> IdentifierDeserializer<'de, E> for i64
where
E: Error
{
type Deserializer = <i64 as IntoDeserializer<'de, E>>::Deserializer;

fn from(self) -> Self::Deserializer {
self.into_deserializer()
}
}

impl<'de, E> IdentifierDeserializer<'de, E> for bool
where
E: Error
{
type Deserializer = <bool as IntoDeserializer<'de, E>>::Deserializer;

fn from(self) -> Self::Deserializer {
self.into_deserializer()
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
pub struct FlatMapDeserializer<'a, 'de: 'a, E>(
pub &'a mut Vec<Option<(Content<'de>, Content<'de>)>>,
Expand Down
37 changes: 28 additions & 9 deletions serde/src/private/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn serialize_tagged_newtype<S, T>(
type_ident: &'static str,
variant_ident: &'static str,
tag: &'static str,
variant_name: &'static str,
variant_name: VariantName,
value: &T,
) -> Result<S::Ok, S::Error>
where
Expand All @@ -39,7 +39,7 @@ struct TaggedSerializer<S> {
type_ident: &'static str,
variant_ident: &'static str,
tag: &'static str,
variant_name: &'static str,
variant_name: VariantName,
delegate: S,
}

Expand Down Expand Up @@ -189,7 +189,7 @@ where

fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
let mut map = try!(self.delegate.serialize_map(Some(1)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_entry(self.tag, &self.variant_name));
map.end()
}

Expand All @@ -200,7 +200,7 @@ where
inner_variant: &'static str,
) -> Result<Self::Ok, Self::Error> {
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_entry(self.tag, &self.variant_name));
try!(map.serialize_entry(inner_variant, &()));
map.end()
}
Expand All @@ -227,7 +227,7 @@ where
T: Serialize,
{
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_entry(self.tag, &self.variant_name));
try!(map.serialize_entry(inner_variant, inner_value));
map.end()
}
Expand Down Expand Up @@ -270,7 +270,7 @@ where
len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_entry(self.tag, &self.variant_name));
try!(map.serialize_key(inner_variant));
Ok(SerializeTupleVariantAsMapValue::new(
map,
Expand All @@ -281,7 +281,7 @@ where

fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
let mut map = try!(self.delegate.serialize_map(len.map(|len| len + 1)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_entry(self.tag, &self.variant_name));
Ok(map)
}

Expand All @@ -291,7 +291,7 @@ where
len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
let mut state = try!(self.delegate.serialize_struct(name, len + 1));
try!(state.serialize_field(self.tag, self.variant_name));
try!(state.serialize_field(self.tag, &self.variant_name));
Ok(state)
}

Expand All @@ -317,7 +317,7 @@ where
len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_entry(self.tag, &self.variant_name));
try!(map.serialize_key(inner_variant));
Ok(SerializeStructVariantAsMapValue::new(
map,
Expand Down Expand Up @@ -1308,3 +1308,22 @@ where
Ok(())
}
}

pub enum VariantName {
String(&'static str),
Integer(i64),
Boolean(bool),
}

impl Serialize for VariantName {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer
{
match self {
VariantName::String(s) => serializer.serialize_str(s),
VariantName::Integer(i) => serializer.serialize_i64(*i),
VariantName::Boolean(b) => serializer.serialize_bool(*b),
}
}
}
Loading