Crate jtd_derive
source ·Expand description
This crate provides a framework for generating JSON Typedef
schemas based on Rust types and how they’d be serialized by
serde_json
.
In order to be able to generate a schema for a type, it must implement the
JsonTypedef
trait. Many types from the Rust standard library already do.
To implement JsonTypedef
for your own types, you’ll probably
want to derive it.
Generating a schema is done by creating a Generator
,
calling Generator::into_root_schema
,
and finally serializing the resulting RootSchema
object.
§Example
use jtd_derive::{JsonTypedef, Generator};
#[derive(JsonTypedef)]
struct Foo {
x: u32,
}
let root_schema = Generator::default().into_root_schema::<Foo>().unwrap();
let json_schema = serde_json::to_value(&root_schema).unwrap();
assert_eq!(json_schema, serde_json::json!{ {
"properties": {
"x": { "type": "uint32" }
},
"additionalProperties": true,
} });
Modules§
- The internal Rust representation of a JSON Typedef schema.
Structs§
- A configurable schema generator. An instance is meant to produce one
RootSchema
and be consumed in the process. - How to refer to a given schema. Used mostly for referring to a schema definition using the “ref” form.
Enums§
- Schema generation errors.
Traits§
- Types that have an associated Typedef schema.