Crates.io | schema2struct |
lib.rs | schema2struct |
version | 0.1.0 |
source | src |
created_at | 2025-01-20 20:33:03.700914+00 |
updated_at | 2025-01-20 20:33:03.700914+00 |
description | Convert a JSON schema into Rust structs for efficient and type-safe data management. |
homepage | https://github.com/abdullah-albanna/schema2struct |
repository | https://github.com/abdullah-albanna/schema2struct |
max_upload_size | |
id | 1524260 |
size | 60,294 |
A powerful procedural macro for generating type-safe Rust structs from JSON Schema definitions with compile-time validation and Serde integration.
Automatic struct generation from JSON Schema
Compile-time type checking
Support for complex nested schemas
Constraint validation
Zero-cost abstraction
Add the following to your Cargo.toml
:
[dependencies]
schema2struct = "0.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
use schema2struct::schema2struct;
schema2struct! {
struct: User,
type: object,
properties: {
"name": { type: string },
"age": { type: number, minimum: 0 }
},
required: ["name", "age"]
}
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
Distributed under the MIT License. See LICENSE for more information.