JSON模式引用解析



我有一个JSON模式,包含"$ref"标签,我试图得到一个版本的JSON模式有"$ref"标签解决。我只希望解决"$ref"从JSON Schema字符串(即。不需要外部分辨率)。

是否有一个库执行JSON模式的解析?(我目前使用org.everit.json.schema库,这是伟大的,但我找不到如何做我需要的)。

例如,我原来的模式是:

{
"$id": "https://example.com/arrays.schema.json",
"description": "A representation of a person, company, organization, or place",
"title": "complex-schema",
"type": "object",
"properties": {
"fruits": {
"type": "array",
"items": {
"type": "string"
}
},
"vegetables": {
"type": "array",
"items": { "$ref": "#/$defs/veggie" }
}
},
"$defs": {
"veggie": {
"type": "object",
"required": [ "veggieName", "veggieLike" ],
"properties": {
"veggieName": {
"type": "string",
"description": "The name of the vegetable."
},
"veggieLike": {
"type": "boolean",
"description": "Do I like this vegetable?"
}
}
}
}
}

将解析为如下内容(注意"#defs/veggie"解析为在模式中内联插入的定义):

{
"$id": "https://example.com/arrays.schema.json",
"description": "A representation of a person, company, organization, or place",
"title": "complex-schema",
"type": "object",
"properties": {
"fruits": {
"type": "array",
"items": {
"type": "string"
}
},
"vegetables": {
"type": "array",
"items": {
"type": "object",
"required": [ "veggieName", "veggieLike" ],
"properties": {
"veggieName": {
"type": "string",
"description": "The name of the vegetable."
},
"veggieLike": {
"type": "boolean",
"description": "Do I like this vegetable?"
}
}
}
}
}
}

这在一般意义上是不可能的,因为:

  • $ref可能是递归的(即再次引用自己)
  • $ref中的关键字可能会重复包含模式中的一些关键字,这将导致一些逻辑被覆盖。

为什么需要以这种方式修改模式?一般来说,JSON模式实现将在根据提供的数据评估模式时自动解析$ref。

最新更新