Derive JsonSchema_repr (#76)
This commit is contained in:
parent
7de2b2276f
commit
11d95b79e5
16 changed files with 225 additions and 18 deletions
14
docs/_includes/examples/enum_repr.rs
Normal file
14
docs/_includes/examples/enum_repr.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
use schemars::{schema_for, JsonSchema_repr};
|
||||
|
||||
#[derive(JsonSchema_repr)]
|
||||
#[repr(u8)]
|
||||
enum SmallPrime {
|
||||
Two = 2,
|
||||
Three = 3,
|
||||
Five = 5,
|
||||
Seven = 7,
|
||||
}
|
||||
fn main() {
|
||||
let schema = schema_for!(SmallPrime);
|
||||
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||
}
|
11
docs/_includes/examples/enum_repr.schema.json
Normal file
11
docs/_includes/examples/enum_repr.schema.json
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "SmallPrime",
|
||||
"type": "integer",
|
||||
"enum": [
|
||||
2,
|
||||
3,
|
||||
5,
|
||||
7
|
||||
]
|
||||
}
|
14
docs/examples/8-enum_repr.md
Normal file
14
docs/examples/8-enum_repr.md
Normal file
|
@ -0,0 +1,14 @@
|
|||
---
|
||||
layout: default
|
||||
title: Serialize enum as number (serde_repr)
|
||||
parent: Examples
|
||||
nav_order: 8
|
||||
summary: >-
|
||||
Generating a schema for with a C-like enum compatible with serde_repr.
|
||||
---
|
||||
|
||||
# Deriving JsonSchema with Fields Using Custom Serialization
|
||||
|
||||
If you use the `#[repr(...)]` attribute on an enum to give it a C-like representation, then you may also want to use the [serde_repr](https://github.com/dtolnay/serde-repr) crate to serialize the enum values as numbers. In this case, you should use the corresponding `JsonSchema_repr` derive to ensure the schema for your type reflects how serde formats your type.
|
||||
|
||||
{% include example.md name="enum_repr" %}
|
Loading…
Add table
Add a link
Reference in a new issue