json - JSON Schema 参考解析
问题描述
我有一个包含“$ref”标签的 JSON 模式,我正在尝试获取解析了“$ref”标签的 JSON 模式版本。我只想从 JSON Schema 字符串中的定义(标签)中解析“$ref”(即不需要外部解析)。
是否有执行 JSON Schema 解析的库?(我目前正在使用 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 Schema 实现将在根据提供的数据评估模式时自动解析 $refs。
推荐阅读
- linux - 迭代文件中的变量以检查 bash 中的特定值
- java - 从 PriorityQueue 转换为 ArrayList
- php - 带有 OpenSSL 的 PHP 中的 chacha20-poly1305
- c - 从不同的文件(主文件)访问 C 结构成员
- lotus-notes - 附件在笔记文档上显示两次
- django - 什么是 django 中的块用户工具?
- node.js - 使用 http-proxy-middleware 时无法传递 req.params ... NodeJS/Express
- javascript - 如何在 jQuery 中选择除对象和对象子项之外的所有内容(使用事件委托)
- r - R:理解与制作多边形相关的错误消息的问题
- mysql - 当我将数据从 servlet 存储到 mysql 数据库时,像“<”、“>”这样的字符被存储为像 u003c 这样的 unicode 格式,而不是实际的符号