【发布时间】:2026-01-07 11:05:01
【问题描述】:
例如,我有一个 JSON 模式,如下所示:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"billing_address": { "$ref": "#/definitions/address" },
"shipping_address": { "$ref": "#/definitions/address" }
}
"definitions": {
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" }
},
"required": ["street_address", "city", "state"]
}
}
}
这个模式表示一个对象有两个可变的billing_address和shipping_address,它们都是address类型,包含三个属性:street_address、city 和 state。
现在我得到了另一个“更大”的架构:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"billing_address": { "$ref": "#/definitions/address" },
"shipping_address": { "$ref": "#/definitions/address" },
"new_address": { "$ref": "#/definitions/address" }
}
"definitions": {
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" },
"zip_code": { "type": "string" }
},
"required": ["street_address", "city", "state"]
}
}
}
如您所见,我在架构中添加了一个新属性 new_address,并且在 address 中有一个名为 zip_code 的新属性,这不是必需的属性。
因此,如果我从旧的 JSON 模式创建了一个对象,它也应该可用于新的 JSON 模式。在这种情况下,我们将称新模式与旧模式兼容。 (换句话说,新模式是旧模式的扩展,但没有修改。)
问题是如何判断一个模式是否与 Java 中的另一个模式兼容?还应注意复杂的情况,例如数字字段的“最小”属性。
【问题讨论】:
-
json 是无模式的。您将必须实现自己的 json 验证器。但这不是很好......
标签: java json jsonschema