我对 json 很陌生。在定义我的 RESTful API 结果的格式(即 JSON)时,我觉得将它记录为我自己的JSON 模式会更容易。在写一篇的时候,我有几个问题:
- 在我的结果 JSON 中,我如何指定它确认的架构的 URI?--edit-- 是否使用
$schema
属性? - JSON 模式版本控制是否有任何约定/指南?是否有一些我应该/可以在我的模式中定义为属性的属性?我看到JSON 模式本身没有定义版本,除了它的 URI 指定为 key 的值
$schema
。 - 我可以将我的一个 BIG JSON 模式分解为多个较小的模式并将一个包含在另一个中吗?就像 C++ 中的 #include 一样,然后在我发送给用户的 JSON 中引用多个模式作为结果。
- 我可以为键“类型”定义自定义值吗?例如,我想像这样重用“日期”的定义:
[忽略这一行,这是为了使格式适用于以下 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 类型,这是不可能的,但它似乎是一个基本的用例!