macro_rules! bytes_serde_json {
($type_:ty) => { ... };
}Expand description
Implements ToBytes and FromBytes via JSON serialization/deserialization for the given type.
This macro generates:
- a
ToBytesimpl that serializes the type to aVec<u8>usingserde_json::to_vecand wraps it inseda_sdk_rs::Bytes. - a
FromBytesimpl that deserializes from a&[u8]orVec<u8>usingserde_json::from_slice.
§Example
§Example
use serde::{Serialize, Deserialize};
use seda_sdk_rs::bytes::{Bytes, ToBytes, FromBytes};
use seda_sdk_rs::bytes_serde_json;
#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
struct MyType { foo: String, bar: i32 }
// Generate the ToBytes/FromBytes impls
bytes_serde_json!(MyType);
let original = MyType { foo: "hi".into(), bar: 123 };
let bytes = original.clone().to_bytes();
let decoded = MyType::from_bytes(&bytes).unwrap();
assert_eq!(original, decoded);