jtd-derive   GitHub Workflow Status

Generate JSON Type Definition schemas from Rust types.

Status

Sort of usable, but lacking important features like better serde support.

The API is unstable. Expect breaking changes between minor version bumps.

Why?

Because Typedef seems really nice in how minimal and unambiguous it is. In particular, systems that generate JSON-based APIs and related IDL files (with the expectation those will be used for code generation) could use something like this. Feature bloat is arguably not a good idea in those sensitive spots.

This crate hopefully makes it a little nicer in that Rust projects can keep language-agnostic type definitions as Rust code rather than a separate thing with a different syntax.

Alternatives

JSON Schema

JSON Schema is often tauted as the more universally accepted solution. The thing is, it's a solution to a different problem. JSON Schema is meant to be very expressive and good for validating JSON data against complex constraints.

If you expect codegen to be a major need for you but want to provide JSON Schemas as well, consider using Typedef and writing a Typedef -> JSON Schema generator. That way codegen consumers can still benefit from Typedef's simplicity.

OpenAPI

OpenAPI serves a similar purpose, but is complex and meant to describe specifically APIs built on top of HTTP (often called "RESTful APIs", though that's usually quite silly), with its paths and methods and all the doodads. In that way, it already has a way of describing your API's endpoints, whereas if you want to use Typedef, you'll want to embed it in some custom IDL of your design.

If you're building a "web" api, OpenAPI might be worth a look. It seems complex, but maybe it will make sense for your use case.

If you're not building a "web" API and aren't constrained by the HTTP vocabulary, you'll probably get more value out of Typedef.

Types supported by serde, but not by jtd_derive

This may all seem quite restrictive, but keep in mind the point of Typedef isn't to be vastly expressive and capable of describing anything that can be described with the Rust type system. The idea is to encourage APIs that are universal and schemas that are suitable for code generation.

Every bit of expressiveness you're missing here is a breath of relief for your consumers.

License

Dual licensed under MIT and Apache 2.0 at your option, like most Rust project.