Skip to main content

一个 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 (6.4 kB 查看哈希

已上传 source

内置分布

custom_json_encoder-0.3.0-py3-none-any.whl (7.7 kB 查看哈希

已上传 py3