Coder Social home page Coder Social logo

zeta1999 / json_typegen Goto Github PK

View Code? Open in Web Editor NEW

This project forked from evestera/json_typegen

0.0 0.0 0.0 392 KB

Tools and libraries to create Rust structs from JSON samples

Home Page: https://typegen.vestera.as

License: Apache License 2.0

Rust 90.50% HTML 6.59% Shell 1.17% JavaScript 1.74%

json_typegen's Introduction

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 Rust types (++) from JSON samples, built on top of serde. I.e. you give it some JSON, and it gives you the type definitions necessary to use that JSON in a Rust program. If you are 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.

There are three interfaces to this code generation logic:

The CLI and web interface also produces types for other languages than Rust. Namely Kotlin, TypeScript and JSON Schemas.

Procedural macro

The first interface to the code generation tools is a procedural macro json_typegen!. As an example, the below code generates code for the type Point.

use json_typegen::json_typegen;

json_typegen!("pub 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);
}
[dependencies]
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
json_typegen = "0.4"

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

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

The code generation can also be customized:

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

For the details, see the relevant documentation.

Conditional compilation

To avoid incurring the cost of 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 usage see its readme.

Web interface

For simple testing and one-time use there is also a web interface (in json_typegen_web). An instance of this interface is currently hosted at https://typegen.vestera.as/

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.

json_typegen's People

Contributors

evestera avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.