示例:
{
"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数据的模式生成器,但是使用它们会丢失描述。
我知道这方面可能有一个问题,虽然还没有找到。
发布于 2022-04-13 14:01:04
这是我的方法。很难看,但能把工作做完。
如果您想解析sharingUser的属性,比方说,您将DISCRIMINATOR设置为sharingUser. (带有句点)。
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文件。
https://stackoverflow.com/questions/71848450
复制相似问题