1

如果我有如下架构:

{
  "id": "http://example.com/my_application",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "my_application_schema",
  "additionalProperties": false,
  "definitions": {
    "NumberField": {
      "type": "number",
      "min": 0,
      "max": 2147483647
    },
    "StringField": {
      "type": "string",
      "minLength": 1,
      "maxLength": 12
    },
    "MainObject": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "object1": {
          "$ref": "#/definitions/Object1"
        },
        "object2": {
          "$ref": "#/definitions/Object2"
        }
      },
      "minProperties": 1,
      "maxProperties": 1
    },
    "Object1": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "field1": {
          "$ref": "#/definitions/NumberField",
          "proto_field": 1
        }
      },
      "required": [
        "field1"
      ]
    },
    "Object2": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "field1": {
          "$ref": "#/definitions/StringField",
          "proto_field": 1
        }
      },
      "required": [
        "field1"
      ]
    }
  },
  "type": "object",
  "properties": {
    "Content": {
      "$ref": "#/definitions/MainObject"
    }
  }
}

我正在使用以下代码生成一组 C# 类:

NJsonSchema.JsonSchema4 schema = await NJsonSchema.JsonSchema4.FromFileAsync(<path to schema>);
var generator = new CSharpGenerator(schema);
var file = generator.GenerateFile();

并且,使用生成的类,执行以下操作:

My_application_schema o = JsonConvert.DeserializeObject<My_application_schema>(@"
            {
'Content': {
'object1': {
'field1': 20
}
}
}");

然后反序列化完成而没有错误,但生成的对象o包含对object1object2的引用,尽管object2的所有成员都是空的。

JsonSerializerSettings settings = new JsonSerializerSettings()
{
    NullValueHandling = NullValueHandling.Ignore
};
String s = Newtonsoft.Json.JsonConvert.SerializeObject(o, settings);
// {"Content":{"object1":{"field1":20.0},"object2":{}}}

我需要的是object2要么不存在于反序列化对象中,要么设置为 null。有没有办法做到这一点,无论是在模式本身还是在这个管道中涉及的各种进程之一?

4

2 回答 2

3

这是否意味着您的 object2 为空或您正在寻找的类型,因此所需的架构更改将类似于:

"properties": {
  "object1":  {
    "oneOf": [
      {
        "$ref": "#/definitions/Object1"
      },
      {
        "type": "null"
      }
    ]
  },
  "object2": {
    "oneOf": [
      {
        "$ref": "#/definitions/Object2"
      },
      {
        "type": "null"
      }
    ]
  }
}

可以在显示公司对象的github 页面上找到类似的示例

于 2017-11-23T17:19:37.990 回答
2

一个相当漫无边际的答案,因为我不知道 C#。

GSON (Java) 将缺失的属性视为空值,NJsonSchema 将缺失的属性视为其类型的默认值。

看起来不需要的初始值是由模板在以下行生成的:https ://github.com/RSuter/NJsonSchema/blob/1a50dfa4d5d562ae89e7aac49c1573ad4e32313a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid#L16

这是以HasDefaultValue真实为条件的。这是在https://github.com/RSuter/NJsonSchema/blob/1a50dfa4d5d562ae89e7aac49c1573ad4e32313a/src/NJsonSchema.CodeGeneration/Models/PropertyModelBase.cs#L40中设置的

https://github.com/RSuter/NJsonSchema/blob/1a50dfa4d5d562ae89e7aac49c1573ad4e32313a/src/NJsonSchema.CodeGeneration/Models/PropertyModelBase.cs#L50提到_settings.GenerateDefaultValues。也许你可以设置为_settings.GenerateDefaultValues假?

https://groups.google.com/forum/#!topic/jsonschema/mD6GDca4zN8看起来 JSON 模式可以有空值。也许这会解决它,但代价是向模式中添加文本?

于 2017-11-23T17:12:01.633 回答