Skip to content

yovio/NJsonSchema

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NJsonSchema for .NET

Build status NuGet Version

JSON Schema draft v4 reader, generator and validator for .NET

NuGet packages:

The library uses Json.NET to read and write JSON data.

NJsonSchema usage

The JsonSchema4 type can be used as follows:

var schema = JsonSchema4.FromType<Person>();
var schemaData = schema.ToJson();

var jsonToken = JToken.Parse("...");
var errors = schema.Validate(jsonToken);

foreach (var error in errors)
    Console.WriteLine(error.Path + ": " + error.Kind);

schema = JsonSchema4.FromJson(schemaData);

The Person class:

public class Person
{
    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }

    public Gender Gender { get; set; }

    [Range(2, 5)]
    public int NumberWithRange { get; set; }

    public DateTime Birthday { get; set; }

    public Company Company { get; set; }

    public Collection<Car> Cars { get; set; }
}

public enum Gender
{
    Male,
    Female
}

public class Car
{
    public string Name { get; set; }

    public Company Manufacturer { get; set; }
}

public class Company
{
    public string Name { get; set; }
}

The generated JSON schema data stored in the schemaData variable:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "typeName": "Person",
  "type": "object",
  "required": [
	"FirstName",
	"LastName",
	"Gender",
	"NumberWithRange",
	"Birthday"
  ],
  "properties": {
	"FirstName": {
	  "type": "string"
	},
	"LastName": {
	  "type": "string"
	},
	"Gender": {
	  "type": "string",
	  "enum": [
		"Male",
		"Female"
	  ]
	},
	"NumberWithRange": {
	  "type": "integer",
	  "maximum": 5.0,
	  "minimum": 2.0
	},
	"Birthday": {
	  "type": "string",
	  "format": "date-time"
	},
	"Company": {
	  "typeName": "Company",
	  "type": "object",
	  "properties": {
		"Name": {
		  "type": "string"
		}
	  }
	},
	"Cars": {
	  "items": {
		"typeName": "Car",
		"type": "object",
		"properties": {
		  "Name": {
			"type": "string"
		  },
		  "Manufacturer": {
			"typeName": "Company",
			"type": "object",
			"$ref": "#/properties/Company"
		  }
		}
	  },
	  "type": "array"
	}
  }
}

NJsonSchema.CodeGeneration usage

The NJsonSchema.CodeGeneration can be used to generate C# or TypeScript code from a JSON schema:

var generator new CSharpClassGenerator(schema);
var file = generator.GenerateFile();

The file variable now contains the C# code for all the classes defined in the JSON schema.

Final notes

Applications which use the library:

  • VisualJsonEditor, a JSON schema based file editor for Windows.
  • NSwag: The Swagger API toolchain for .NET

About

JSON Schema draft v4 reader, generator and validator for .NET

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 99.7%
  • Batchfile 0.3%