1

我有一个 JSONSchema 将有一些项目。现在定义这些项目的模式需要在主模式中引用吗?

* one schema that you reference:
 { 
    "id": "http://some.where/sub/schema#", 
    "type": "object", 
    "properties": { 
        "p1": { 
            "type": "integer", 
            "minimum": 12 
        } 
    }     
} 
--- * the main schema: ---- 
{ 
    "id": "http://path.to/base/schema#", 
    "type": "array", 
    "items": { 
        "extends": { 
            "$ref": "http://some.where/sub/schema#/properties/p1" 
        }, 
        "divisibleBy": 5 
    }     
} 

另请注意,我将在该项目中有多个项目。我在 api 中看不到这样做的方法。api 也不允许我添加自定义属性。我怎样才能做到这一点?我正在使用JSON.net

4

1 回答 1

3

由于评论太长,我将其发布为答案。但是您应该根据自己的需要对其进行自定义。

string oneSchema = @"{ 
    ""id"": ""http://some.where/sub/schema#"", 
    ""type"": ""object"", 
    ""properties"": { 
        ""p1"": { 
            ""type"": ""integer"", 
            ""minimum"": 12 
        } 
    }     
} ";

string main = @"
{ 
    ""id"": ""http://path.to/base/schema#"", 
    ""type"": ""array"", 
    ""items"": { 
        ""extends"": { 
            ""$ref"": ""http://some.where/sub/schema#/properties/p1"" 
        }, 
        ""divisibleBy"": 5 
    }     
}";

var JObjMain = (JObject)JsonConvert.DeserializeObject(main);
var jObjOther = (JObject)JsonConvert.DeserializeObject(oneSchema);

JToken src = JObjMain["items"]["extends"]["$ref"];
JToken reference = jObjOther["id"];


var path = src.ToString().Replace(reference.ToString(), "").Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
JToken j = jObjOther[path[0]];
for(int i=1;i<path.Length;i++)
{
    j = j[path[i]];
}

src.Replace(j);

Console.WriteLine(JObjMain);
于 2012-01-08T20:30:47.780 回答