#request #envelope #serde #data #field #name #tagged

serde-request-envelope

A serde request envelope with named type and data fields

3 releases

new 0.1.2 Dec 17, 2024
0.1.1 Dec 14, 2024
0.1.0 Dec 14, 2024

#710 in Data structures

Download history 190/week @ 2024-12-09

190 downloads per month

MIT license

12KB
291 lines

Serde Request Envelope

This crate provides the Request struct, which is a newtype wrapper that takes any given serde friendly type and turns it into a request envelope that includes the type name of the given type. This lets you do tagged structures without having to manually curate enums.

For example:

use serde::{Deserialize, Serialize};
use serde_request_envelope::Request;

#[derive(Serialize, Deserialize, Debug)]
struct MyStruct {
   field: String,
}

let my_struct = MyStruct {
   field: "Hello, World!".to_string(),
};

let request = Request::new(my_struct);

let serialized = serde_json::to_string(&request).unwrap();
// serialized is now: {"type":"MyStruct","data":{"field":"Hello, World!"}}

let deserialized: Request<MyStruct> = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized.0.field, "Hello, World!");

Dependencies

~100–330KB