Skip to content

Commit

Permalink
Support serializing bool in map keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Aug 15, 2023
1 parent 58dd8d9 commit 283a68b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,21 @@ where
type SerializeStruct = Impossible<(), Error>;
type SerializeStructVariant = Impossible<(), Error>;

fn serialize_bool(self, _value: bool) -> Result<()> {
Err(key_must_be_a_string())
fn serialize_bool(self, value: bool) -> Result<()> {
tri!(self
.ser
.formatter
.begin_string(&mut self.ser.writer)
.map_err(Error::io));
tri!(self
.ser
.formatter
.write_bool(&mut self.ser.writer, value)
.map_err(Error::io));
self.ser
.formatter
.end_string(&mut self.ser.writer)
.map_err(Error::io)
}

fn serialize_i8(self, value: i8) -> Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions src/value/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,8 @@ impl serde::Serializer for MapKeySerializer {
value.serialize(self)
}

fn serialize_bool(self, _value: bool) -> Result<String> {
Err(key_must_be_a_string())
fn serialize_bool(self, value: bool) -> Result<String> {
Ok(value.to_string())
}

fn serialize_i8(self, value: i8) -> Result<String> {
Expand Down

0 comments on commit 283a68b

Please sign in to comment.