我想从dynamic类型的对象中提取JSON模式(as defined here).
This is the best example I could find.
但是JSON.NET的Schema Generator需要查看实际的类/类型才能生成模式.
任何人对如何从动态对象中提取模式有任何想法?
解决方法:
您仍然可以使用JSON.NET从动态对象中提取JSON模式.你只需要一个动态类型的实际对象就可以做到这一点.请尝试以下示例:
dynamic person = new
{
Id = 1,
FirstName = "John",
LastName = "Doe"
};
JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator {};
JsonSchema schema = schemaGenerator.Generate(person.GetType());
生成的JSON模式应如下所示:
{
"type": "object",
"additionalProperties": false,
"properties": {
"Id": {
"required": true,
"type": "integer"
},
"FirstName": {
"required": true,
"type": [
"string",
"null"
]
},
"LastName": {
"required": true,
"type": [
"string",
"null"
]
}
}
}