Skip to content

Commit

Permalink
fix: serialize entire RequestPacket
Browse files Browse the repository at this point in the history
closes #15
  • Loading branch information
gakonst committed Nov 1, 2023
1 parent 2761995 commit bcab0ef
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 26 deletions.
2 changes: 1 addition & 1 deletion crates/json-rpc/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Serialize for RequestPacket {
S: serde::Serializer,
{
match self {
RequestPacket::Single(single) => single.payload().serialize(serializer),
RequestPacket::Single(single) => single.request().serialize(serializer),
RequestPacket::Batch(batch) => {
let mut seq = serializer.serialize_seq(Some(batch.len()))?;
for req in batch {
Expand Down
26 changes: 1 addition & 25 deletions crates/json-rpc/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where

/// Serialize the request, including the request parameters.
pub fn serialize(self) -> serde_json::Result<SerializedRequest> {
let request = serde_json::to_string(&self.params)?;
let request = serde_json::to_string(&self)?;
Ok(SerializedRequest {
meta: self.meta,
request: RawValue::from_string(request)?,
Expand Down Expand Up @@ -153,28 +153,4 @@ impl SerializedRequest {
pub fn request(&self) -> &RawValue {
&self.request
}

pub fn payload(&self) -> Payload {
let params = if self.request.get() == "null" {
None
} else {
Some(self.request())
};
Payload {
id: self.id().clone(),
method: self.method(),
jsonrpc: "2.0",
params,
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Payload<'a> {
id: Id,
method: &'static str,
jsonrpc: &'static str,
#[serde(borrow)]
#[serde(skip_serializing_if = "Option::is_none")]
params: Option<&'a RawValue>,
}

0 comments on commit bcab0ef

Please sign in to comment.