Skip to main content

A JSON encoder that allows customizing the indentation based on the content and

Project description

Custom JSON Encoder

A JSON encoder that allows customizing the indentation based on the content and the width of the line.

See the command-line tool to understand how to use the CustomJSONEncoder class. This tool is a patch from json/tool.py, so follow the #region and #endregion comments to understand the differences.

Command Line Interface

Instead of using the standard JSON tool

$ 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()"
                }
            ]
        }
    }
}

you can use the custom JSON encoder instead with the same flags

$ 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()"
                }
            ]
        }
    }
}

This tool provides the same functionality as the standard JSON tool

$ 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

except for the --indent-after, --indent-after-width and --indent-after-indentation flags, which allow indenting ONLY after the given key or after reaching the given width.

$ 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()"}
            ]}}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

custom_json_encoder-0.5.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

custom_json_encoder-0.5.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file custom_json_encoder-0.5.0.tar.gz.

File metadata

  • Download URL: custom_json_encoder-0.5.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for custom_json_encoder-0.5.0.tar.gz
Algorithm Hash digest
SHA256 da455020dc57dd80c0bfa125d23bff2905cc49b6fc92e157bb72c8b68e371bdf
MD5 57aa7ccf619bfc15fbb17d7702b489b7
BLAKE2b-256 4ee497944936e71b2fe644e4f9c9b7a66a4c980714f61c008090e4c3e4700f86

See more details on using hashes here.

File details

Details for the file custom_json_encoder-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for custom_json_encoder-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 db2bfac6409731c3a8e2740e289ba0e5d7b9024157be88adc9edead6af065056
MD5 e862c72c20364fe69fd49087a5c82880
BLAKE2b-256 37cc91865d196781941528eb4226e67bfc968b24c254d3530156d7be64bbf36a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page