Generating JSON schema from C# class

独自空忆成欢 提交于 2019-12-03 06:38:57

问题


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/


回答1:


Another option which supports generating JSON Schema v4 is NJsonSchema:

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

The library can be installed via NuGet.

Update for NJsonSchema v9.4.3+:

using NJsonSchema;

var schema = await JsonSchema4.FromTypeAsync<Person>();
var schemaJson = schema.ToJson();



回答2:


For those who land here from google searching for the reverse
(generate the C# class from JSON) - I use those fine online tools:

JSON:
http://json2csharp.com/
(Source: http://jsonclassgenerator.codeplex.com/)

XML:
http://xmltocsharp.azurewebsites.net/
(Source: https://github.com/msyoung/XmlToCSharp)




回答3:


JsonSchemaGenerator js = new JsonSchemaGenerator();
var schema = js.Generate(typeof(Person));
schema.Title = typeof(Person).Name;
using (StreamWriter fileWriter = File.CreateText(filePath))
{
      fileWriter.WriteLine(schema);
}


来源:https://stackoverflow.com/questions/15782982/generating-json-schema-from-c-sharp-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!