Zaiets.JsonSchema.Net
1.0.0
dotnet add package Zaiets.JsonSchema.Net --version 1.0.0
NuGet\Install-Package Zaiets.JsonSchema.Net -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Zaiets.JsonSchema.Net" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Zaiets.JsonSchema.Net" Version="1.0.0" />
<PackageReference Include="Zaiets.JsonSchema.Net" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Zaiets.JsonSchema.Net --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Zaiets.JsonSchema.Net, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Zaiets.JsonSchema.Net@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Zaiets.JsonSchema.Net&version=1.0.0
#tool nuget:?package=Zaiets.JsonSchema.Net&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Zaiets.JsonSchema.Net
Generate JSON Schema from C# types — reflection-based, zero dependencies, OpenAPI-ready.
Installation
dotnet add package Zaiets.JsonSchema.Net
Quick start
using Zaiets.JsonSchema.Net;
// Generate a schema from any C# type
var schema = JsonSchemaGenerator.Default.Generate<CreateUserRequest>();
Console.WriteLine(schema.ToJson());
Output:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "CreateUserRequest",
"type": "object",
"properties": {
"name": { "type": "string", "minLength": 1, "maxLength": 100 },
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0, "maximum": 150 }
},
"required": ["name", "email"]
}
Annotating your models
using System.ComponentModel.DataAnnotations;
using Zaiets.JsonSchema.Net;
[SchemaId("https://sarmkadan.com/schemas/user.json")]
[SchemaTitle("User")]
[SchemaDescription("A registered user account.")]
public class CreateUserRequest
{
[Required]
[MinLength(1), MaxLength(100)]
[SchemaDescription("Full display name")]
public string Name { get; set; } = "";
[Required]
[EmailAddress]
public string Email { get; set; } = "";
[Range(0, 150)]
public int? Age { get; set; }
[SchemaWriteOnly]
[MinLength(8)]
[SchemaFormat(SchemaFormats.Password)]
public string? Password { get; set; }
[SchemaReadOnly]
public DateTimeOffset CreatedAt { get; set; }
[SchemaDeprecated("Use Tags instead.")]
public string? Category { get; set; }
}
Supported attributes
| Attribute | Behavior |
|---|---|
[Required] |
Adds property to required array |
[MinLength(n)] / [MaxLength(n)] |
Sets minLength / maxLength |
[StringLength(max, MinimumLength = min)] |
Sets minLength / maxLength |
[Range(min, max)] |
Sets minimum / maximum |
[RegularExpression(pattern)] |
Sets pattern |
[EmailAddress] |
Sets format: email |
[Url] |
Sets format: uri |
[Phone] |
Sets format: phone |
[SchemaTitle(text)] |
Sets title |
[SchemaDescription(text)] |
Sets description |
[SchemaFormat(fmt)] |
Overrides format |
[SchemaPattern(regex)] |
Sets pattern |
[SchemaIgnore] |
Excludes property |
[SchemaReadOnly] |
Sets readOnly: true |
[SchemaWriteOnly] |
Sets writeOnly: true |
[SchemaDeprecated(msg?)] |
Sets deprecated: true |
[SchemaExample(values…)] |
Sets examples |
[SchemaDefault(value)] |
Sets default |
[SchemaId(uri)] |
Sets $id on the schema |
Generator options
// Draft 7 (default)
var generator = new JsonSchemaGenerator();
// Draft 2020-12
var generator = new JsonSchemaGenerator(new JsonSchemaGeneratorOptions
{
SchemaVersion = JsonSchemaVersion.Draft202012,
});
// OpenAPI 3.0 (nullable: true style, no $schema header)
var generator = new JsonSchemaGenerator(JsonSchemaGeneratorOptions.ForOpenApi30());
// OpenAPI 3.1
var generator = new JsonSchemaGenerator(JsonSchemaGeneratorOptions.ForOpenApi31());
// Strict: no additional properties
var generator = new JsonSchemaGenerator(JsonSchemaGeneratorOptions.Strict());
// Custom property naming
var generator = new JsonSchemaGenerator(new JsonSchemaGeneratorOptions
{
PropertyNameTransformer = name => name.ToUpperInvariant(),
});
Nullable handling
Nullable reference types and Nullable<T> are fully supported.
public class Product
{
public string Name { get; set; } = ""; // required, non-nullable
public string? Description { get; set; } // nullable reference type
public int? Stock { get; set; } // Nullable<int>
}
Draft 7 output (default — uses anyOf):
{
"description": { "anyOf": [{ "type": "string" }, { "type": "null" }] },
"stock": { "anyOf": [{ "type": "integer", "format": "int32" }, { "type": "null" }] }
}
OpenAPI 3.0 output (UseOpenApiNullable: true):
{
"description": { "type": "string", "nullable": true },
"stock": { "type": "integer", "nullable": true }
}
Fluent builder
Build schemas programmatically without reflection:
var addressSchema = JsonSchemaBuilder.New()
.ObjectType()
.Title("Address")
.Property("street", JsonSchemaBuilder.StringType(b => b.MinLength(1)), required: true)
.Property("city", JsonSchemaBuilder.StringType(b => b.MinLength(1)), required: true)
.Property("zip", JsonSchemaBuilder.StringType(b => b.Pattern(@"^\d{5}$")))
.Property("country", b => b
.StringType()
.Enum("US", "CA", "GB", "DE", "FR"))
.NoAdditionalProperties()
.Build();
Console.WriteLine(addressSchema.ToJson());
Validation
Validate JSON instances against a schema:
var schema = JsonSchemaGenerator.Default.Generate<CreateUserRequest>();
// Validate a JSON string
var result = SchemaValidator.Validate("""{"name":"Alice","email":"alice@example.com"}""", schema);
if (!result.IsValid)
foreach (var error in result.Errors)
Console.WriteLine(error); // [required] /: Required property 'email' is missing.
// Validate an object directly
var user = new CreateUserRequest { Name = "Bob", Email = "bob@example.com" };
var objectResult = SchemaValidator.Validate(user, schema);
Console.WriteLine(objectResult.IsValid); // True
OpenAPI integration
// Minimal API / Swashbuckle style
var generator = new JsonSchemaGenerator(JsonSchemaGeneratorOptions.ForOpenApi30());
var userSchema = generator.Generate<CreateUserRequest>()
.WithoutSchemaKeyword()
.WithExternalDocs("https://sarmkadan.com/docs/user", "User API reference");
// Access the raw JSON for embedding in an OpenAPI document
string json = userSchema.ToJson();
Schema registry (caching)
The generator caches schemas per type automatically:
var generator = new JsonSchemaGenerator();
var schema1 = generator.Generate<MyType>(); // generated
var schema2 = generator.Generate<MyType>(); // returned from cache (same instance)
// Inspect or pre-warm the cache
generator.Registry.Register<MyType>(customSchema);
Console.WriteLine(generator.Registry.Count);
Enum support
public enum OrderStatus { Pending, Processing, Shipped, Delivered, Cancelled }
// Integer enum (default)
var intSchema = JsonSchemaGenerator.Default.Generate<OrderStatus>();
// String enum
var stringSchema = new JsonSchemaGenerator(new JsonSchemaGeneratorOptions
{
UseStringEnums = true
}).Generate<OrderStatus>();
Type mapping
| C# type | JSON type | format |
|---|---|---|
bool |
boolean |
— |
int, short, byte |
integer |
int32 |
long, ulong |
integer |
int64 |
float |
number |
float |
double, decimal |
number |
double |
string |
string |
— |
Guid |
string |
uuid |
Uri |
string |
uri |
DateTime, DateTimeOffset |
string |
date-time |
DateOnly |
string |
date |
TimeOnly |
string |
time |
TimeSpan |
string |
duration |
byte[] |
string |
byte |
T[], List<T>, IEnumerable<T> |
array |
— |
Dictionary<K,V> |
object |
additionalProperties |
| class / struct | object |
— |
| enum | integer or string |
— |
License
MIT — see LICENSE.
Built by Vladyslav Zaiets · CTO & Software Architect
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 134 | 5/3/2026 |