A JSON encoder that allows customizing the indentation based on the content and the width of the line.
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.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52cc7afe191011a16b4d645c88b203ccf518d1360c9dccc75190aaa908156b82 |
|
MD5 | ff8e6d94d6bb4f27642be0a77df58be2 |
|
BLAKE2b-256 | 9b8266bcf2d6c322496e1cd352c51501022b2503e27439b9286cd4fa15d8c22e |
Hashes for custom_json_encoder-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c636895e9b19c0812824494d3676b5852f5038403c328cc17e2c785880e130e |
|
MD5 | 2916b81bf9a3e1888031138e65b0fcd9 |
|
BLAKE2b-256 | f33a33ab01226980db4c8be7d76c3475719f21a0446eb735afe79d4d4b1ed71c |