Update docs

This commit is contained in:
Graham Esau 2021-03-25 22:51:51 +00:00
parent cb26e783dc
commit 93435d519a
5 changed files with 23 additions and 5 deletions

View file

@ -1,13 +1,14 @@
---
layout: default
title: Serialize enum as number (serde_repr)
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.
nav_exclude: true # schemars 0.8.2 currently unreleased
---
# Deriving JsonSchema with Fields Using Custom Serialization
# Serialize Enum as Number (serde_repr Compatibility)
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.

View file

@ -0,0 +1,17 @@
---
layout: default
title: Generate Schema from Example Value
parent: Examples
nav_order: 9
summary: >-
Generating a schema for a serializable value.
nav_exclude: true # schemars 0.8.2 currently unreleased
---
# Generate Schema from Example Value
If you want a schema for a type that can't/doesn't implement `JsonSchema`, but does implement [`serde::Serialize`](https://docs.serde.rs/serde/trait.Serialize.html), then you can generate a JSON schema from a value of that type. However, this schema will generally be less precise than if the type implemented `JsonSchema` - particularly when it involves enums, since schemars will not make any assumptions about the structure of an enum based on a single variant.
{% include example.md name="from_value" %}
Note that the schema for the enum is not very useful in this case, since schemars doesn't know anything about the second variant.