Support (de-)serializing flattened unit struct

Fixes #2801.
This commit is contained in:
Jon Gjengset 2024-08-15 15:50:40 +02:00
parent 1b4da41f97
commit 4036ff88ed
No known key found for this signature in database
3 changed files with 38 additions and 6 deletions

View File

@ -2710,6 +2710,17 @@ where
visitor.visit_unit() visitor.visit_unit()
} }
fn deserialize_unit_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_unit()
}
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where where
V: Visitor<'de>, V: Visitor<'de>,
@ -2734,7 +2745,6 @@ where
deserialize_string() deserialize_string()
deserialize_bytes() deserialize_bytes()
deserialize_byte_buf() deserialize_byte_buf()
deserialize_unit_struct(&'static str)
deserialize_seq() deserialize_seq()
deserialize_tuple(usize) deserialize_tuple(usize)
deserialize_tuple_struct(&'static str, usize) deserialize_tuple_struct(&'static str, usize)

View File

@ -51,8 +51,6 @@ enum Unsupported {
String, String,
ByteArray, ByteArray,
Optional, Optional,
#[cfg(any(feature = "std", feature = "alloc"))]
UnitStruct,
Sequence, Sequence,
Tuple, Tuple,
TupleStruct, TupleStruct,
@ -69,8 +67,6 @@ impl Display for Unsupported {
Unsupported::String => formatter.write_str("a string"), Unsupported::String => formatter.write_str("a string"),
Unsupported::ByteArray => formatter.write_str("a byte array"), Unsupported::ByteArray => formatter.write_str("a byte array"),
Unsupported::Optional => formatter.write_str("an optional"), Unsupported::Optional => formatter.write_str("an optional"),
#[cfg(any(feature = "std", feature = "alloc"))]
Unsupported::UnitStruct => formatter.write_str("unit struct"),
Unsupported::Sequence => formatter.write_str("a sequence"), Unsupported::Sequence => formatter.write_str("a sequence"),
Unsupported::Tuple => formatter.write_str("a tuple"), Unsupported::Tuple => formatter.write_str("a tuple"),
Unsupported::TupleStruct => formatter.write_str("a tuple struct"), Unsupported::TupleStruct => formatter.write_str("a tuple struct"),
@ -1092,7 +1088,7 @@ where
} }
fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> { fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
Err(Self::bad_type(Unsupported::UnitStruct)) Ok(())
} }
fn serialize_unit_variant( fn serialize_unit_variant(

View File

@ -1815,6 +1815,32 @@ fn test_flatten_unit() {
); );
} }
#[test]
fn test_flatten_unit_struct() {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Response<T> {
#[serde(flatten)]
data: T,
status: usize,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Unit;
assert_tokens(
&Response {
data: Unit,
status: 0,
},
&[
Token::Map { len: None },
Token::Str("status"),
Token::U64(0),
Token::MapEnd,
],
);
}
#[test] #[test]
fn test_flatten_unsupported_type() { fn test_flatten_unsupported_type() {
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]