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
Built Distribution
Hashes for custom_json_encoder-0.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92dc621e992ee93d6a132cb6653ff68d2ab17fec319512a15700dbb8a4f0ffa5 |
|
MD5 | 2a8b416ebcdd9f7d7e27fdd8c5fab89c |
|
BLAKE2b-256 | 171ebbba653ba75f6a4311babdbbb210d12cc98b25c582276db821050e115bf7 |
Hashes for custom_json_encoder-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e776c64e98bcaeafb3e88326ec80d46de3abd4dad0cd915fbce85dffdad55cc |
|
MD5 | 8c497d7024d23ce4d3e2ec8943b160f6 |
|
BLAKE2b-256 | 1f5b1c3f773c949f144944763eeaf2f0f8852c1977d12fedb59edacd1358ba2e |