一个 JSON 编码器,允许根据内容和自定义缩进
项目描述
自定义 JSON 编码器
一个 JSON 编码器,允许根据内容和行宽自定义缩进。
请参阅命令行工具以了解如何使用CustomJSONEncoder 类。该工具是来自json/tool.py的补丁,因此请按照#region和#endregion注释了解差异。
命令行界面
而不是使用标准的JSON 工具
$ python -m json.tool demo.json --indent 4
{
"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{
"value": "New",
"onclick": "CreateNewDoc()"
},
{
"value": "Open",
"onclick": "OpenDoc()"
},
{
"value": "Close",
"onclick": "CloseDoc()"
}
]
}
}
}
您可以使用具有相同标志的自定义 JSON 编码器
$ python -m custom_json_encoder demo.json --indent 4
{
"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{
"value": "New",
"onclick": "CreateNewDoc()"
},
{
"value": "Open",
"onclick": "OpenDoc()"
},
{
"value": "Close",
"onclick": "CloseDoc()"
}
]
}
}
}
此工具提供与标准 JSON 工具相同的功能
$ python -m custom_json_encoder -h
usage: custom_json_encoder [-h] [--sort-keys] [--no-ensure-ascii] [--json-lines] [--indent INDENT | --indent-after KEY | --tab | --compact] [--indent-after-width AMOUNT]
[--indent-after-indentation AMOUNT]
[infile] [outfile]
A simple command line interface for json module to validate and pretty-print JSON objects.
positional arguments:
infile a JSON file to be validated or pretty-printed
outfile write the output of infile to outfile
options:
-h, --help show this help message and exit
--sort-keys sort the output of dictionaries alphabetically by key
--no-ensure-ascii disable escaping of non-ASCII characters
--json-lines parse input using the JSON Lines format. Use with --no-indent or --compact to produce valid JSON Lines output.
--indent INDENT separate items with newlines and use this number of spaces for indentation
--indent-after KEY indent after the given key using --indent-after-indentation spaces
--tab separate items with newlines and use tabs for indentation
--compact suppress all whitespace separation (most compact)
--indent-after-width AMOUNT
set the width of the output line when --indent-after is active
--indent-after-indentation AMOUNT
use this number of spaces for indentation when --indent-after is active
除了--indent-after,--indent-after-width和--indent-after-indentation标志,它们只允许在给定键之后或达到给定宽度之后缩进。
$ python -m custom_json_encoder demo.json --indent-after menuitem --indent-after-width 50 --indent-after-indentation 4
{
"menu": {"id": "file", "value": "File",
"popup": {"menuitem": [
{"value": "New", "onclick":
"CreateNewDoc()"},
{"value": "Open", "onclick":
"OpenDoc()"},
{"value": "Close", "onclick":
"CloseDoc()"}
]}}
}
项目详情
关
custom_json_encoder -0.3.0.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 1796ece381ea75be85cc6e8de01190c7d29990030db7303c5c37f975b590afe6 |
|
| MD5 | e062df8f2a31057deac8ee22b4ef26f7 |
|
| 布莱克2-256 | e1c2ae3d893cd80c6e372e1552e23bee622424cc6ea9df34f9e10eea33e9d8bb |
关
custom_json_encoder -0.3.0-py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | e6c327dc4f6fe3ed72133bdc6d70b802e42c8ea59f1acbc5377fe09a5ae5cb6b |
|
| MD5 | c0377aa6f4a0f6157e6c1ecf66e9db0f |
|
| 布莱克2-256 | 4f5c2724936d3ccfd016d5922613b5fd4aa808d0cff54256078397c16b47c076 |