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

发布于 2025-02-12 02:16:30 字数 830 浏览 1 评论 0原文

我有一个类层次结构,我想在数组中标记的联盟中序列化与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; }
}

示例JSON

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

层次结构可能会更改,我想发布一个不错的JSON模式,用于在VSCODE中验证和手动编辑JSON文件。有什么合理的工具可以让我生成这个?甚至只是一些强烈键入的JSON模式C#类,我可以用来从反射中创建模式。

I have a class hierarchy that I want to serialize to json in a tagged union in an array.

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

{
    "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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俏︾媚 2025-02-19 02:16:30

我的nuget软件包 jsonschema.net.net.generation.generation.generation 是从C#代码中构建的。

可以在

My Nuget package JsonSchema.Net.Generation is built for generating schemas from C# code.

The docs and a playground can be found on https://json-everything.net/json-schema

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文