A .NET tool that generates C# code from JSON schemas, with support for detecting shared and variant-specific types across multiple schema files.
- JSON Schema to C# code generation using NJsonSchema
- Multi-schema analysis to detect shared vs variant-specific types
- Three generation modes:
All- Generates shared types + variant-specific types for all variantsShared- Generates only types that exist in all provided schemasVariant- Generates only types unique to a specific variant
- Automatic interface generation for variant types
- Roslyn-based code formatting for clean, consistent output
- .NET 9.0 SDK or later
dotnet tool install --global DotSchemadotnet new tool-manifest # if you don't have one already
dotnet tool install DotSchemadotnet tool restore
dotnet build# If installed as a tool
dotschema [options]
# If running from source
dotnet run -- [options]| Option | Short | Required | Description |
|---|---|---|---|
--schemas |
-s |
Yes | One or more JSON schema files to process |
--output |
-o |
Yes | Output file path (Shared/Variant) or directory (All mode) |
--namespace |
-n |
Yes | Namespace for generated types |
--mode |
-m |
No | Generation mode: All, Shared, or Variant (default: All) |
--variant |
-v |
No | Variant name for single-variant generation |
--no-interface |
No | Skip generating the marker interface | |
--verbose |
No | Enable verbose output (debug-level logging) | |
--quiet |
-q |
No | Suppress non-error output |
--dry-run |
No | Preview what would be generated without writing files |
Generate all types from multiple schemas:
dotnet run -- -s windows.schema.json linux.schema.json -o ./Generated -n MyApp.ConfigGenerate only shared types:
dotnet run -- -m Shared -s windows.schema.json linux.schema.json -o SharedConfig.cs -n MyApp.ConfigGenerate variant-specific types:
dotnet run -- -m Variant -v Windows -s windows.schema.json linux.schema.json -o WindowsConfig.cs -n MyApp.ConfigIn All mode, the tool generates:
I{RootType}.cs- Marker interface implemented by all variant typesShared{RootType}.cs- Types common to all schemas{Variant}{RootType}.cs- Variant-specific types for each schema
The codebase is organized into several key components:
DotSchema/
├── Program.cs # Entry point, CLI parsing
├── CommandLineOptions.cs # CLI option definitions
├── Constants.cs # Shared constants and naming utilities
├── CodePostProcessor.cs # Roslyn-based code cleanup and transformation
├── Analyzers/
│ └── SchemaAnalyzer.cs # Detects shared vs variant-specific types
└── Generators/
├── SchemaGenerator.cs # Orchestrates code generation
├── CleanTypeNameGenerator.cs # Type name cleanup
└── PascalCasePropertyNameGenerator.cs # Property name conversion
Flow:
SchemaAnalyzerparses all schemas and categorizes types as shared, variant-specific, or conflictingSchemaGeneratoruses NJsonSchema to generate C# code with custom name generatorsCodePostProcessoruses Roslyn syntax trees to clean up the generated code (seal classes, remove boilerplate, add interfaces)
- CommandLineParser - Command line argument parsing
- NJsonSchema.CodeGeneration.CSharp - JSON Schema to C# code generation
- Microsoft.CodeAnalysis.CSharp - Roslyn C# syntax tree APIs for code formatting
- Microsoft.Extensions.Logging - Logging infrastructure