简体   繁体   English

从 C# 中的类层次结构生成 JsonSchema

[英]Generate JsonSchema from a class hierarchy in C#

I have a class hierarchy that I want to serialize to json in a tagged union in an array.我有一个类层次结构,我想在数组中的标记联合中序列化为 json。

class BaseComponent
{
    public string Id { get; set; }
}
class Child1: BaseComponent
{
    public string Child1Prop { get; set; }
}
class Child2: BaseComponent
{
    public string Child2Prop { get; set; }
}

class Wrapper
{
    public List<BaseComponent> Components { get; set; }
}

Sample json示例 json

{
    "components": [
        {
            "id": "id-1",
            "type": "Child1",
            "child2Prop": "Hello"
        },
        {
            "id": "id-2",
            "type": "Child2",
            "child2Prop": "world"
        },
    ]
}

The hierarchy is likely to change, and I want to publish a nice Json Schema for validation and manual editing of the json files in VSCode.层次结构可能会发生变化,我想发布一个不错的 Json Schema 来验证和手动编辑 VSCode 中的 json 文件。 Is there any reasonable tools that lets me generate this?有什么合理的工具可以让我生成这个吗? Or even just some strongly typed json schema C# classes that i can use to create the schema from reflection.甚至只是一些强类型的 json 模式 C# 类,我可以使用它们从反射创建模式。

My Nuget package JsonSchema.Net.Generation is built for generating schemas from C# code.我的 Nuget 包JsonSchema.Net.Generation是为从 C# 代码生成模式而构建的。

The docs and a playground can be found on https://json-everything.net/json-schema可以在https://json-everything.net/json-schema上找到文档和游乐场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM