From 55a7f5c35281feb75f27778200f2a8bca01680b3 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 11 Jul 2023 15:53:48 -0700 Subject: [PATCH 1/7] Inline Serializer::serialize_seq into serialize_bytes --- src/ser.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/ser.rs b/src/ser.rs index e15db92bf..62f5c4f01 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -187,10 +187,27 @@ where format_escaped_str(&mut self.writer, &mut self.formatter, value).map_err(Error::io) } - #[inline] fn serialize_bytes(self, value: &[u8]) -> Result<()> { use serde::ser::SerializeSeq; - let mut seq = tri!(self.serialize_seq(Some(value.len()))); + tri!(self + .formatter + .begin_array(&mut self.writer) + .map_err(Error::io)); + let mut seq = if value.is_empty() { + tri!(self + .formatter + .end_array(&mut self.writer) + .map_err(Error::io)); + Compound::Map { + ser: self, + state: State::Empty, + } + } else { + Compound::Map { + ser: self, + state: State::First, + } + }; for byte in value { tri!(seq.serialize_element(byte)); } From 1b72f2b34bccd40abef5f283034540b4cb460856 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 11 Jul 2023 15:59:44 -0700 Subject: [PATCH 2/7] Inline SerializeSeq::serialize_element into serialize_bytes --- src/ser.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/ser.rs b/src/ser.rs index 62f5c4f01..5d91fa8f6 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -193,7 +193,7 @@ where .formatter .begin_array(&mut self.writer) .map_err(Error::io)); - let mut seq = if value.is_empty() { + let seq = if value.is_empty() { tri!(self .formatter .end_array(&mut self.writer) @@ -203,14 +203,21 @@ where state: State::Empty, } } else { - Compound::Map { - ser: self, - state: State::First, + let mut state = State::First; + for byte in value { + tri!(self + .formatter + .begin_array_value(&mut self.writer, state == State::First) + .map_err(Error::io)); + state = State::Rest; + tri!(byte.serialize(&mut *self)); + tri!(self + .formatter + .end_array_value(&mut self.writer) + .map_err(Error::io)); } + Compound::Map { ser: self, state } }; - for byte in value { - tri!(seq.serialize_element(byte)); - } seq.end() } From 0e2c949638571990059c9783cf6f06f0d77126b2 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 11 Jul 2023 16:01:27 -0700 Subject: [PATCH 3/7] Inline SerializeSeq::end into serialize_bytes --- src/ser.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/ser.rs b/src/ser.rs index 5d91fa8f6..0f422d182 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -188,20 +188,15 @@ where } fn serialize_bytes(self, value: &[u8]) -> Result<()> { - use serde::ser::SerializeSeq; tri!(self .formatter .begin_array(&mut self.writer) .map_err(Error::io)); - let seq = if value.is_empty() { + if value.is_empty() { tri!(self .formatter .end_array(&mut self.writer) .map_err(Error::io)); - Compound::Map { - ser: self, - state: State::Empty, - } } else { let mut state = State::First; for byte in value { @@ -216,9 +211,12 @@ where .end_array_value(&mut self.writer) .map_err(Error::io)); } - Compound::Map { ser: self, state } - }; - seq.end() + tri!(self + .formatter + .end_array(&mut self.writer) + .map_err(Error::io)); + } + Ok(()) } #[inline] From 44b4a6c8599d8988c82e3932a7a1cf3a38f377c3 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 11 Jul 2023 16:03:21 -0700 Subject: [PATCH 4/7] Simplify serialize_bytes --- src/ser.rs | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/ser.rs b/src/ser.rs index 0f422d182..bcf11d4ac 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -192,31 +192,22 @@ where .formatter .begin_array(&mut self.writer) .map_err(Error::io)); - if value.is_empty() { + let mut first = true; + for byte in value { tri!(self .formatter - .end_array(&mut self.writer) + .begin_array_value(&mut self.writer, first) .map_err(Error::io)); - } else { - let mut state = State::First; - for byte in value { - tri!(self - .formatter - .begin_array_value(&mut self.writer, state == State::First) - .map_err(Error::io)); - state = State::Rest; - tri!(byte.serialize(&mut *self)); - tri!(self - .formatter - .end_array_value(&mut self.writer) - .map_err(Error::io)); - } + tri!(byte.serialize(&mut *self)); tri!(self .formatter - .end_array(&mut self.writer) + .end_array_value(&mut self.writer) .map_err(Error::io)); + first = false; } - Ok(()) + self.formatter + .end_array(&mut self.writer) + .map_err(Error::io) } #[inline] From 6ad54959060c8b87b4f7459e203d4750dcb1fd94 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 11 Jul 2023 16:05:05 -0700 Subject: [PATCH 5/7] Inline u8::serialize into serialize_bytes --- src/ser.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ser.rs b/src/ser.rs index bcf11d4ac..dbdec8543 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -198,7 +198,7 @@ where .formatter .begin_array_value(&mut self.writer, first) .map_err(Error::io)); - tri!(byte.serialize(&mut *self)); + tri!(self.serialize_u8(*byte)); tri!(self .formatter .end_array_value(&mut self.writer) From 857b010ce0dd98ba37e9c7c773b72940b7605008 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 11 Jul 2023 16:05:37 -0700 Subject: [PATCH 6/7] Inline Serializer::serialize_u8 into serialize_bytes --- src/ser.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ser.rs b/src/ser.rs index dbdec8543..7657db9ca 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -198,7 +198,10 @@ where .formatter .begin_array_value(&mut self.writer, first) .map_err(Error::io)); - tri!(self.serialize_u8(*byte)); + tri!(self + .formatter + .write_u8(&mut self.writer, *byte) + .map_err(Error::io)); tri!(self .formatter .end_array_value(&mut self.writer) From a1ca32a5c759066b3d8a84267078869ce74df773 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 11 Jul 2023 16:11:25 -0700 Subject: [PATCH 7/7] Factor out byte array serialization to a new Formatter method --- src/ser.rs | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/ser.rs b/src/ser.rs index 7657db9ca..6bb6fd761 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -187,29 +187,10 @@ where format_escaped_str(&mut self.writer, &mut self.formatter, value).map_err(Error::io) } + #[inline] fn serialize_bytes(self, value: &[u8]) -> Result<()> { - tri!(self - .formatter - .begin_array(&mut self.writer) - .map_err(Error::io)); - let mut first = true; - for byte in value { - tri!(self - .formatter - .begin_array_value(&mut self.writer, first) - .map_err(Error::io)); - tri!(self - .formatter - .write_u8(&mut self.writer, *byte) - .map_err(Error::io)); - tri!(self - .formatter - .end_array_value(&mut self.writer) - .map_err(Error::io)); - first = false; - } self.formatter - .end_array(&mut self.writer) + .write_byte_array(&mut self.writer, value) .map_err(Error::io) } @@ -1786,6 +1767,24 @@ pub trait Formatter { writer.write_all(s) } + /// Writes the representation of a byte array. Formatters can choose whether + /// to represent bytes as a JSON array of integers (the default), or some + /// JSON string encoding like hex or base64. + fn write_byte_array(&mut self, writer: &mut W, value: &[u8]) -> io::Result<()> + where + W: ?Sized + io::Write, + { + tri!(self.begin_array(writer)); + let mut first = true; + for byte in value { + tri!(self.begin_array_value(writer, first)); + tri!(self.write_u8(writer, *byte)); + tri!(self.end_array_value(writer)); + first = false; + } + self.end_array(writer) + } + /// Called before every array. Writes a `[` to the specified /// writer. #[inline]