bytes_serde_json

Macro bytes_serde_json 

Source
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 ToBytes impl that serializes the type to a Vec<u8> using serde_json::to_vec and wraps it in seda_sdk_rs::Bytes.
  • a FromBytes impl that deserializes from a &[u8] or Vec<u8> using serde_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);