JSON Schema reader, generator and validator for .NET
JSON Schema reader, generator and validator for .NET
NSwag | NJsonSchema | Apimundo | Namotion.Reflection NJsonSchema is a .NET library to read, generate and validate JSON Schema draft v4+ schemas. The library can read a schema from a file or string and validate JSON data against it. A schema can also be generated from an existing .NET class. With the code generation APIs you can generate C# and TypeScript classes or interfaces from a schema.
The library uses Json.NET to read and write JSON data and Namotion.Reflection for additional .NET reflection APIs.
NuGet packages:
Preview NuGet Feed: https://www.myget.org/F/njsonschema/api/v3/index.json
Features:
JsonSchema.FromJsonAsync())JsonSchema.FromType<MyType>())JsonSchema.FromSampleJson())NJsonSchema is heavily used in NSwag, a Swagger API toolchain for .NET which generates client code for Web API services. NSwag also provides command line tools to use the NJsonSchema's JSON Schema generator (command types2swagger).
The project is developed and maintained by Rico Suter and other contributors.
Some code generators can directly be used via the Apimundo service.
The JsonSchema class can be used as follows:
var schema = JsonSchema.FromType<Person>();
var schemaData = schema.ToJson();
var errors = schema.Validate("{...}");
foreach (var error in errors)
Console.WriteLine(error.Path + ": " + error.Kind);
schema = await JsonSchema.FromJsonAsync(schemaData);The Person class:
…The generated JSON schema data stored in the schemaData variable:
…The NJsonSchema.CodeGeneration can be used to generate C# or TypeScript code from a JSON schema:
var generator = new CSharpGenerator(schema);
var file = generator.GenerateFile();The file variable now contains the C# code for all the classes defined in the JSON schema.
The previously generated JSON Schema would generate the following TypeScript interfaces.
Settings:
new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, TypeScriptVersion = 4.3m }Output:
export enum Gender {
Male = 0,
Female = 1,
}
export interface Company {
Name: string | undefined;
}
export interface Car {
Name: string | undefined;
Manufacturer: Company | undefined;
}
export interface Person {
FirstName: string;
MiddleName: string | undefined;
LastName: string;
Gender: Gender;
NumberWithRange: number;
Birthday: Date;
Company: Company | undefined;
Cars: Car[] | undefined;
}... and the following TypeScript classes.
Settings:
new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, TypeScriptVersion = 4.3m }Output:
…The NJsonSchema.SampleJsonSchemaGenerator can be used to generate a JSON Schema from sample JSON data:
By default, the NJsonSchema.SampleJsonSchemaGenerator generates a JSON Schema based on the JSON Schema specification. See: JSON Schema Specification
var generator = new SampleJsonSchemaGenerator(new SampleJsonSchemaGeneratorSettings());
var schema = generator.Generate("{...}");Input:
{
"int": 1,
"float": 340282346638528859811704183484516925440.0,
"str": "abc",
"bool": true,
"date": "2012-07-19",
"datetime": "2012-07-19 10:11:11",
"timespan": "10:11:11"
}Output:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"int": {
"type": "integer"
},
"float": {
"type": "number"
},
"str": {
"type": "string"
},
"bool": {
"type": "boolean"
},
"date": {
"type": "string",
"format": "date"
},
"datetime": {
"type": "string",
"format": "date-time"
},
"timespan": {
"type": "string",
"format": "duration"
}
}
}To generate a JSON Schema for OpenApi, provide the SchemaType.OpenApi3 in the settings. See: OpenApi Specification
var generator = new SampleJsonSchemaGenerator(new SampleJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 });
var schema = generator.Generate("{...}");Input:
{
"int": 12345,
"long": 1736347656630,
"float": 340282346638528859811704183484516925440.0,
"double": 340282346638528859811704183484516925440123456.0,
}Output:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"int": {
"type": "integer",
"format": "int32"
},
"long": {
"type": "integer",
"format": "int64"
},
"float": {
"type": "number",
"format": "float"
},
"double": {
"type": "number",
"format": "double"
}
}
}Applications which use the library:
No open issues yet, or sync has not completed.