Class JsonSchemaGenerator
Generates JSON Schema from .NET types using System.Text.Json.
public static class JsonSchemaGenerator
- Inheritance
-
JsonSchemaGenerator
- Inherited Members
Remarks
This utility uses .NET 9's built-in JSON Schema generation via
JsonSchemaExporterOptions and GetJsonSchemaAsNode.
The default options use camelCase property naming for JSON convention compatibility and no indentation for wire efficiency.
Custom schema annotations can be included by setting IncludeAnnotations to true.
Methods
Generate(Type)
Generates a JSON Schema string for the specified type.
public static string Generate(Type type)
Parameters
typeTypeThe type to generate a schema for.
Returns
- string
The JSON Schema as a string.
Exceptions
- ArgumentNullException
typeis null.
Generate(Type, JsonSchemaOptions)
Generates a JSON Schema string for the specified type with custom options.
public static string Generate(Type type, JsonSchemaOptions options)
Parameters
typeTypeThe type to generate a schema for.
optionsJsonSchemaOptionsThe schema generation options.
Returns
- string
The JSON Schema as a string.
Exceptions
- ArgumentNullException
typeoroptionsis null.
Generate(Type, JsonSerializerOptions)
Generates a JSON Schema string for the specified type using custom serializer options.
public static string Generate(Type type, JsonSerializerOptions options)
Parameters
typeTypeThe type to generate a schema for.
optionsJsonSerializerOptionsThe JSON serializer options to use for schema generation.
Returns
- string
The JSON Schema as a string.
Exceptions
- ArgumentNullException
typeoroptionsis null.
Generate(Type, JsonSerializerOptions, JsonSchemaExporterOptions)
Generates a JSON Schema string for the specified type using custom serializer and exporter options.
public static string Generate(Type type, JsonSerializerOptions serializerOptions, JsonSchemaExporterOptions exporterOptions)
Parameters
typeTypeThe type to generate a schema for.
serializerOptionsJsonSerializerOptionsThe JSON serializer options to use.
exporterOptionsJsonSchemaExporterOptionsThe JSON Schema exporter options to use.
Returns
- string
The JSON Schema as a string.
Exceptions
- ArgumentNullException
Any argument is null.
Generate<T>()
Generates a JSON Schema string for the specified type.
public static string Generate<T>()
Returns
- string
The JSON Schema as a string.
Type Parameters
TThe type to generate a schema for.
Generate<T>(JsonSchemaOptions)
Generates a JSON Schema string for the specified type with custom options.
public static string Generate<T>(JsonSchemaOptions options)
Parameters
optionsJsonSchemaOptionsThe schema generation options.
Returns
- string
The JSON Schema as a string.
Type Parameters
TThe type to generate a schema for.
Exceptions
- ArgumentNullException
optionsis null.