generated from actions/typescript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 9
feat: 商店存放 json5 格式的数据 #259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f9ddc22
chore(deps): 添加 pyjson5
he0119 4971efc
feat: 商店存放 json5 格式的数据
he0119 9ae97d8
fix: 模块名应该为 pyjson5
he0119 1fcdbe7
refactor: 使用推荐的 decode 方法
he0119 b90c804
test: 加上 json5 的测试
he0119 3ade02d
fix: 后缀都改成 json5
he0119 7357b7c
fix: 空列表末尾也应该有换行符
he0119 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import json | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| import httpx | ||
| import pyjson5 | ||
| from pydantic_core import to_jsonable_python | ||
|
|
||
|
|
||
| def load_json_from_file(file_path: Path): | ||
| """从文件加载 JSON 文件""" | ||
| with open(file_path, encoding="utf-8") as file: | ||
| return json.load(file) | ||
|
|
||
|
|
||
| def load_json_from_web(url: str): | ||
| """从网络加载 JSON 文件""" | ||
| r = httpx.get(url) | ||
| if r.status_code != 200: | ||
| raise ValueError(f"下载文件失败:{r.text}") | ||
| return r.json() | ||
|
|
||
|
|
||
| def dump_json(path: Path, data: Any, minify: bool = True) -> None: | ||
| """保存 JSON 文件 | ||
|
|
||
| 为减少文件大小,还需手动设置 separators | ||
| """ | ||
| data = to_jsonable_python(data) | ||
| with open(path, "w", encoding="utf-8") as f: | ||
| if minify: | ||
| json.dump(data, f, ensure_ascii=False, separators=(",", ":")) | ||
| else: | ||
| json.dump(data, f, ensure_ascii=False, indent=2) | ||
|
|
||
|
|
||
| def load_json5_from_file(file_path: Path): | ||
| """从文件加载 JSON5 文件""" | ||
| with open(file_path, encoding="utf-8") as file: | ||
| return pyjson5.decode_io(file) # type: ignore | ||
|
|
||
|
|
||
| def load_json5_from_web(url: str): | ||
| """从网络加载 JSON5 文件""" | ||
| r = httpx.get(url) | ||
| if r.status_code != 200: | ||
| raise ValueError(f"下载文件失败:{r.text}") | ||
| return pyjson5.decode(r.text) | ||
|
|
||
|
|
||
| def dump_json5(path: Path, data: Any) -> None: | ||
| """保存 JSON5 文件 | ||
|
|
||
| 手动添加末尾的逗号和换行符 | ||
| """ | ||
| data = to_jsonable_python(data) | ||
|
|
||
| content = json.dumps(data, ensure_ascii=False, indent=2) | ||
| # 手动添加末尾的逗号和换行符 | ||
| # 避免合并时出现冲突 | ||
| content = content.replace("}\n]", "},\n]") | ||
| content += "\n" | ||
|
|
||
| with open(path, "w", encoding="utf-8") as f: | ||
| f.write(content) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.