Skip to content

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

Notifications You must be signed in to change notification settings

abdullah-albanna/schema2struct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

schema2struct: JSON Schema to Rust Struct Generator

A powerful procedural macro for generating type-safe Rust structs from JSON Schema definitions with compile-time validation and Serde integration.

Features

  • Automatic struct generation from JSON Schema

  • Compile-time type checking

  • Support for complex nested schemas

  • Constraint validation

  • Zero-cost abstraction

Installation

Add the following to your Cargo.toml:

[dependencies]

schema2struct = "0.1"

serde = { version = "1.0", features = ["derive"] }

serde_json = "1.0"

Quick Start

Basic Usage

use schema2struct::schema2struct;

schema2struct! {
    struct: User,
    type: object,
    properties: {
        "name": { type: string },
        "age": { type: number, minimum: 0 }
    },
    required: ["name", "age"]
}

Output

pub static USER_JSON_VALUE: ::std::sync::LazyLock<::serde_json::Value> = ::std::sync::LazyLock::new(||
{
    ::serde_json::from_str(
            "{\"type\":\"object\",\"properties\":{\"name\":{\"type\":\"string\"},\"age\":{\"type\":\"number\",\"minimum\":0}},\"required\":[\"name\",\"age\"]}",
        )
        .expect("Couldn't convert the text into valid json")
});

#[derive(Deserialize, Serialize, Clone, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct User {
    #[serde(alias = "age")]
    pub age: f64,
    #[serde(alias = "name")]
    pub name: String,
}

more complex usages can be found in the examples folder

License

Distributed under the MIT License. See LICENSE for more information.

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages