diff --git a/crates/json-rpc/src/packet.rs b/crates/json-rpc/src/packet.rs index e440ecc84ed..89cd8cfd01e 100644 --- a/crates/json-rpc/src/packet.rs +++ b/crates/json-rpc/src/packet.rs @@ -1,7 +1,6 @@ use crate::{Id, Response, SerializedRequest}; use serde::{ de::{self, Deserializer, MapAccess, SeqAccess, Visitor}, - ser::SerializeSeq, Deserialize, Serialize, }; use serde_json::value::RawValue; @@ -35,14 +34,8 @@ impl Serialize for RequestPacket { S: serde::Serializer, { match self { - RequestPacket::Single(single) => single.serialized().serialize(serializer), - RequestPacket::Batch(batch) => { - let mut seq = serializer.serialize_seq(Some(batch.len()))?; - for req in batch { - seq.serialize_element(req.serialized())?; - } - seq.end() - } + RequestPacket::Single(single) => single.serialize(serializer), + RequestPacket::Batch(batch) => batch.serialize(serializer), } } } @@ -54,8 +47,13 @@ impl RequestPacket { } /// Serialize the packet as a boxed [`RawValue`]. - pub fn serialize(&self) -> serde_json::Result> { - serde_json::to_string(self).and_then(RawValue::from_string) + pub fn serialize(self) -> serde_json::Result> { + match self { + RequestPacket::Single(single) => Ok(single.take_request()), + RequestPacket::Batch(batch) => { + serde_json::to_string(&batch).and_then(RawValue::from_string) + } + } } /// Get the request IDs of all subscription requests in the packet. diff --git a/crates/json-rpc/src/request.rs b/crates/json-rpc/src/request.rs index e489c842f78..03d9a92ea48 100644 --- a/crates/json-rpc/src/request.rs +++ b/crates/json-rpc/src/request.rs @@ -206,3 +206,12 @@ impl SerializedRequest { } } } + +impl Serialize for SerializedRequest { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + self.request.serialize(serializer) + } +}