c# - JsonSchemaGenerator 字符串的自定义 ContractResolver

标签 c# json json.net jsonschema

我想从自定义对象创建一个模式。自定义对象有一些字符串属性,这些属性被架构化为类型 String|Null。我如何修改 JSonSchemaGenerator 以将字符串转换为类型 String 而不是 String|Null? 换句话说,如果我序列化 Jsonschema,我不希望这样:

{
  "title":"myObject",
  "type":"object",
  "properties":{
    "ID":{
      "required":true,
      "type":["string","null"]
    }
  }
}

但是这个:

{
  "title":"myObject",
  "type":"object",
  "properties":{
    "ID":{
      "required":true,
      "type":"string"
    }
  }
}

最佳答案

你可以这样做:

[JsonProperty(Required = Required.Always)]
public string ID {get;set;}

关于c# - JsonSchemaGenerator 字符串的自定义 ContractResolver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21707506/

相关文章:

Azure 持续部署在 Newtonsoft 上失败,而 VS 发布正常

php - 复杂的 JSON 数组通过 PHP 到 MySQL

c# - 在 C# 中处理货币金额的最佳实践

c# - 静态变量和多线程

c# - 将处理程序的结果转换为数组格式

javascript - foreach for JSON 数组,语法

C# Http.Response 流返回内容类型为 application/json 的空字符串

c# - 为什么 JsonConvert.DeserializeObject 返回空值?

c# - 序列化对象时如何忽略 JsonPropertyNameAttribute 注释?

c# - 锁和线程池