Add example for handling custom serialization

This commit is contained in:
Graham Esau 2019-12-28 16:45:00 +00:00
parent 79155cddf5
commit aec4824425
8 changed files with 170 additions and 2 deletions

View file

@ -9,6 +9,6 @@
<summary>Click to see the output JSON schema...</summary> <summary>Click to see the output JSON schema...</summary>
```json ```json
{% include {{ output }} %} {% include {{ output }} -%}
``` ```
</details> </details>

View file

@ -0,0 +1,48 @@
use schemars::{schema_for, JsonSchema};
use serde::{Deserialize, Serialize};
// `int_as_string` and `bool_as_string` use the schema for `String`.
#[derive(Default, Deserialize, Serialize, JsonSchema)]
pub struct MyStruct {
#[serde(default = "eight", with = "as_string")]
#[schemars(with = "String")]
pub int_as_string: i32,
#[serde(default = "eight")]
pub int_normal: i32,
#[serde(default, with = "as_string")]
#[schemars(with = "String")]
pub bool_as_string: bool,
#[serde(default)]
pub bool_normal: bool,
}
fn eight() -> i32 {
8
}
// This module serializes values as strings
mod as_string {
use serde::{de::Error, Deserialize, Deserializer, Serializer};
pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: std::fmt::Display,
S: Serializer,
{
serializer.collect_str(value)
}
pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: std::str::FromStr,
D: Deserializer<'de>,
{
let string = String::deserialize(deserializer)?;
string.parse().map_err(|_| D::Error::custom("Input was not valid"))
}
}
fn main() {
let schema = schema_for!(MyStruct);
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}

View file

@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MyStruct",
"type": "object",
"properties": {
"bool_as_string": {
"default": "false",
"type": "string"
},
"bool_normal": {
"default": false,
"type": "boolean"
},
"int_as_string": {
"default": "8",
"type": "string"
},
"int_normal": {
"default": 8,
"type": "integer",
"format": "int32"
}
}
}

View file

@ -53,3 +53,7 @@ details summary {
display: list-item; display: list-item;
cursor: pointer; cursor: pointer;
} }
details {
margin-bottom: 0.8rem;
}

View file

@ -2,7 +2,7 @@
layout: default layout: default
title: Doc Comments title: Doc Comments
parent: Examples parent: Examples
nav_order: 5 nav_order: 6
summary: Giving schemas a custom title and/or description using doc comments. summary: Giving schemas a custom title and/or description using doc comments.
--- ---

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

View file

@ -0,0 +1,48 @@
use schemars::{schema_for, JsonSchema};
use serde::{Deserialize, Serialize};
// `int_as_string` and `bool_as_string` use the schema for `String`.
#[derive(Default, Deserialize, Serialize, JsonSchema)]
pub struct MyStruct {
#[serde(default = "eight", with = "as_string")]
#[schemars(with = "String")]
pub int_as_string: i32,
#[serde(default = "eight")]
pub int_normal: i32,
#[serde(default, with = "as_string")]
#[schemars(with = "String")]
pub bool_as_string: bool,
#[serde(default)]
pub bool_normal: bool,
}
fn eight() -> i32 {
8
}
// This module serializes values as strings
mod as_string {
use serde::{de::Error, Deserialize, Deserializer, Serializer};
pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: std::fmt::Display,
S: Serializer,
{
serializer.collect_str(value)
}
pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: std::str::FromStr,
D: Deserializer<'de>,
{
let string = String::deserialize(deserializer)?;
string.parse().map_err(|_| D::Error::custom("Input was not valid"))
}
}
fn main() {
let schema = schema_for!(MyStruct);
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}

View file

@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MyStruct",
"type": "object",
"properties": {
"bool_as_string": {
"default": "false",
"type": "string"
},
"bool_normal": {
"default": false,
"type": "boolean"
},
"int_as_string": {
"default": "8",
"type": "string"
},
"int_normal": {
"default": 8,
"type": "integer",
"format": "int32"
}
}
}