schemars/docs/examples/9-from_value.md
Graham Esau 61d64df57d v0.8.2
2021-03-27 00:04:07 +00:00

835 B

layout title parent nav_order summary
default Generate Schema from Example Value Examples 9 Generating a schema for a serializable value.

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, 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.