0

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.

ivarne
  • 3,753
  • 1
  • 27
  • 31

1 Answers1

0

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

gregsdennis
  • 7,218
  • 3
  • 38
  • 71