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.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56a5f3618802ab2b65fd05ad267fe27043213cf66fae9dae0f3ef16772d47fb1 |
|
MD5 | f9d8789138a8c2de9c61627025bb5f4b |
|
BLAKE2b-256 | 901b5e0d171c8b9e25921778c940decac3c94459f34dc17b78807699b9873d9a |
Hashes for custom_json_encoder-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92f88706ed8176a2903f5300b0242fccb111bf850363217ca562bafdbfe304c7 |
|
MD5 | 252e4f7748342799c6d230d99e77de52 |
|
BLAKE2b-256 | c38dcb4fd85e890e4e5dcffee6abb0848109de997eb4efd97b39a4ae6adb88e8 |