Expand description
serde-prefix-all
§Serde Prefix All
A small extension to serde that will allow you to use the macro #[prefix_all("myprefix_")
. The
macro will prefix each field in a struct or variant in an enum in the serialized format, with the
prefix of your choice.
Behind the doors it’s using #[serde(rename = "...")]
to rename each field/variant with the prefix
defined in prefix_all.
§Usage
use serde::{Serialize, Deserialize};
use serde_prefix_all::prefix_all;
#[prefix_all("test_")]
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Point {
x: i32,
y: i32,
}
let point = Point { x: 1, y: 2 };
let serialized = serde_json::to_string(&point).unwrap();
let json = r#"{"test_x":1,"test_y":2}"#;
assert_eq!(serialized, json);
let deserialized: Point = serde_json::from_str(json).unwrap();
assert_eq!(point, deserialized);
§Background
This is a fork of serde-prefix. The
serde-prefix
crate was unmaintained for years and this fork is a continuation of the work started
by jonathan-s.
Attribute Macros§
- prefix_
all - Prefix all fields of a struct or enum with a given prefix.