Skip to content

Commit 68a5582

Browse files
committed
Support deserializing bool in map keys
1 parent 283a68b commit 68a5582

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

src/de.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,41 @@ where
22032203
deserialize_numeric_key!(deserialize_f32, deserialize_f32);
22042204
deserialize_numeric_key!(deserialize_f64);
22052205

2206+
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
2207+
where
2208+
V: de::Visitor<'de>,
2209+
{
2210+
self.de.eat_char();
2211+
2212+
let peek = match tri!(self.de.next_char()) {
2213+
Some(b) => b,
2214+
None => {
2215+
return Err(self.de.peek_error(ErrorCode::EofWhileParsingValue));
2216+
}
2217+
};
2218+
2219+
let value = match peek {
2220+
b't' => {
2221+
tri!(self.de.parse_ident(b"rue\""));
2222+
visitor.visit_bool(true)
2223+
}
2224+
b'f' => {
2225+
tri!(self.de.parse_ident(b"alse\""));
2226+
visitor.visit_bool(false)
2227+
}
2228+
_ => {
2229+
self.de.scratch.clear();
2230+
let s = tri!(self.de.read.parse_str(&mut self.de.scratch));
2231+
Err(de::Error::invalid_type(Unexpected::Str(&s), &visitor))
2232+
}
2233+
};
2234+
2235+
match value {
2236+
Ok(value) => Ok(value),
2237+
Err(err) => Err(self.de.fix_position(err)),
2238+
}
2239+
}
2240+
22062241
#[inline]
22072242
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
22082243
where
@@ -2258,7 +2293,7 @@ where
22582293
}
22592294

22602295
forward_to_deserialize_any! {
2261-
bool char str string unit unit_struct seq tuple tuple_struct map struct
2296+
char str string unit unit_struct seq tuple tuple_struct map struct
22622297
identifier ignored_any
22632298
}
22642299
}

src/value/de.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,22 @@ impl<'de> serde::Deserializer<'de> for MapKeyDeserializer<'de> {
11831183
deserialize_numeric_key!(deserialize_i128, do_deserialize_i128);
11841184
deserialize_numeric_key!(deserialize_u128, do_deserialize_u128);
11851185

1186+
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error>
1187+
where
1188+
V: Visitor<'de>,
1189+
{
1190+
if self.key == "true" {
1191+
visitor.visit_bool(true)
1192+
} else if self.key == "false" {
1193+
visitor.visit_bool(false)
1194+
} else {
1195+
Err(serde::de::Error::invalid_type(
1196+
Unexpected::Str(&self.key),
1197+
&visitor,
1198+
))
1199+
}
1200+
}
1201+
11861202
#[inline]
11871203
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
11881204
where
@@ -1219,8 +1235,8 @@ impl<'de> serde::Deserializer<'de> for MapKeyDeserializer<'de> {
12191235
}
12201236

12211237
forward_to_deserialize_any! {
1222-
bool char str string bytes byte_buf unit unit_struct seq tuple
1223-
tuple_struct map struct identifier ignored_any
1238+
char str string bytes byte_buf unit unit_struct seq tuple tuple_struct
1239+
map struct identifier ignored_any
12241240
}
12251241
}
12261242

0 commit comments

Comments
 (0)