如何使用newtonsoft

时间:2019-05-09 14:09:32

标签: c# json.net jsonschema

使用Newtonsoft.Json.Schema,我试图添加依赖项并将其序列化为json模式(.json文件)。

var schema = new JSchema()
{
    Type = JSchemaType.Object,
    SchemaVersion = new Uri("http://json-schema.org/draft-07/schema#")
};

schema.Properties.Add("ShowField", new JSchema() { Type = JSchemaType.Boolean });

var oneOf1 = new JSchema();
oneOf1.Properties.Add("ShowField", new JSchema() {Enum = { false }});

var oneOf2 = new JSchema();
oneOf2.Properties.Add("ShowField", new JSchema() { Enum = { true } });
oneOf2.Properties.Add("ExtraField", new JSchema() { Type = JSchemaType.String });

var oneOf = new JSchema();
oneOf.OneOf.Add(oneOf1);
oneOf.OneOf.Add(oneOf2);

schema.Dependencies.Add("ShowField", oneOf);

Console.WriteLine(schema.ToString());

我期望的结果是:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "ShowField": {
            "type": "boolean"
        }
    },
    "dependencies": {
        "ShowField": {
            "oneOf": [
                {
                    "properties": {
                        "ShowField": {
                            "enum": [false]
                        }
                    }
                },
                {
                    "properties": {
                        "ShowField": {
                            "enum": [true]
                        },
                        "ExtraField": {
                            "type": "string"
                        }
                    }
                }
            ]
        }
    }
}

但是当我运行代码时,结果是这样的:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "ShowField": {
      "type": "boolean"
    }
  }
}

我已经检查了JSchema对象,并验证了将依赖项添加到该对象中,但是仍然没有序列化依赖项。

有什么提示吗?代码中有任何错误?

1 个答案:

答案 0 :(得分:0)

这是Newtonsof.Json.Schema的问题。现在已修复: https://github.com/JamesNK/Newtonsoft.Json.Schema/issues/187