From 39f86154ce4e811994ca65b87caca716bd10c5d1 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 29 Oct 2020 09:48:54 +1100 Subject: [PATCH] Use serde_cbor instead of serde_json We have serde trait implementations that use `serde_cbor` which is a superset of josn. We cannot use `serder_json` to ser/deser these objects. --- swap/src/network/request_response.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/swap/src/network/request_response.rs b/swap/src/network/request_response.rs index 6ae8c00c..4950b818 100644 --- a/swap/src/network/request_response.rs +++ b/swap/src/network/request_response.rs @@ -68,8 +68,9 @@ impl RequestResponseCodec for Codec { let message = upgrade::read_one(io, BUF_SIZE) .await .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; - let mut de = serde_json::Deserializer::from_slice(&message); - let msg = BobToAlice::deserialize(&mut de)?; + let mut de = serde_cbor::Deserializer::from_slice(&message); + let msg = BobToAlice::deserialize(&mut de) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; Ok(msg) } @@ -85,8 +86,9 @@ impl RequestResponseCodec for Codec { let message = upgrade::read_one(io, BUF_SIZE) .await .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; - let mut de = serde_json::Deserializer::from_slice(&message); - let msg = AliceToBob::deserialize(&mut de)?; + let mut de = serde_cbor::Deserializer::from_slice(&message); + let msg = AliceToBob::deserialize(&mut de) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; Ok(msg) } @@ -100,7 +102,8 @@ impl RequestResponseCodec for Codec { where T: AsyncWrite + Unpin + Send, { - let bytes = serde_json::to_vec(&req)?; + let bytes = + serde_cbor::to_vec(&req).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; upgrade::write_one(io, &bytes).await?; Ok(()) @@ -115,7 +118,8 @@ impl RequestResponseCodec for Codec { where T: AsyncWrite + Unpin + Send, { - let bytes = serde_json::to_vec(&res)?; + let bytes = + serde_cbor::to_vec(&res).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; upgrade::write_one(io, &bytes).await?; Ok(())