Reputation: 1085
I'm working on generating a HTML form dynamically from a class in C#. My current approach is to generate a JSON Schema from the C# class using the JsonSchemaGenerator included in Json.NET on the server side and then render the form from this JSON Schema on the client side.
I want to be able to add JSON Schema properties such as a title or description to the properties of the C# class, but can't find any simple way to get JsonSchemaGenerator to include these in the generated schema.
I can add these JSON Schema properties to the class itself using one of the C# attributes JsonObjectAttribute, JsonArrayAttribute, or JsonDictionaryAttribute, but using the JsonProperty attribute for the properties, I can only change whether the property is required or not.
In essence, what I want is to generate this JSON Schema:
{
"type": "object",
"properties": {
"MyString": {
"type": "string",
"title": "Title for my string"
}
}
}
from this class:
class MyClass
{
public string MyString { get; set; }
}
Should I write a custom JsonConverter or add in the schema properties manually? Is Json.NET not the right tool for the job? Is JSON Schema the wrong format for this in the first place? Or is there just some fancy function of JsonSchemaGenerator that I've missed?
Upvotes: 2
Views: 2565
Reputation: 34293
JsonSchemaGenerator
considers all objects of a type to have the same schema ("type" in generic sense, not Type
class). It's not designed to support individual schemas for properties. It considers Title
and Description
attributes of a type, not of a property (this is why it looks only for JsonContainerAttribute
and its descendants). Therefore, even if you manage to use JsonConverter
as a workaround (to generate "types"), it would essentially be a hack.
If I understand correctly, the way Json.NET treats Title
and Description
is wrong. I recommend submitting a feature request/bug report.
P.S. I just looked at the code, I'm not entirely sure.
Upvotes: 1