Update docs
This commit is contained in:
parent
cb26e783dc
commit
93435d519a
5 changed files with 23 additions and 5 deletions
|
@ -200,7 +200,7 @@ println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||||
|
|
||||||
`#[serde(...)]` attributes can be overriden using `#[schemars(...)]` attributes, which behave identically (e.g. `#[schemars(rename_all = "camelCase")]`). You may find this useful if you want to change the generated schema without affecting Serde's behaviour, or if you're just not using Serde.
|
`#[serde(...)]` attributes can be overriden using `#[schemars(...)]` attributes, which behave identically (e.g. `#[schemars(rename_all = "camelCase")]`). You may find this useful if you want to change the generated schema without affecting Serde's behaviour, or if you're just not using Serde.
|
||||||
|
|
||||||
### Schema from Example Values
|
### Schema from Example Value
|
||||||
|
|
||||||
If you want a schema for a type that can't/doesn't implement `JsonSchema`, but does implement `serde::Serialize`, 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.
|
If you want a schema for a type that can't/doesn't implement `JsonSchema`, but does implement `serde::Serialize`, 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.
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ If you want more control over how the schema is generated, you can use the [`gen
|
||||||
See the API documentation for more info on how to use those types for custom schema generation.
|
See the API documentation for more info on how to use those types for custom schema generation.
|
||||||
|
|
||||||
<!-- UNCOMMMENT FOR 0.8.2:
|
<!-- UNCOMMMENT FOR 0.8.2:
|
||||||
## Schema from Example Values
|
## Schema from Example Value
|
||||||
|
|
||||||
If you want a schema for a type that can't/doesn't implement `JsonSchema`, but does implement `serde::Serialize`, then you can generate a JSON schema from a value of that type using the [`schema_for_value!` macro](https://docs.rs/schemars/latest/schemars/macro.schema_for_value.html). 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.
|
If you want a schema for a type that can't/doesn't implement `JsonSchema`, but does implement `serde::Serialize`, then you can generate a JSON schema from a value of that type using the [`schema_for_value!` macro](https://docs.rs/schemars/latest/schemars/macro.schema_for_value.html). 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.
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
---
|
---
|
||||||
layout: default
|
layout: default
|
||||||
title: Serialize enum as number (serde_repr)
|
title: Serialize Enum as Number (serde_repr)
|
||||||
parent: Examples
|
parent: Examples
|
||||||
nav_order: 8
|
nav_order: 8
|
||||||
summary: >-
|
summary: >-
|
||||||
Generating a schema for with a C-like enum compatible with serde_repr.
|
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.
|
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.
|
||||||
|
|
||||||
|
|
17
docs/examples/9-from_value.md
Normal file
17
docs/examples/9-from_value.md
Normal 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.
|
|
@ -195,7 +195,7 @@ println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||||
|
|
||||||
`#[serde(...)]` attributes can be overriden using `#[schemars(...)]` attributes, which behave identically (e.g. `#[schemars(rename_all = "camelCase")]`). You may find this useful if you want to change the generated schema without affecting Serde's behaviour, or if you're just not using Serde.
|
`#[serde(...)]` attributes can be overriden using `#[schemars(...)]` attributes, which behave identically (e.g. `#[schemars(rename_all = "camelCase")]`). You may find this useful if you want to change the generated schema without affecting Serde's behaviour, or if you're just not using Serde.
|
||||||
|
|
||||||
### Schema from Example Values
|
### Schema from Example Value
|
||||||
|
|
||||||
If you want a schema for a type that can't/doesn't implement `JsonSchema`, but does implement `serde::Serialize`, 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.
|
If you want a schema for a type that can't/doesn't implement `JsonSchema`, but does implement `serde::Serialize`, 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.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue