Example Recipes
Runnable examples demonstrating JSON Schema patterns with the V5 code generator.
Simple Data Objects
This recipe demonstrates how to define a simple data object composed of primitive values using JSON Schema, and how the Corvus.Text.Json code generator produces strongly-typed .NET types with rich property accessors, zero-allocation comparisons, and NodaTime integration.
002Data Object Validation
This recipe demonstrates how to validate JSON data against schema constraints using the generated types from Corvus.Text.Json, including fast boolean-only validation and detailed diagnostic output with the `JsonSchemaResultsCollector`.
003Reusing Common Types
This recipe demonstrates how to use JSON Schema's `$ref` and `$defs` keywords to share type definitions across properties, reducing generated code size and enabling zero-allocation conversions between compatible property types.
004Open Versus Closed Types
This recipe demonstrates how to use `unevaluatedProperties` to control whether a JSON Schema object type is open (extensible) or closed (strict), and how that affects validation and mutation in generated .NET code.
005Extending a Base Type
This recipe demonstrates how to use `$ref` to extend an open base type with additional properties, creating a derived schema that includes all base properties plus its own — similar to inheritance in object-oriented languages.
006Constraining a Base Type
This recipe demonstrates how to use `$ref` to base a new schema on an existing type and add tighter constraints to its properties, showing how JSON Schema composes constraints from both base and derived schemas.
007Strongly Typed Arrays
This recipe demonstrates how to define strongly-typed JSON arrays in .NET using JSON Schema's `items`, `minItems`, and `maxItems` keywords, giving you compile-time type safety, IntelliSense support, and standard LINQ operators for array elements.
008Multi-Dimensional Arrays
This recipe demonstrates how to define multi-dimensional (higher-rank) JSON arrays in .NET by nesting array schemas, giving you strongly-typed indexers and tensor operations for fixed-size numeric arrays.
009Working with Tensors
This recipe demonstrates how to construct and manipulate fixed-size numeric arrays as tensors in .NET, including span-based construction, `TryGetNumericValues()` extraction, and integration with APIs like `System.Numerics.Tensors`.
010Creating Tuples
This recipe demonstrates how to use JSON Schema `prefixItems` and `unevaluatedItems` to create strongly-typed tuple representations in .NET.
011Interfaces and Mix-In Types
This recipe demonstrates how to compose multiple JSON Schema definitions using `allOf`, creating types that behave like .NET interfaces or mix-ins.
012Pattern Matching and Discriminated Unions
This recipe demonstrates how to use JSON Schema `oneOf` to create discriminated unions that support pattern matching in .NET.
013Polymorphism with Discriminator Properties
This recipe demonstrates how to use JSON Schema `oneOf` with `const` properties to create polymorphic types with discriminators - a pattern similar to OpenAPI's polymorphism feature and `System.Text.Json`'s polymorphic serialization.
014String Enumerations and Pattern Matching
This recipe demonstrates how to use JSON Schema `enum` keyword with string values to create type-safe enumerations with pattern matching support.
015Numeric Enumerations
This recipe demonstrates how to use JSON Schema `enum` keyword with numeric values to create type-safe numeric enumerations.
016Maps of Strings to Strongly Typed Values
This recipe demonstrates how to use JSON Schema `additionalProperties` to create strongly-typed map/dictionary structures with string keys and typed values.
017Mapping Input and Output Values
This recipe demonstrates how to efficiently convert between different schema representations of similar entities - a common pattern in layered architectures where data transforms between API, domain, and persistence layers.
018Creating and Mutating Objects
This recipe demonstrates the full mutable document lifecycle using the V5 API: parsing an immutable document, creating a mutable builder, reading and modifying properties, mutating nested arrays, and serializing the result.
019Clone, Freeze, and Builder Snapshots
Clone, Freeze, and Builder Snapshots
020Conditional Schemas
This recipe demonstrates how to use JSON Schema's `if`/`then`/`else` keywords for conditional validation, where the required properties change based on the value of another property.
021Default Values
This recipe demonstrates how JSON Schema `default` annotations work with generated types in Corvus.Text.Json, and how the generated property getters automatically apply schema-declared defaults when properties are missing from the JSON document.
022Format Validation
This recipe demonstrates how JSON Schema `format` keywords map to strongly-typed .NET values, and how format-specific validation catches semantic errors in string properties.
023JSON Patch, Merge Patch, and Diff
This recipe demonstrates [RFC 6902 JSON Patch](https://datatracker.ietf.org/doc/html/rfc6902), [RFC 7396 JSON Merge Patch](https://datatracker.ietf.org/doc/html/rfc7396), and JSON Diff using the `Corvus.Text.Json.Patch` library.
024JsonLogic Rule Evaluation
This recipe demonstrates how to evaluate [JsonLogic](https://jsonlogic.com/) rules against JSON data using the `Corvus.Text.Json.JsonLogic` library.
025JSONata Expression Evaluation
This recipe demonstrates how to evaluate [JSONata](https://jsonata.org/) expressions against JSON data using the `Corvus.Text.Json.Jsonata` library.
026JMESPath Query Evaluation
This recipe demonstrates how to evaluate [JMESPath](https://jmespath.org/) expressions against JSON data using the `Corvus.Text.Json.JMESPath` library.
027YAML Conversion
This recipe demonstrates how to convert between YAML and JSON using the `Corvus.Text.Json.Yaml` library.
028JSON Canonicalization (RFC 8785)
This recipe demonstrates how to use [RFC 8785 JSON Canonicalization Scheme (JCS)](https://datatracker.ietf.org/doc/html/rfc8785) with the `Corvus.Text.Json` library.
029Json Path
This recipe demonstrates how to query JSON data using [JSONPath (RFC 9535)](https://www.rfc-editor.org/rfc/rfc9535) expressions with the `Corvus.Text.Json.JsonPath` library.