SchemaSharp 0.2.0
dotnet add package SchemaSharp --version 0.2.0
NuGet\Install-Package SchemaSharp -Version 0.2.0
<PackageReference Include="SchemaSharp" Version="0.2.0" />
<PackageVersion Include="SchemaSharp" Version="0.2.0" />
<PackageReference Include="SchemaSharp" />
paket add SchemaSharp --version 0.2.0
#r "nuget: SchemaSharp, 0.2.0"
#:package SchemaSharp@0.2.0
#addin nuget:?package=SchemaSharp&version=0.2.0
#tool nuget:?package=SchemaSharp&version=0.2.0
SchemaSharp
SchemaSharp is a Roslyn source generator that produces strongly-typed C# types from JSON Schema documents.
Overview
- The generator parses JSON Schema files and produces C# POCOs using NJsonSchema and its C# code generation engine.
- Generated files are emitted into the compilation as additional source files (named
<TypeName>.g.cs). - The generator supports custom templates using
.liquidfiles. Template files are forwarded to theNJsonSchemaengine so you can customize generated output (for example, to inject attributes or additional members).
How it works
- At initialization the generator registers an embedded marker attribute
GenerateFromJsonSchemaAttributein theSchemaSharpnamespace. - During incremental execution the generator:
- Scans the compilation for types annotated with
GenerateFromJsonSchemaAttribute. - Collects all additional files whose filename ends with
schema.jsonand parses them withNJsonSchema. - Collects
.liquidtemplate files provided as additional text and forwards their directories to theNJsonSchemaC# generator as template directory locations. - For each matched schema/type the generator invokes
NJsonSchema.CodeGeneration.CSharp.CSharpGeneratorto create a C# file and adds it to the compilation as source.
- Scans the compilation for types annotated with
The generated types use settings that favor POCO style classes, nullable reference types, and optional properties mapped to nullable C# types where appropriate.
Usage
- Add the generator project or package to the consuming project (as an analyzer or package).
- Add the JSON Schema file(s) to the project as additional files. Filenames must end with
schema.json.
Example MSBuild entry:
<ItemGroup>
<AdditionalFiles Include="Schemas\person.schema.json" />
<AdditionalFiles Include="Templates\Class.Property.Annotations.liquid" />
</ItemGroup>
- Declare a partial type and apply the generator attribute. The type name must match the
titlein the JSON Schema so the generator can match schema → type.
[SchemaSharp.GenerateFromJsonSchema]
public partial class Person
{
}
- If you want to customize generated code, add
.liquidtemplate files as additional text. Template files are forwarded to NJsonSchema and can control template rendering.
Example template usage:
- To add attributes to all properties of a type create a template file named
Class.Property.Annotations.liquidwith the desired attribute text. For example:
[global::Microsoft.Extensions.Options.ValidateObjectMembersAttribute]
That content will be injected by the template into the generated property declarations according to how NJsonSchema templates are written.
Notes and recommendations
- The generator matches a schema to a type by comparing the type name to the JSON Schema
titlevalue. Each type must match exactly one schema. - The
.liquidtemplate mechanism and code-generation features are provided by NJsonSchema. Templates must followNJsonSchemaconventions for names and placements.
Troubleshooting
- If a schema cannot be read or if multiple schemas match a type name the generator emits a diagnostic to help locate the problem.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.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.