Link Search Menu Expand Document

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

use schemars::{schema_for, JsonSchema_repr};

enum SmallPrime {
    Two = 2,
    Three = 3,
    Five = 5,
    Seven = 7,

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

Click to see the output JSON schema...
  "$schema": "",
  "title": "SmallPrime",
  "type": "integer",
  "enum": [