首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自抽象模式定义的JSON-schema

来自抽象模式定义的JSON-schema
EN

Stack Overflow用户
提问于 2022-04-12 19:56:32
回答 1查看 80关注 0票数 0

我有 JSON摘要吗?从表这里解析的架构。

示例:

代码语言:javascript
复制
{
    "kind": {
        "type": "string",
        "description": "Identifies what kind of resource this is. Value: the fixed string \"drive#file\".",
        "Notes": ""
    },
    "id": {
        "type": "string",
        "description": "The ID of the file.",
        "Notes": "writable"
    },
    "name": {
        "type": "string",
        "description": "The name of the file. This is not necessarily unique within a folder. Note that for immutable items such as the top level folders of shared drives, My Drive root folder, and Application Data folder the name is constant.",
        "Notes": "writable"
    },
    "sharingUser.kind": {
        "type": "string",
        "description": "Identifies what kind of resource this is. Value: the fixed string \"drive#user\".",
        "Notes": ""
    },
    "sharingUser.displayName": {
        "type": "string",
        "description": "A plain text displayable name for this user.",
        "Notes": ""
    }
}

我怎样才能轻松地将它转换成一个完整的JSON模式,其中包含引用之类的东西呢?

我可以使用来自JSON数据的模式生成器,但是使用它们会丢失描述。

我知道这方面可能有一个问题,虽然还没有找到。

EN

回答 1

Stack Overflow用户

发布于 2022-04-13 14:01:04

这是我的方法。很难看,但能把工作做完。

如果您想解析sharingUser的属性,比方说,您将DISCRIMINATOR设置为sharingUser. (带有句点)。

代码语言:javascript
复制
import json
from pathlib import Path

path = Path("./common/folder/")
desc_path = Path(path, "desc.json")
output_path = Path(path, "output.json")

DISCRIMINATOR = "sharingUser."
NOT_IN = ["."]
DEPRECATED = "Warning: This item is deprecated."
desc: dict[str, dict] = dict(json.loads(desc_path.read_text("utf-8")))


class Types:
    def __init__(self) -> None:
        self.mapping = {"long": "integer", "list": "array"}

    def get_type(self, __name: str):
        return self.mapping.get(__name, __name)


output = {"properties": {}}
types = Types()

for name, prop in desc.items():
    if name.startswith(DISCRIMINATOR) and not (DEPRECATED in prop["description"]):
        name = name.replace(DISCRIMINATOR, "")
        if not any(x in name for x in NOT_IN):
            title = name
            type = types.get_type(prop["type"])
            description = prop["description"].replace("  ", "\n")
            if prop["Notes"]:
                description += f"\n\n.. note:\n    {prop['Notes']}"

            output["properties"][name] = {
                "title": name,
                "type": type,
                "description": description,
            }


output_path.write_text(json.dumps(output), "utf-8")

输出将是带有jsonschema "properties“对象的json文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71848450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档