Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating JSON schema from C# class

Is there any way to programmatically generate a JSON schema from a C# class?

Something which we can do manually using http://www.jsonschema.net/

like image 518
Ravi Gupta Avatar asked Apr 03 '13 09:04

Ravi Gupta


2 Answers

Another option which supports generating JSON Schema v4 is NJsonSchema:

var schema = JsonSchema.FromType<Person>(); var schemaJson = schema.ToJson(); 

The library can be installed via NuGet.

Update for NJsonSchema v9.4.3+:

using NJsonSchema;  var schema = await JsonSchema.FromTypeAsync<Person>(); var schemaJson = schema.ToJson(); 
like image 153
Rico Suter Avatar answered Sep 20 '22 06:09

Rico Suter


JsonSchemaGenerator js = new JsonSchemaGenerator(); var schema = js.Generate(typeof(Person)); schema.Title = typeof(Person).Name; using (StreamWriter fileWriter = File.CreateText(filePath)) {       fileWriter.WriteLine(schema); } 
like image 25
Daniel Avatar answered Sep 20 '22 06:09

Daniel