Link Search Menu Expand Document

Deriving JsonSchema with Fields Using Custom Serialization

Serde allows you to change how a field is (de)serialized by setting a #[serde(with = "path")] 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")] or #[schemars(schema_with = "path")] attribute.

use schemars::schema::{Schema, SchemaObject};
use schemars::{gen::SchemaGenerator, 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(schema_with = "make_custom_schema")]
    pub bool_as_string: bool,

    pub bool_normal: bool,

fn make_custom_schema(gen: &mut SchemaGenerator) -> Schema {
    let mut schema: SchemaObject = <String>::json_schema(gen).into();
    schema.format = Some("boolean".to_owned());

fn eight() -> i32 {

// 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>
        T: std::fmt::Display,
        S: Serializer,

    pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
        T: std::str::FromStr,
        D: Deserializer<'de>,
        let string = String::deserialize(deserializer)?;
            .map_err(|_| D::Error::custom("Input was not valid"))

fn main() {
    let schema = schema_for!(MyStruct);
    println!("{}", serde_json::to_string_pretty(&schema).unwrap());

Click to see the output JSON schema...
  "$schema": "",
  "title": "MyStruct",
  "type": "object",
  "properties": {
    "bool_as_string": {
      "default": "false",
      "type": "string",
      "format": "boolean"
    "bool_normal": {
      "default": false,
      "type": "boolean"
    "int_as_string": {
      "default": "8",
      "type": "string"
    "int_normal": {
      "default": 8,
      "type": "integer",
      "format": "int32"

Note that the default values in the schema are serialized as strings where appropriate.