Generating JSON schema from C# class

前端 未结 3 697
梦毁少年i
梦毁少年i 2020-12-28 14:34

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/

相关标签:
3条回答
  • 2020-12-28 14:38

    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)

    0 讨论(0)
  • 2020-12-28 14:43
    JsonSchemaGenerator js = new JsonSchemaGenerator();
    var schema = js.Generate(typeof(Person));
    schema.Title = typeof(Person).Name;
    using (StreamWriter fileWriter = File.CreateText(filePath))
    {
          fileWriter.WriteLine(schema);
    }
    
    0 讨论(0)
  • 2020-12-28 14:52

    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();
    
    0 讨论(0)
提交回复
热议问题