Skip to content

JSON Format

zod-rs uses serde’s default externally-tagged format for enums. Here’s how each variant type maps to JSON.

Variant TypeRustJSON
UnitStatus::Active{"Active": null}
Tuple (single)Message::Text("hi"){"Text": "hi"}
Tuple (multiple)Message::Coords(1, 2){"Coords": [1, 2]}
StructEvent::Click { x: 1, y: 2 }{"Click": {"x": 1, "y": 2}}

Unit variants serialize as {"VariantName": null}:

#[derive(Serialize, Deserialize, ZodSchema)]
enum Status { Active, Inactive }
// JSON: {"Active": null}

Single-value tuple variants unwrap the value:

#[derive(Serialize, Deserialize, ZodSchema)]
enum Msg { Text(String) }
// JSON: {"Text": "hello"}

Multi-value tuple variants use an array:

#[derive(Serialize, Deserialize, ZodSchema)]
enum Msg { Coords(i32, i32) }
// JSON: {"Coords": [10, 20]}

Struct variants use a nested object:

#[derive(Serialize, Deserialize, ZodSchema)]
enum Event { Click { x: i32, y: i32 } }
// JSON: {"Click": {"x": 1, "y": 2}}

Each variant is validated as a union. The value must be a single-key object where the key matches a variant name, and the value matches that variant’s expected format.