#json-schema #schema #schemafy

schemafy_lib

Generates serializeable Rust types from a json schema

3 unstable releases

0.6.0 Oct 6, 2021
0.5.2 May 18, 2021
0.5.1 Jul 12, 2020

#1 in #schemafy

Download history 22095/week @ 2024-12-18 10225/week @ 2024-12-25 18576/week @ 2025-01-01 26162/week @ 2025-01-08 31305/week @ 2025-01-15 39463/week @ 2025-01-22 40530/week @ 2025-01-29 42660/week @ 2025-02-05 38092/week @ 2025-02-12 43748/week @ 2025-02-19 34096/week @ 2025-02-26 34733/week @ 2025-03-05 35425/week @ 2025-03-12 28465/week @ 2025-03-19 26843/week @ 2025-03-26 27786/week @ 2025-04-02

125,230 downloads per month
Used in 73 crates (9 directly)

MIT license

46KB
1K SLoC

This is a Rust crate which can take a json schema (draft 4) and generate Rust types which are serializable with serde. No checking such as min_value are done but instead only the structure of the schema is followed as closely as possible.

As a schema could be arbitrarily complex this crate makes no guarantee that it can generate good types or even any types at all for a given schema but the crate does manage to bootstrap itself which is kind of cool.

Example

Generated types for VS Codes debug server protocol: https://docs.rs/debugserver-types

Usage

Rust code is generated by providing a Schema struct (which can be deserialized from JSON).

A proc macro is available in schemafy crate

extern crate serde;
extern crate schemafy_core;
extern crate serde_json;

use serde::{Serialize, Deserialize};
use schemafy_lib::Expander;

let json = std::fs::read_to_string("src/schema.json").expect("Read schema JSON file");

let schema = serde_json::from_str(&json).unwrap();
let mut expander = Expander::new(
    Some("Schema"),
    "::schemafy_core::",
    &schema,
);

let code = expander.expand(&schema);

Dependencies

~1.2–2.1MB
~41K SLoC