【发布时间】:2011-11-20 19:23:19
【问题描述】:
我不熟悉 json。在定义我的 RESTful API 结果的格式(即 JSON)时,我觉得将其记录为我自己的 JSON schema 会更容易。在写一篇文章时,我有几个问题:
- 在我的结果 JSON 中,如何将 URI 指定到它确认的架构?
--edit-- 是否使用
$schema属性? - 是否有任何 JSON 模式版本控制的约定/指南?是否有一些我应该/可以在我的模式中定义为属性的属性?我看到 JSON schema itself 没有定义版本,除了它的 URI 指定为键
$schema的值。 - 我能否将我的一个 BIG JSON 模式分解为多个较小的模式并将一个包含在另一个中?就像 C++ 中的 #include 一样,然后引用我发送给用户的 JSON 中的多个模式作为结果。
- 能否为键“type”定义自定义值?例如。我想像这样重用“日期”的定义:
[忽略这一行,这是为了使格式适用于以下 json..]
{
"date":{
"type":"object",
"properties":{
"month":{
"type":"integer",
"minimum":1,
"maximum":12
},
"year":{
"type":"integer",
"minimum":0
}
}
},
"personInfo":{
"type":"object",
"properties":{
"name":{
"type":"string"
},
"dateOfBirth":{
"type":"date"
}
}
},
"student":{
"type":"object",
"properties":{
"id":{
"type":"personInfo"
},
"pass_out_year":{
"type":"date"
}
}
}
}
而不是像这样在多个地方提供“日期”的属性:
{
"personInfo":{
"type":"object",
"properties":{
"name":{
"type":"string"
},
"dateOfBirth":{
"type":"object",
"properties":{
"month":{
"type":"integer",
"minimum":1,
"maximum":12
},
"year":{
"type":"integer",
"minimum":0
}
}
}
}
},
"student":{
"type":"object",
"properties":{
"id":{
"type":"personInfo"
},
"pass_out_year":{
"type":"object",
"properties":{
"month":{
"type":"integer",
"minimum":1,
"maximum":12
},
"year":{
"type":"integer",
"minimum":0
}
}
}
}
}
}
根据 5.1 类型 in the spec,这是不可能的,但它似乎是一个基本的用例!
【问题讨论】:
标签: json jsonschema