#[derive(JsonSchema)]
Expand description
Derives a JSON Schema representation for a struct.
This procedural macro generates a json_schema()
method for the annotated struct, returning a
serde_json::Map<String, serde_json::Value>
that represents the struct as a JSON Schema object.
The schema includes the struct’s fields as properties, with support for basic types, Option<T>
,
Vec<T>
, and nested structs that also derive JsonSchema
.
§Features
- Basic Types: Maps
String
to"string"
,i32
to"integer"
,bool
to"boolean"
, etc. Option<T>
: Adds"nullable": true
to the schema of the inner type, indicating the field is optional.Vec<T>
: Generates an"array"
schema with an"items"
field describing the inner type.- Nested Structs: Recursively includes the schema of nested structs (assumed to derive
JsonSchema
), embedding their"properties"
and"required"
fields. - Required Fields: Adds a top-level
"required"
array listing field names not wrapped inOption
.
§Notes
It’s designed as a straightforward solution to meet the basic needs of this package, supporting
common types and simple nested structures. For more advanced features or robust JSON Schema generation,
consider exploring established crates like
schemars
on crates.io
§Limitations
- Supports only structs with named fields (e.g.,
struct S { field: Type }
). - Nested structs must also derive
JsonSchema
, or compilation will fail. - Unknown types are mapped to
{"type": "unknown"}
. - Type paths must be in scope (e.g., fully qualified paths like
my_mod::InnerStruct
work if imported).
§Panics
- If the input is not a struct with named fields (e.g., tuple structs or enums).
§Dependencies
Relies on serde_json
for Map
and Value
types.