MCPcopy Index your code
hub / github.com/evestera/json_typegen

github.com/evestera/json_typegen @v0.7.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.7.0 ↗ · + Follow
175 symbols 380 edges 29 files 17 documented · 10%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

json_typegen - Rust types from JSON samples

Travis Build Status Appveyor build status crates.io docs.rs

json_typegen is a collection of tools for generating types from JSON samples for Rust, Kotlin and TypeScript. I.e. you give it some JSON, and it gives you the type definitions necessary to use that JSON in a program.

There are three interfaces to this code generation logic:

Procedural macro

In Rust the code generation can be used straight from the program you are making, with a procedural macro.

For those familiar with F#, the procedural macro json_typegen! works as a type provider for JSON in Rust. It was inspired by and uses the same kind of inference algorithm as F# Data.

As an example, the below code generates the type Point based on an inline sample.

use json_typegen::json_typegen;

json_typegen!("Point", r#"{ "x": 1, "y": 2 }"#);

fn main() {
    let mut p: Point = serde_json::from_str(r#"{ "x": 3, "y": 5 }"#).unwrap();
    println!("deserialized = {:?}", p);
    p.x = 4;
    let serialized = serde_json::to_string(&p).unwrap();
    println!("serialized = {}", serialized);
}

The following crate dependencies are necessary for this example to work:

[dependencies]
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
json_typegen = "0.7"

The sample json can also come from local or remote files:

json_typegen!("Point", "json_samples/point.json");
json_typegen!("Point", "http://example.com/someapi/point.json");

The code generation can also be customized:

json_typegen!("Point", "http://example.com/someapi/point.json", {
    use_default_for_missing_fields,
    "/foo/bar": {
        use_type: "map"
    }
});

For the details on configuration, see the relevant documentation.

Conditional compilation

To avoid doing a HTTP request per sample used for every build you can use conditional compilation to only check against remote samples when desired:

#[cfg(not(feature = "online-samples"))]
json_typegen!("pub Point", r#"{ "x": 1, "y": 2 }"#);
#[cfg(feature = "online-samples")]
json_typegen!("pub Point", "https://typegen.vestera.as/examples/point.json");

And in Cargo.toml:

[features]
online-samples = []

You can then verify that remote samples match your expectations in e.g. CI builds as follows:

cargo check --features "online-samples"

Command line interface

The crate json_typegen_cli provides a CLI to the same code generation as the procedural macro uses internally. This provides a useful migration path if you at some point need to customize the generated code beyond what is practical through macro arguments.

For details on installation and usage see its readme.

Web interface

For simple testing and one-time use there is also a WebAssembly-powered web interface hosted at https://typegen.vestera.as/. Source code in json_typegen_web.

Creating your own type provider crate

Both procedural macros and the shape inference algorithm are actually very simple. To learn/copy the algorithm you can look at this stripped-down version(< 200 lines).

License

This project is dual licensed, under either the Apache 2.0 or the MIT license, at your option.

Core symbols most depended-on inside this repo

$
called by 45
json_typegen_web/src/util.js
add
called by 15
json_typegen_shared/src/hints.rs
fold_shapes
called by 10
json_typegen_shared/src/shape.rs
string_option
called by 8
json_typegen_shared/src/parse.rs
render
called by 6
json_typegen_web/src/index.js
skip
called by 5
json_typegen_shared/src/parse.rs
codegen
called by 5
json_typegen_shared/src/lib.rs
handle_pub_in_name
called by 5
json_typegen_shared/src/lib.rs

Shape

Function 142
Method 16
Class 10
Enum 7

Languages

Rust95%
TypeScript5%

Modules by API surface

json_typegen_shared/src/parse.rs25 symbols
json_typegen_shared/src/hints.rs17 symbols
json_typegen_shared/src/generation/kotlin.rs16 symbols
json_typegen_shared/src/generation/rust.rs15 symbols
json_typegen_shared/src/generation/typescript.rs11 symbols
json_typegen_shared/tests/with_defaults.rs10 symbols
json_typegen_shared/src/util.rs10 symbols
json_typegen_shared/src/lib.rs9 symbols
json_typegen_shared/src/generation/shape.rs8 symbols
json_typegen_shared/src/generation/json_schema.rs8 symbols
json_typegen_shared/src/shape.rs7 symbols
json_typegen_shared/src/options.rs7 symbols

For agents

$ claude mcp add json_typegen \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact