#struct #json #convert-json #codegen #macro #derive-debug #debugging

macro json_to_struct

Convert JSON into Rust structs for efficient and type-safe data management

1 unstable release

0.1.0 Jan 20, 2025

#1585 in Data structures

Download history 89/week @ 2025-01-17 6/week @ 2025-01-24 6/week @ 2025-01-31 2/week @ 2025-02-07

103 downloads per month

MIT license

26KB
333 lines

json2struct: Compile-Time Struct Generation

A procedural macro for generating Rust structs from JSON-like structures with extensive compile-time type safety and configuration options.


Features

  • Automatic Struct Generation: Create Rust structs from JSON-like syntax.
  • Flexible Type Inference: Automatically infer types for fields.
  • Serde Integration: Easily serialize and deserialize your structs.
  • Compile-Time Type Checking: Catch errors during compilation.
  • Configurable with Flags: Customize struct generation with powerful flags like @debug, @snake, @derive.

Installation

Add this crate as a dependency in your Cargo.toml:

[dependencies]
json_to_struct = "0.1"

Basic Usage

Generate a simple struct from JSON-like syntax:

use json_to_struct::json2struct;

json2struct!(User {
    "first_name" => "John",
    "last_name" => "Doe",
    "age" => 30
});

Output


#[derive(Clone, Deserialize, Serialize)]
struct User {
    #[serde(alias = "first_name")]
    first_name: String,

    #[serde(alias = "last_name")]
    last_name: String,

    #[serde(alias = "age")]
    age: f64,
}

Advanced Usage

Customize your structs with flags:

json2struct!(Company @debug @camel @derive(PartialEq) @store_json {
    "company_name" => "Acme Corp",
    "employees" => [
        {
            "id" => 1,
            "details" => {
                "email" => "john@example.com",
                "department" => "Engineering"
            }
        }
    ]
});

Output

This example generates nested structs, debug derives, and a static JSON value

static COMPANY_JSON_VALUE: LazyLock<Value> = LazyLock::new(|| {
    serde_json::from_str(
        "{\"company_name\":\"Acme Corp\",\"employees\":[{\"details\":{\"department\":\"Engineering\",\"email\":\"john@example.com\"},\"id\":1.0}]}"
    ).expect("Couldn't convert the text into valid json")
});

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
struct Company {
    #[serde(alias = "company_name")]
    company_name: String,


    #[serde(alias = "employees")]
    employees: Vec<Employee>,
}

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
struct Employee {

    #[serde(alias = "id")]
    id: f64,

    #[serde(alias = "details")]
    details: Details,
}

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
struct Details {

    #[serde(alias = "email")]
    email: String,

    #[serde(alias = "department")]
    department: String,
}

Supported Flags

Flag Description Example
@debug Adds Debug derive @debug
@snake Renames fields to snake_case @snake
@camel Renames fields to camelCase @camel
@pascal Renames fields to PascalCase @pascal
@derive(Type) Adds custom derives @derive(PartialEq, Clone)
@store_json Generates a static JSON value constant @store_json

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

License

This project is licensed under the MIT License.

Dependencies

~0.7–1.6MB
~34K SLoC