Add json serde support

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/25/head
trivernis 3 years ago
parent bb7534d333
commit 67f9ae2b6b
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -39,4 +39,10 @@ jobs:
run: cargo test --verbose --all --features serialize_rmp
- name: Run bincode serialization tests
run: cargo test --verbose --all --features serialize_bincode
run: cargo test --verbose --all --features serialize_bincode
- name: Run postcard serialization tests
run: cargo test --verbose --all --features serialize_postcard
- name: Run json serialization tests
run: cargo test --verbose --all --features serialize_json

1
Cargo.lock generated

@ -105,6 +105,7 @@ dependencies = [
"postcard",
"rmp-serde",
"serde",
"serde_json",
"thiserror",
"tokio",
"tracing",

@ -29,6 +29,7 @@ async-trait = "0.1.51"
futures = "0.3.17"
rmp-serde = {version = "0.15.5", optional = true}
bincode = {version = "1.3.3", optional = true}
serde_json = {version = "1.0.72", optional = true}
[dependencies.serde]
optional = true
@ -65,4 +66,5 @@ default = []
serialize = ["serde"]
serialize_rmp = ["serialize", "rmp-serde"]
serialize_bincode = ["serialize", "bincode"]
serialize_postcard = ["serialize", "postcard"]
serialize_postcard = ["serialize", "postcard"]
serialize_json = ["serialize", "serde_json"]

@ -15,3 +15,9 @@ mod serialize_postcard;
#[cfg(feature = "serialize_postcard")]
pub use serialize_postcard::*;
#[cfg(feature = "serialize_json")]
mod serialize_json;
#[cfg(feature = "serialize_json")]
pub use serialize_json::*;

@ -0,0 +1,29 @@
use crate::payload::{EventReceivePayload, EventSendPayload};
use crate::prelude::IPCResult;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::io::Read;
pub type SerializationError = serde_json::Error;
impl<T> EventSendPayload for T
where
T: Serialize,
{
fn to_payload_bytes(self) -> IPCResult<Vec<u8>> {
let bytes = serde_json::to_vec(&self)?;
Ok(bytes)
}
}
impl<T> EventReceivePayload for T
where
T: DeserializeOwned,
{
fn from_payload_bytes<R: Read>(reader: R) -> IPCResult<Self> {
let type_data = serde_json::from_reader(reader)?;
Ok(type_data)
}
}

@ -106,7 +106,8 @@
not(any(
feature = "serialize_bincode",
feature = "serialize_rmp",
feature = "serialize_postcard"
feature = "serialize_postcard",
feature = "serialize_json"
))
))]
compile_error!("Feature 'serialize' cannot be used by its own. Choose one of 'serialize_rmp', 'serialize_bincode', 'serialize_postcard' instead.");
@ -114,15 +115,35 @@ compile_error!("Feature 'serialize' cannot be used by its own. Choose one of 'se
#[cfg(any(
all(
feature = "serialize_rmp",
any(feature = "serialize_postcard", feature = "serialize_bincode")
any(
feature = "serialize_postcard",
feature = "serialize_bincode",
feature = "serialize_json"
)
),
all(
feature = "serialize_bincode",
any(feature = "serialize_rmp", feature = "serialize_postcard")
any(
feature = "serialize_rmp",
feature = "serialize_postcard",
feature = "serialize_json"
)
),
all(
feature = "serialize_postcard",
any(feature = "serialize_rmp", feature = "serialize_bincode")
any(
feature = "serialize_rmp",
feature = "serialize_bincode",
feature = "serialize_json"
)
),
all(
feature = "serialize_json",
any(
feature = "serialize_rmp",
feature = "serialize_bincode",
feature = "serialize_postcard"
)
)
))]
compile_error!("You cannot use two serialize_* features at the same time");

Loading…
Cancel
Save