我有一个 MVC 应用程序,它将我的模型序列化为 json 模式(使用 Newtonsoft json.net 模式)。问题是我的数组中的项的类型为 ["string", "null"]
,但我需要的只是 "string"
。这是我类(class)的代码:
public class Form
{
[Required()]
public string[] someStrings { get; set; }
}
这是由 Json.net 架构制作的架构:
"someStrings": {
"type": "array",
"items": {
"type": [
"string",
"null"
]
}
}
当我期待这个时:
"someStrings": {
"type": "array",
"items": {
"type": "string"
}
}
请帮我去掉那个“null”。
最佳答案
尝试在生成模式时将 DefaultRequired
设置为 DisallowNull
:
JSchemaGenerator generator = new JSchemaGenerator()
{
DefaultRequired = Required.DisallowNull
};
JSchema schema = generator.Generate(typeof(Form));
schema.ToString();
输出:
{
"type": "object",
"properties": {
"someStrings": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
关于c# - 在 Newtonsoft JSON.NET 模式中禁用空类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43610133/