# 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](custom_json_encoder/__main__.py) to understand how to use the [CustomJSONEncoder class](custom_json_encoder/__init__.py). This tool is a patch from [json/tool.py](https://github.com/python/cpython/blob/3.10/Lib/json/tool.py), so follow the `#region` and `#endregion` comments to understand the differences.
## Command Line Interface
Instead of using the standard [JSON tool](https://docs.python.org/3/library/json.html#module-json.tool)
```bash
$ 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](custom_json_encoder/__main__.py) instead with the same flags
```bash
$ 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
```bash
$ 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.
```bash
$ 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()"}
]}}
}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/Kerrigan29a/py_custom_json_encoder",
"name": "custom-json-encoder",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "json,encoder,pretty-print,width",
"author": "Javier Escalada G\u00f3mez",
"author_email": "kerrigan29a@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/4e/e4/97944936e71b2fe644e4f9c9b7a66a4c980714f61c008090e4c3e4700f86/custom_json_encoder-0.5.0.tar.gz",
"platform": "any",
"description": "# Custom JSON Encoder\n\nA JSON encoder that allows customizing the indentation based on the content and the width of the line.\n\nSee [the command-line tool](custom_json_encoder/__main__.py) to understand how to use the [CustomJSONEncoder class](custom_json_encoder/__init__.py). This tool is a patch from [json/tool.py](https://github.com/python/cpython/blob/3.10/Lib/json/tool.py), so follow the `#region` and `#endregion` comments to understand the differences.\n\n## Command Line Interface\n\nInstead of using the standard [JSON tool](https://docs.python.org/3/library/json.html#module-json.tool)\n\n```bash\n$ python -m json.tool demo.json --indent 4\n{\n \"menu\": {\n \"id\": \"file\",\n \"value\": \"File\",\n \"popup\": {\n \"menuitem\": [\n {\n \"value\": \"New\",\n \"onclick\": \"CreateNewDoc()\"\n },\n {\n \"value\": \"Open\",\n \"onclick\": \"OpenDoc()\"\n },\n {\n \"value\": \"Close\",\n \"onclick\": \"CloseDoc()\"\n }\n ]\n }\n }\n}\n```\n\nyou can use the [custom JSON encoder](custom_json_encoder/__main__.py) instead with the same flags\n\n```bash\n$ python -m custom_json_encoder demo.json --indent 4\n{\n \"menu\": {\n \"id\": \"file\",\n \"value\": \"File\",\n \"popup\": {\n \"menuitem\": [\n {\n \"value\": \"New\",\n \"onclick\": \"CreateNewDoc()\"\n },\n {\n \"value\": \"Open\",\n \"onclick\": \"OpenDoc()\"\n },\n {\n \"value\": \"Close\",\n \"onclick\": \"CloseDoc()\"\n }\n ]\n }\n }\n}\n```\n\nThis tool provides the same functionality as the standard JSON tool\n\n```bash\n$ python -m custom_json_encoder -h\nusage: custom_json_encoder [-h] [--sort-keys] [--no-ensure-ascii] [--json-lines] [--indent INDENT | --indent-after KEY | --tab | --compact] [--indent-after-width AMOUNT]\n [--indent-after-indentation AMOUNT]\n [infile] [outfile]\n\nA simple command line interface for json module to validate and pretty-print JSON objects.\n\npositional arguments:\n infile a JSON file to be validated or pretty-printed\n outfile write the output of infile to outfile\n\noptions:\n -h, --help show this help message and exit\n --sort-keys sort the output of dictionaries alphabetically by key\n --no-ensure-ascii disable escaping of non-ASCII characters\n --json-lines parse input using the JSON Lines format. Use with --no-indent or --compact to produce valid JSON Lines output.\n --indent INDENT separate items with newlines and use this number of spaces for indentation\n --indent-after KEY indent after the given key using --indent-after-indentation spaces\n --tab separate items with newlines and use tabs for indentation\n --compact suppress all whitespace separation (most compact)\n --indent-after-width AMOUNT\n set the width of the output line when --indent-after is active\n --indent-after-indentation AMOUNT\n use this number of spaces for indentation when --indent-after is active\n```\n\nexcept 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.\n\n```bash\n$ python -m custom_json_encoder demo.json --indent-after menuitem --indent-after-width 50 --indent-after-indentation 4\n{\n \"menu\": {\"id\": \"file\", \"value\": \"File\",\n \"popup\": {\"menuitem\": [\n {\"value\": \"New\", \"onclick\":\n \"CreateNewDoc()\"},\n {\"value\": \"Open\", \"onclick\":\n \"OpenDoc()\"},\n {\"value\": \"Close\", \"onclick\":\n \"CloseDoc()\"}\n ]}}\n}\n```\n",
"bugtrack_url": null,
"license": "BSD 3-Clause Clear License",
"summary": "A JSON encoder that allows customizing the indentation based on the content and",
"version": "0.5.0",
"split_keywords": [
"json",
"encoder",
"pretty-print",
"width"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "37cc91865d196781941528eb4226e67bfc968b24c254d3530156d7be64bbf36a",
"md5": "e862c72c20364fe69fd49087a5c82880",
"sha256": "db2bfac6409731c3a8e2740e289ba0e5d7b9024157be88adc9edead6af065056"
},
"downloads": -1,
"filename": "custom_json_encoder-0.5.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e862c72c20364fe69fd49087a5c82880",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 8183,
"upload_time": "2023-01-26T15:15:37",
"upload_time_iso_8601": "2023-01-26T15:15:37.407558Z",
"url": "https://files.pythonhosted.org/packages/37/cc/91865d196781941528eb4226e67bfc968b24c254d3530156d7be64bbf36a/custom_json_encoder-0.5.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "4ee497944936e71b2fe644e4f9c9b7a66a4c980714f61c008090e4c3e4700f86",
"md5": "57aa7ccf619bfc15fbb17d7702b489b7",
"sha256": "da455020dc57dd80c0bfa125d23bff2905cc49b6fc92e157bb72c8b68e371bdf"
},
"downloads": -1,
"filename": "custom_json_encoder-0.5.0.tar.gz",
"has_sig": false,
"md5_digest": "57aa7ccf619bfc15fbb17d7702b489b7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 6590,
"upload_time": "2023-01-26T15:15:38",
"upload_time_iso_8601": "2023-01-26T15:15:38.639981Z",
"url": "https://files.pythonhosted.org/packages/4e/e4/97944936e71b2fe644e4f9c9b7a66a4c980714f61c008090e4c3e4700f86/custom_json_encoder-0.5.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-26 15:15:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "Kerrigan29a",
"github_project": "py_custom_json_encoder",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "custom-json-encoder"
}