Add example for handling custom serialization
This commit is contained in:
parent
79155cddf5
commit
aec4824425
8 changed files with 170 additions and 2 deletions
|
@ -2,7 +2,7 @@
|
|||
layout: default
|
||||
title: Doc Comments
|
||||
parent: Examples
|
||||
nav_order: 5
|
||||
nav_order: 6
|
||||
summary: Giving schemas a custom title and/or description using doc comments.
|
||||
---
|
||||
|
||||
|
|
20
docs/examples/7-custom_serialization.md
Normal file
20
docs/examples/7-custom_serialization.md
Normal file
|
@ -0,0 +1,20 @@
|
|||
---
|
||||
layout: default
|
||||
title: Custom Serialization
|
||||
parent: Examples
|
||||
nav_order: 7
|
||||
summary: >-
|
||||
If a field has a #[serde(with = "path")] attribute where "path" is not a type that implements JsonSchema,
|
||||
then in order to derive JsonSchema on the type, it must also have a #[schemars(with = "Type")] attribute,
|
||||
where "Type" implements JsonSchema.
|
||||
---
|
||||
|
||||
# Deriving JsonSchema with Fields Using Custom Serialization
|
||||
|
||||
Serde allows you to change how a field is (de)serialized by setting a [`#[serde(with = "path")]`](https://serde.rs/field-attrs.html#with) attribute, where `$path::serialize` and `$path::deserialize` must be functions with the correct signature. Schemars supports the same attribute, but `path` must be a type implementing `JsonSchema`.
|
||||
|
||||
In order to derive `JsonSchema` on a type which includes a `#[serde(with = "path")]` attribute where `path` is not a type implementing `JsonSchema`, you'll need to override it with a suitable `#[schemars(with = "Type")]` attribute.
|
||||
|
||||
{% include example.md name="custom_serialization" %}
|
||||
|
||||
Note that the `default` values in the schema are serialized as strings where appropriate.
|
Loading…
Add table
Add a link
Reference in a new issue