#macro-attributes #struct-fields #enums #serde

macro serde-prefix-all

Attribute macro to add a prefix to all struct fields & enum variants

1 unstable release

0.1.0 Jun 29, 2025

#2999 in Procedural macros

Download history 14/week @ 2025-08-29 14/week @ 2025-09-05 20/week @ 2025-09-12 111/week @ 2025-09-19 257/week @ 2025-09-26 279/week @ 2025-10-03 399/week @ 2025-10-10 335/week @ 2025-10-17 463/week @ 2025-10-24 661/week @ 2025-10-31 587/week @ 2025-11-07 718/week @ 2025-11-14 331/week @ 2025-11-21 417/week @ 2025-11-28 593/week @ 2025-12-05 120/week @ 2025-12-12

1,590 downloads per month
Used in 3 crates

MIT license

9KB
139 lines

Build Status API Documentation crates.io

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.

Dependencies

~150–560KB
~13K SLoC