我想定义一个像这样的对象,以映射到类似java Map的东西(但不完全是,因为这也需要与typescript接口)。最终,我想弄清楚的是如何在另一个对象中声明<T>
类型的东西(比如说Dict<MyObj>
)(如果这是可能的话):
(这显然不是一个有效的json架构)
{
"type": "object",
"javaType": "some.package.base.DictKeyValuePair",
"properties": {
"key": { "type": "string" },
"value": { "type": "<T>" }
}
}
将在其他json模式中引用,如下所示:
{
"type": "object",
"javaType": "some.package.base.Dict",
"properties": {
"key": { "type": "string" },
"value": { "type": "<T>" },
"keValuePairs": {
"type": "array",
"items": {
"$ref": "DictKeyValuePair.json",
"genericType": "<T>"
}
}
}
}
最终用于与此类似的对象:
{
"type": "object",
"javaType": "some.package.SomeObject",
"properties": {
"someDict": {
"$ref": "Dict.json",
"genericType": "SomeObjectOrSimpleType"
}
}
}
所以...这在json架构中是否可行?
答案 0 :(得分:0)
使用json-schema可以做类似的事情,但你需要定义模式,看看这个样本:
http://json-schema.org/learn/examples/address.schema.json
{
"$id": "https://example.com/address.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "An address similar to http://microformats.org/wiki/h-card",
"type": "object",
"properties": {
"post-office-box": {
"type": "string"
},
"extended-address": {
"type": "string"
},
"street-address": {
"type": "string"
},
"locality": {
"type": "string"
},
"region": {
"type": "string"
},
"postal-code": {
"type": "string"
},
"country-name": {
"type": "string"
}
},
"required": [
"locality",
"region",
"country-name"
],
"dependencies": {
"post-office-box": [
"street-address"
],
"extended-address": [
"street-address"
]
}
}