jsonreflow 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jsonreflow-0.4.0/.gitignore +223 -0
- jsonreflow-0.4.0/LICENSE.txt +21 -0
- jsonreflow-0.4.0/PKG-INFO +51 -0
- jsonreflow-0.4.0/README.md +31 -0
- jsonreflow-0.4.0/pyproject.toml +70 -0
- jsonreflow-0.4.0/src/jsonreflow/__init__.py +5 -0
- jsonreflow-0.4.0/src/jsonreflow/cli.py +46 -0
- jsonreflow-0.4.0/src/jsonreflow/reflow.py +59 -0
- jsonreflow-0.4.0/tests/test_jsonreflow.py +499 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
# Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
# poetry.lock
|
|
109
|
+
# poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
# pdm.lock
|
|
116
|
+
# pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
# pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# Redis
|
|
135
|
+
*.rdb
|
|
136
|
+
*.aof
|
|
137
|
+
*.pid
|
|
138
|
+
|
|
139
|
+
# RabbitMQ
|
|
140
|
+
mnesia/
|
|
141
|
+
rabbitmq/
|
|
142
|
+
rabbitmq-data/
|
|
143
|
+
|
|
144
|
+
# ActiveMQ
|
|
145
|
+
activemq-data/
|
|
146
|
+
|
|
147
|
+
# SageMath parsed files
|
|
148
|
+
*.sage.py
|
|
149
|
+
|
|
150
|
+
# Environments
|
|
151
|
+
.env
|
|
152
|
+
.envrc
|
|
153
|
+
.venv
|
|
154
|
+
env/
|
|
155
|
+
venv/
|
|
156
|
+
ENV/
|
|
157
|
+
env.bak/
|
|
158
|
+
venv.bak/
|
|
159
|
+
|
|
160
|
+
# Spyder project settings
|
|
161
|
+
.spyderproject
|
|
162
|
+
.spyproject
|
|
163
|
+
|
|
164
|
+
# Rope project settings
|
|
165
|
+
.ropeproject
|
|
166
|
+
|
|
167
|
+
# mkdocs documentation
|
|
168
|
+
/site
|
|
169
|
+
|
|
170
|
+
# mypy
|
|
171
|
+
.mypy_cache/
|
|
172
|
+
.dmypy.json
|
|
173
|
+
dmypy.json
|
|
174
|
+
|
|
175
|
+
# Pyre type checker
|
|
176
|
+
.pyre/
|
|
177
|
+
|
|
178
|
+
# pytype static type analyzer
|
|
179
|
+
.pytype/
|
|
180
|
+
|
|
181
|
+
# Cython debug symbols
|
|
182
|
+
cython_debug/
|
|
183
|
+
|
|
184
|
+
# PyCharm
|
|
185
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
186
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
188
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
189
|
+
# .idea/
|
|
190
|
+
|
|
191
|
+
# Abstra
|
|
192
|
+
# Abstra is an AI-powered process automation framework.
|
|
193
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
194
|
+
# Learn more at https://abstra.io/docs
|
|
195
|
+
.abstra/
|
|
196
|
+
|
|
197
|
+
# Visual Studio Code
|
|
198
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
199
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
200
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
201
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
202
|
+
.vscode/
|
|
203
|
+
|
|
204
|
+
# Ruff stuff:
|
|
205
|
+
.ruff_cache/
|
|
206
|
+
|
|
207
|
+
# PyPI configuration file
|
|
208
|
+
.pypirc
|
|
209
|
+
|
|
210
|
+
# Marimo
|
|
211
|
+
marimo/_static/
|
|
212
|
+
marimo/_lsp/
|
|
213
|
+
__marimo__/
|
|
214
|
+
|
|
215
|
+
# Streamlit
|
|
216
|
+
.streamlit/secrets.toml
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
# Generic temporary files and folders
|
|
220
|
+
tmp/
|
|
221
|
+
temp/
|
|
222
|
+
tmp-*
|
|
223
|
+
temp-*
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Stefaan Lippens
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jsonreflow
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: Indented JSON, but with one-line arrays or objects if they fit a desired line length.
|
|
5
|
+
Project-URL: Repository, https://github.com/soxofaan/jsonreflow
|
|
6
|
+
Project-URL: Changelog, https://github.com/soxofaan/jsonreflow/blob/main/CHANGELOG.md
|
|
7
|
+
Author-email: Stefaan Lippens <soxofaan@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE.txt
|
|
10
|
+
Keywords: JSON,serialization
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Topic :: File Formats :: JSON
|
|
15
|
+
Classifier: Topic :: Utilities
|
|
16
|
+
Requires-Python: >=3.8
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest>=8.3; extra == 'dev'
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# JSON Reflow
|
|
23
|
+
|
|
24
|
+
Python library and CLI tool to reflow JSON files and streams,
|
|
25
|
+
to allow a better compromise between
|
|
26
|
+
|
|
27
|
+
- compactness:
|
|
28
|
+
try to fit short arrays and objects on a single line,
|
|
29
|
+
within a given line length limit
|
|
30
|
+
- human readability:
|
|
31
|
+
indented JSON for larger constructs otherwise.
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
## The problem
|
|
36
|
+
|
|
37
|
+
Standard JSON serialization tools typically only support two extremes:
|
|
38
|
+
|
|
39
|
+
- put everything on a single line:
|
|
40
|
+
the most compact, but very poor for human readability
|
|
41
|
+
|
|
42
|
+
- spread out each and every array item and object property on its own line
|
|
43
|
+
with appropriate indentation to visualize the structure.
|
|
44
|
+
This is easier for humans to parse visually
|
|
45
|
+
(which is why it is often referred to as "prettifying" or "beautifying"),
|
|
46
|
+
but for larger documents, this easily becomes unwieldy, "too vertical"
|
|
47
|
+
and very space-inefficient because of all the repeated indentation.
|
|
48
|
+
|
|
49
|
+
JSON Reflow allows to find a better compromise:
|
|
50
|
+
only serialize arrays or objects over multiple lines
|
|
51
|
+
if the single-line approach would exceed a given line length.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
|
|
2
|
+
# JSON Reflow
|
|
3
|
+
|
|
4
|
+
Python library and CLI tool to reflow JSON files and streams,
|
|
5
|
+
to allow a better compromise between
|
|
6
|
+
|
|
7
|
+
- compactness:
|
|
8
|
+
try to fit short arrays and objects on a single line,
|
|
9
|
+
within a given line length limit
|
|
10
|
+
- human readability:
|
|
11
|
+
indented JSON for larger constructs otherwise.
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## The problem
|
|
16
|
+
|
|
17
|
+
Standard JSON serialization tools typically only support two extremes:
|
|
18
|
+
|
|
19
|
+
- put everything on a single line:
|
|
20
|
+
the most compact, but very poor for human readability
|
|
21
|
+
|
|
22
|
+
- spread out each and every array item and object property on its own line
|
|
23
|
+
with appropriate indentation to visualize the structure.
|
|
24
|
+
This is easier for humans to parse visually
|
|
25
|
+
(which is why it is often referred to as "prettifying" or "beautifying"),
|
|
26
|
+
but for larger documents, this easily becomes unwieldy, "too vertical"
|
|
27
|
+
and very space-inefficient because of all the repeated indentation.
|
|
28
|
+
|
|
29
|
+
JSON Reflow allows to find a better compromise:
|
|
30
|
+
only serialize arrays or objects over multiple lines
|
|
31
|
+
if the single-line approach would exceed a given line length.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling >= 1.26"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
[project]
|
|
7
|
+
name = "jsonreflow"
|
|
8
|
+
dynamic = ["version"]
|
|
9
|
+
description = "Indented JSON, but with one-line arrays or objects if they fit a desired line length."
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
requires-python = ">= 3.8"
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "Stefaan Lippens", email = "soxofaan@gmail.com"},
|
|
14
|
+
]
|
|
15
|
+
license = "MIT"
|
|
16
|
+
license-files = ["LICENSE.txt"]
|
|
17
|
+
keywords = ["JSON", "serialization"]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Operating System :: OS Independent",
|
|
21
|
+
"License :: OSI Approved :: MIT License",
|
|
22
|
+
"Topic :: File Formats :: JSON",
|
|
23
|
+
"Topic :: Utilities",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Repository = "https://github.com/soxofaan/jsonreflow"
|
|
29
|
+
Changelog = "https://github.com/soxofaan/jsonreflow/blob/main/CHANGELOG.md"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
[project.optional-dependencies]
|
|
33
|
+
dev = [
|
|
34
|
+
"pytest>=8.3",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
[project.scripts]
|
|
39
|
+
jsonreflow = "jsonreflow.cli:main"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
[tool.hatch.build.targets.wheel]
|
|
44
|
+
packages = ["src/jsonreflow"]
|
|
45
|
+
|
|
46
|
+
[tool.hatch.build.targets.sdist]
|
|
47
|
+
include = [
|
|
48
|
+
"src/",
|
|
49
|
+
"tests/",
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
[tool.hatch.version]
|
|
53
|
+
path = "src/jsonreflow/__init__.py"
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
[tool.ruff.lint]
|
|
57
|
+
select = [
|
|
58
|
+
# pycodestyle
|
|
59
|
+
"E",
|
|
60
|
+
# Pyflakes
|
|
61
|
+
"F",
|
|
62
|
+
# pyupgrade
|
|
63
|
+
"UP",
|
|
64
|
+
# flake8-bugbear
|
|
65
|
+
"B",
|
|
66
|
+
# flake8-simplify
|
|
67
|
+
"SIM",
|
|
68
|
+
# isort
|
|
69
|
+
"I",
|
|
70
|
+
]
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import json
|
|
3
|
+
|
|
4
|
+
from jsonreflow.reflow import MAX_WIDTH_DEFAULT, dumps, reflow_iter
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def main():
|
|
8
|
+
cli = argparse.ArgumentParser(description="Reflow JSON to fit within a given width")
|
|
9
|
+
cli.add_argument(
|
|
10
|
+
"input",
|
|
11
|
+
nargs="?",
|
|
12
|
+
type=argparse.FileType("r", encoding="utf-8"),
|
|
13
|
+
default="-",
|
|
14
|
+
help="Input JSON file (defaults to stdin)",
|
|
15
|
+
)
|
|
16
|
+
cli.add_argument(
|
|
17
|
+
"--assume-formatted",
|
|
18
|
+
action="store_true",
|
|
19
|
+
help="""
|
|
20
|
+
Assume the input is already properly formatted as multiline, indented JSON.
|
|
21
|
+
Allows to reflow without parsing the JSON, which is more efficient,
|
|
22
|
+
and avoids subtle re-encoding issues.
|
|
23
|
+
""",
|
|
24
|
+
)
|
|
25
|
+
cli.add_argument(
|
|
26
|
+
"-w",
|
|
27
|
+
"--max-width",
|
|
28
|
+
type=int,
|
|
29
|
+
default=MAX_WIDTH_DEFAULT,
|
|
30
|
+
help=f"Maximum line width to reflow for (default: {MAX_WIDTH_DEFAULT})",
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
args = cli.parse_args()
|
|
34
|
+
if args.assume_formatted:
|
|
35
|
+
for line in reflow_iter(
|
|
36
|
+
(s.rstrip() for s in args.input.readlines()), max_width=args.max_width
|
|
37
|
+
):
|
|
38
|
+
print(line)
|
|
39
|
+
else:
|
|
40
|
+
data = json.load(args.input)
|
|
41
|
+
# TODO: integrate with streaming JSON encoding
|
|
42
|
+
print(dumps(data, max_width=args.max_width))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
if __name__ == "__main__":
|
|
46
|
+
main()
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from typing import Iterable, Iterator, List
|
|
3
|
+
|
|
4
|
+
MAX_WIDTH_DEFAULT = 80
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def reflow_iter(
|
|
8
|
+
lines: Iterable[str], max_width: int = MAX_WIDTH_DEFAULT
|
|
9
|
+
) -> Iterator[str]:
|
|
10
|
+
# Stack of buffers of possibly foldable levels.
|
|
11
|
+
# Note that only the currently deepest levels are tracked,
|
|
12
|
+
# levels more towards the top that are already collapsed
|
|
13
|
+
# are not represented here anymore.
|
|
14
|
+
buffer_stack: List[List[str]] = []
|
|
15
|
+
|
|
16
|
+
for line in lines:
|
|
17
|
+
stripped = line.strip()
|
|
18
|
+
|
|
19
|
+
if stripped.endswith("{") or stripped.endswith("["):
|
|
20
|
+
# Start a new level on the stack
|
|
21
|
+
buffer_stack.append([])
|
|
22
|
+
|
|
23
|
+
# Depending on whether we are at a possibly foldable level:
|
|
24
|
+
# yield (collapse) or try folding
|
|
25
|
+
if not buffer_stack:
|
|
26
|
+
yield line
|
|
27
|
+
else:
|
|
28
|
+
buffer_stack[-1].append(line)
|
|
29
|
+
|
|
30
|
+
if stripped in {"}", "},", "]", "],"}:
|
|
31
|
+
# Close current level: time to see if we can fold to one-liner
|
|
32
|
+
# or have to collapse to multi-line
|
|
33
|
+
closed = buffer_stack.pop()
|
|
34
|
+
folded = (
|
|
35
|
+
closed[0]
|
|
36
|
+
+ " ".join(s.strip() for s in closed[1:-1])
|
|
37
|
+
+ closed[-1].strip()
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
if len(folded) > max_width:
|
|
41
|
+
# Current level doesn't fit: collapse all levels we've been tracking
|
|
42
|
+
for level in buffer_stack:
|
|
43
|
+
yield from level
|
|
44
|
+
buffer_stack = []
|
|
45
|
+
yield from closed
|
|
46
|
+
else:
|
|
47
|
+
# Move folded result up one level (unless it's collapsed already)
|
|
48
|
+
if buffer_stack:
|
|
49
|
+
buffer_stack[-1].append(folded)
|
|
50
|
+
else:
|
|
51
|
+
yield folded
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def reflow(encoded: str, max_width: int = MAX_WIDTH_DEFAULT) -> str:
|
|
55
|
+
return "\n".join(reflow_iter(encoded.split("\n"), max_width=max_width))
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def dumps(obj, max_width: int = MAX_WIDTH_DEFAULT) -> str:
|
|
59
|
+
return reflow(json.dumps(obj=obj, indent=2), max_width=max_width)
|
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import textwrap
|
|
3
|
+
from typing import Iterable, List
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from jsonreflow import dumps, reflow_iter
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def test_dumps_none():
|
|
11
|
+
assert dumps(None) == "null"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def test_dumps_int():
|
|
15
|
+
assert dumps(123) == "123"
|
|
16
|
+
assert dumps(-123) == "-123"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_dumps_float():
|
|
20
|
+
assert dumps(123.5) == "123.5"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_dumps_bool():
|
|
24
|
+
assert dumps(True) == "true"
|
|
25
|
+
assert dumps(False) == "false"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def test_dumps_string():
|
|
29
|
+
assert dumps("hello world") == '"hello world"'
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_dumps_empty_list():
|
|
33
|
+
assert dumps([]) == "[]"
|
|
34
|
+
assert dumps(()) == "[]"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_dumps_empty_dict():
|
|
38
|
+
assert dumps({}) == "{}"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def test_dumps_flat_list():
|
|
42
|
+
assert dumps([1, 2, 3, 4, 5]) == "[1, 2, 3, 4, 5]"
|
|
43
|
+
assert dumps([1, 2, 3, 4, 5], max_width=10) == "[\n 1,\n 2,\n 3,\n 4,\n 5\n]"
|
|
44
|
+
assert dumps([1, 2, 3, 4, 5], max_width=1) == "[\n 1,\n 2,\n 3,\n 4,\n 5\n]"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_dumps_flat_dict():
|
|
48
|
+
assert (
|
|
49
|
+
dumps({"name": "alice", "color": "green"})
|
|
50
|
+
== '{"name": "alice", "color": "green"}'
|
|
51
|
+
)
|
|
52
|
+
assert (
|
|
53
|
+
dumps({"name": "alice", "color": "green"}, max_width=10)
|
|
54
|
+
== '{\n "name": "alice",\n "color": "green"\n}'
|
|
55
|
+
)
|
|
56
|
+
assert (
|
|
57
|
+
dumps({"name": "alice", "color": "green"}, max_width=1)
|
|
58
|
+
== '{\n "name": "alice",\n "color": "green"\n}'
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@pytest.mark.parametrize(
|
|
63
|
+
["max_width", "obj", "expected"],
|
|
64
|
+
[
|
|
65
|
+
(
|
|
66
|
+
80,
|
|
67
|
+
{"five": list(range(5)), "ten": list(range(10))},
|
|
68
|
+
'{"five": [0, 1, 2, 3, 4], "ten": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}',
|
|
69
|
+
),
|
|
70
|
+
(
|
|
71
|
+
40,
|
|
72
|
+
{"five": list(range(5)), "ten": list(range(10))},
|
|
73
|
+
"""\
|
|
74
|
+
{
|
|
75
|
+
"five": [0, 1, 2, 3, 4],
|
|
76
|
+
"ten": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
77
|
+
}""",
|
|
78
|
+
),
|
|
79
|
+
(
|
|
80
|
+
30,
|
|
81
|
+
{"five": list(range(5)), "ten": list(range(10))},
|
|
82
|
+
"""\
|
|
83
|
+
{
|
|
84
|
+
"five": [0, 1, 2, 3, 4],
|
|
85
|
+
"ten": [
|
|
86
|
+
0,
|
|
87
|
+
1,
|
|
88
|
+
2,
|
|
89
|
+
3,
|
|
90
|
+
4,
|
|
91
|
+
5,
|
|
92
|
+
6,
|
|
93
|
+
7,
|
|
94
|
+
8,
|
|
95
|
+
9
|
|
96
|
+
]
|
|
97
|
+
}""",
|
|
98
|
+
),
|
|
99
|
+
(
|
|
100
|
+
80,
|
|
101
|
+
{str(x): chr(97 + x) * x for x in range(5)},
|
|
102
|
+
'{"0": "", "1": "b", "2": "cc", "3": "ddd", "4": "eeee"}',
|
|
103
|
+
),
|
|
104
|
+
(
|
|
105
|
+
40,
|
|
106
|
+
{str(x): chr(97 + x) * x for x in range(5)},
|
|
107
|
+
"""\
|
|
108
|
+
{
|
|
109
|
+
"0": "",
|
|
110
|
+
"1": "b",
|
|
111
|
+
"2": "cc",
|
|
112
|
+
"3": "ddd",
|
|
113
|
+
"4": "eeee"
|
|
114
|
+
}""",
|
|
115
|
+
),
|
|
116
|
+
],
|
|
117
|
+
)
|
|
118
|
+
def test_dumps_listings(max_width, obj, expected):
|
|
119
|
+
expected = textwrap.dedent(expected)
|
|
120
|
+
assert dumps(obj, max_width=max_width) == expected
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@pytest.mark.parametrize(
|
|
124
|
+
["max_width", "obj", "expected"],
|
|
125
|
+
[
|
|
126
|
+
(
|
|
127
|
+
80,
|
|
128
|
+
{
|
|
129
|
+
"query": "get stuff",
|
|
130
|
+
"results": {
|
|
131
|
+
"count": 5,
|
|
132
|
+
"data": [
|
|
133
|
+
{"id": 1, "name": "Alice", "payments": None},
|
|
134
|
+
{"id": 23, "name": "Bob", "payments": [100, 200]},
|
|
135
|
+
{
|
|
136
|
+
"id": 3000,
|
|
137
|
+
"name": "Carol",
|
|
138
|
+
"status": "premium",
|
|
139
|
+
"payments": [1000, 3000, 2000, 2, -5],
|
|
140
|
+
},
|
|
141
|
+
{"id": 44, "name": "Dave", "payments": [1, 5]},
|
|
142
|
+
{
|
|
143
|
+
"id": 555,
|
|
144
|
+
"name": "Eric",
|
|
145
|
+
"payments": [44, {"price": 666, "currency": "tulip bulbs"}],
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
},
|
|
149
|
+
"_id": "123kthxbye",
|
|
150
|
+
},
|
|
151
|
+
"""\
|
|
152
|
+
{
|
|
153
|
+
"query": "get stuff",
|
|
154
|
+
"results": {
|
|
155
|
+
"count": 5,
|
|
156
|
+
"data": [
|
|
157
|
+
{"id": 1, "name": "Alice", "payments": null},
|
|
158
|
+
{"id": 23, "name": "Bob", "payments": [100, 200]},
|
|
159
|
+
{
|
|
160
|
+
"id": 3000,
|
|
161
|
+
"name": "Carol",
|
|
162
|
+
"status": "premium",
|
|
163
|
+
"payments": [1000, 3000, 2000, 2, -5]
|
|
164
|
+
},
|
|
165
|
+
{"id": 44, "name": "Dave", "payments": [1, 5]},
|
|
166
|
+
{
|
|
167
|
+
"id": 555,
|
|
168
|
+
"name": "Eric",
|
|
169
|
+
"payments": [44, {"price": 666, "currency": "tulip bulbs"}]
|
|
170
|
+
}
|
|
171
|
+
]
|
|
172
|
+
},
|
|
173
|
+
"_id": "123kthxbye"
|
|
174
|
+
}""",
|
|
175
|
+
),
|
|
176
|
+
(
|
|
177
|
+
120,
|
|
178
|
+
{
|
|
179
|
+
"query": "get stuff",
|
|
180
|
+
"results": {
|
|
181
|
+
"count": 5,
|
|
182
|
+
"data": [
|
|
183
|
+
{"id": 1, "name": "Alice", "payments": None},
|
|
184
|
+
{"id": 23, "name": "Bob", "payments": [100, 200]},
|
|
185
|
+
{
|
|
186
|
+
"id": 3000,
|
|
187
|
+
"name": "Carol",
|
|
188
|
+
"status": "premium",
|
|
189
|
+
"payments": [1000, 3000, 2000, 2, -5],
|
|
190
|
+
},
|
|
191
|
+
{"id": 44, "name": "Dave", "payments": [1, 5]},
|
|
192
|
+
{
|
|
193
|
+
"id": 555,
|
|
194
|
+
"name": "Eric",
|
|
195
|
+
"payments": [44, {"price": 666, "currency": "tulip bulbs"}],
|
|
196
|
+
},
|
|
197
|
+
],
|
|
198
|
+
},
|
|
199
|
+
"_id": "123kthxbye",
|
|
200
|
+
},
|
|
201
|
+
"""\
|
|
202
|
+
{
|
|
203
|
+
"query": "get stuff",
|
|
204
|
+
"results": {
|
|
205
|
+
"count": 5,
|
|
206
|
+
"data": [
|
|
207
|
+
{"id": 1, "name": "Alice", "payments": null},
|
|
208
|
+
{"id": 23, "name": "Bob", "payments": [100, 200]},
|
|
209
|
+
{"id": 3000, "name": "Carol", "status": "premium", "payments": [1000, 3000, 2000, 2, -5]},
|
|
210
|
+
{"id": 44, "name": "Dave", "payments": [1, 5]},
|
|
211
|
+
{"id": 555, "name": "Eric", "payments": [44, {"price": 666, "currency": "tulip bulbs"}]}
|
|
212
|
+
]
|
|
213
|
+
},
|
|
214
|
+
"_id": "123kthxbye"
|
|
215
|
+
}""", # noqa: E501
|
|
216
|
+
),
|
|
217
|
+
(
|
|
218
|
+
80,
|
|
219
|
+
{
|
|
220
|
+
"a": {
|
|
221
|
+
"bb": {
|
|
222
|
+
"ccc": {
|
|
223
|
+
"dddd": {
|
|
224
|
+
"eeeee": {
|
|
225
|
+
"ffffff": "foo",
|
|
226
|
+
},
|
|
227
|
+
}
|
|
228
|
+
},
|
|
229
|
+
"CCC": {
|
|
230
|
+
"D": 13,
|
|
231
|
+
"DD": 133,
|
|
232
|
+
"DDD": 1333,
|
|
233
|
+
},
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
"""\
|
|
238
|
+
{
|
|
239
|
+
"a": {
|
|
240
|
+
"bb": {
|
|
241
|
+
"ccc": {"dddd": {"eeeee": {"ffffff": "foo"}}},
|
|
242
|
+
"CCC": {"D": 13, "DD": 133, "DDD": 1333}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}""",
|
|
246
|
+
),
|
|
247
|
+
(
|
|
248
|
+
120,
|
|
249
|
+
{
|
|
250
|
+
"a": {
|
|
251
|
+
"bb": {
|
|
252
|
+
"ccc": {
|
|
253
|
+
"dddd": {
|
|
254
|
+
"eeeee": {
|
|
255
|
+
"ffffff": "foo",
|
|
256
|
+
},
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
"CCC": {
|
|
260
|
+
"D": 13,
|
|
261
|
+
"DD": 133,
|
|
262
|
+
"DDD": 1333,
|
|
263
|
+
},
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
'{"a": {"bb": {"ccc": {"dddd": {"eeeee": {"ffffff": "foo"}}}, "CCC": {"D": 13, "DD": 133, "DDD": 1333}}}}', # noqa: E501
|
|
268
|
+
),
|
|
269
|
+
(
|
|
270
|
+
40,
|
|
271
|
+
{
|
|
272
|
+
"a": {
|
|
273
|
+
"bb": {
|
|
274
|
+
"ccc": {
|
|
275
|
+
"dddd": {
|
|
276
|
+
"eeeee": {
|
|
277
|
+
"ffffff": "foo",
|
|
278
|
+
},
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
"CCC": {
|
|
282
|
+
"D": 13,
|
|
283
|
+
"DD": 133,
|
|
284
|
+
"DDD": 1333,
|
|
285
|
+
},
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
},
|
|
289
|
+
"""\
|
|
290
|
+
{
|
|
291
|
+
"a": {
|
|
292
|
+
"bb": {
|
|
293
|
+
"ccc": {
|
|
294
|
+
"dddd": {
|
|
295
|
+
"eeeee": {"ffffff": "foo"}
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
"CCC": {
|
|
299
|
+
"D": 13,
|
|
300
|
+
"DD": 133,
|
|
301
|
+
"DDD": 1333
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}""",
|
|
306
|
+
),
|
|
307
|
+
],
|
|
308
|
+
)
|
|
309
|
+
def test_dumps_deep_nesting(obj, max_width, expected):
|
|
310
|
+
expected = textwrap.dedent(expected)
|
|
311
|
+
assert dumps(obj, max_width=max_width) == expected
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
class TrackingIterator:
|
|
315
|
+
"""
|
|
316
|
+
Wrapper for an iterable of strings, to keep track of what has been consumed already.
|
|
317
|
+
"""
|
|
318
|
+
|
|
319
|
+
def __init__(self, items: Iterable[str]):
|
|
320
|
+
self._items = iter(items)
|
|
321
|
+
self.consumed = []
|
|
322
|
+
self._report_index = 0
|
|
323
|
+
|
|
324
|
+
def __iter__(self):
|
|
325
|
+
return self
|
|
326
|
+
|
|
327
|
+
def __next__(self):
|
|
328
|
+
item = next(self._items)
|
|
329
|
+
self.consumed.append(item)
|
|
330
|
+
return item
|
|
331
|
+
|
|
332
|
+
def new_consumed(self) -> List[str]:
|
|
333
|
+
"""Report newly consumed items since the last call."""
|
|
334
|
+
index = self._report_index
|
|
335
|
+
self._report_index = len(self.consumed)
|
|
336
|
+
return self.consumed[index:]
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def test_tracking_iterator_basic():
|
|
340
|
+
iterator = TrackingIterator(["a", "b", "c"])
|
|
341
|
+
|
|
342
|
+
assert iterator.consumed == []
|
|
343
|
+
assert iterator.new_consumed() == []
|
|
344
|
+
|
|
345
|
+
assert next(iterator) == "a"
|
|
346
|
+
assert iterator.consumed == ["a"]
|
|
347
|
+
assert iterator.new_consumed() == ["a"]
|
|
348
|
+
|
|
349
|
+
assert next(iterator) == "b"
|
|
350
|
+
assert iterator.consumed == ["a", "b"]
|
|
351
|
+
|
|
352
|
+
assert next(iterator) == "c"
|
|
353
|
+
assert iterator.consumed == ["a", "b", "c"]
|
|
354
|
+
assert iterator.new_consumed() == ["b", "c"]
|
|
355
|
+
assert iterator.new_consumed() == []
|
|
356
|
+
|
|
357
|
+
with pytest.raises(StopIteration):
|
|
358
|
+
next(iterator)
|
|
359
|
+
assert iterator.consumed == ["a", "b", "c"]
|
|
360
|
+
assert iterator.new_consumed() == []
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def test_reflow_iter_flushing_simple_one_line():
|
|
364
|
+
"""
|
|
365
|
+
Trivial case: everything fits on one line,
|
|
366
|
+
so we should consume all input lines immediately.
|
|
367
|
+
"""
|
|
368
|
+
data = {"color": "green", "shape": "square"}
|
|
369
|
+
input_lines = TrackingIterator(json.dumps(data, indent=2).split("\n"))
|
|
370
|
+
|
|
371
|
+
folded = reflow_iter(input_lines, max_width=80)
|
|
372
|
+
assert input_lines.consumed == []
|
|
373
|
+
|
|
374
|
+
line = next(folded)
|
|
375
|
+
assert line == '{"color": "green", "shape": "square"}'
|
|
376
|
+
assert input_lines.consumed == [
|
|
377
|
+
"{",
|
|
378
|
+
' "color": "green",',
|
|
379
|
+
' "shape": "square"',
|
|
380
|
+
"}",
|
|
381
|
+
]
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def test_reflow_iter_flushing_simple_multiline():
|
|
385
|
+
"""
|
|
386
|
+
Multi-line result, but just one level,
|
|
387
|
+
so all lines should be consumed immediately.
|
|
388
|
+
"""
|
|
389
|
+
data = {"color": "green", "shape": "square"}
|
|
390
|
+
input_lines = TrackingIterator(json.dumps(data, indent=2).split("\n"))
|
|
391
|
+
|
|
392
|
+
folded = reflow_iter(input_lines, max_width=20)
|
|
393
|
+
assert input_lines.new_consumed() == []
|
|
394
|
+
|
|
395
|
+
assert next(folded) == "{"
|
|
396
|
+
assert input_lines.new_consumed() == [
|
|
397
|
+
"{",
|
|
398
|
+
' "color": "green",',
|
|
399
|
+
' "shape": "square"',
|
|
400
|
+
"}",
|
|
401
|
+
]
|
|
402
|
+
|
|
403
|
+
assert next(folded) == ' "color": "green",'
|
|
404
|
+
assert input_lines.new_consumed() == []
|
|
405
|
+
|
|
406
|
+
assert next(folded) == ' "shape": "square"'
|
|
407
|
+
assert input_lines.new_consumed() == []
|
|
408
|
+
|
|
409
|
+
assert next(folded) == "}"
|
|
410
|
+
assert input_lines.new_consumed() == []
|
|
411
|
+
|
|
412
|
+
with pytest.raises(StopIteration):
|
|
413
|
+
_ = next(folded)
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
def test_reflow_iter_flushing_nested():
|
|
417
|
+
"""Multi-line result with nesting: input is consumed in chunks."""
|
|
418
|
+
data = {"three": list(range(3)), "five": list(range(5)), "ten": list(range(10))}
|
|
419
|
+
input_lines = TrackingIterator(json.dumps(data, indent=2).split("\n"))
|
|
420
|
+
|
|
421
|
+
folded = reflow_iter(input_lines, max_width=25)
|
|
422
|
+
assert input_lines.new_consumed() == []
|
|
423
|
+
|
|
424
|
+
# Top level flush: while "three" would fit on one line, "five" would overflow.
|
|
425
|
+
assert next(folded) == "{"
|
|
426
|
+
assert input_lines.new_consumed() == [
|
|
427
|
+
"{",
|
|
428
|
+
' "three": [',
|
|
429
|
+
" 0,",
|
|
430
|
+
" 1,",
|
|
431
|
+
" 2",
|
|
432
|
+
" ],",
|
|
433
|
+
# TODO: with the next line, it should already be possible to determine
|
|
434
|
+
# that "five" won't fit, and it's already time to flush
|
|
435
|
+
# without further consumption.
|
|
436
|
+
' "five": [',
|
|
437
|
+
" 0,",
|
|
438
|
+
" 1,",
|
|
439
|
+
" 2,",
|
|
440
|
+
" 3,",
|
|
441
|
+
" 4",
|
|
442
|
+
" ],",
|
|
443
|
+
]
|
|
444
|
+
|
|
445
|
+
# "three" fits on one line
|
|
446
|
+
assert next(folded) == ' "three": [0, 1, 2],'
|
|
447
|
+
assert input_lines.new_consumed() == []
|
|
448
|
+
|
|
449
|
+
# "five" doesn't fit, so we get multiple lines
|
|
450
|
+
assert next(folded) == ' "five": ['
|
|
451
|
+
assert input_lines.new_consumed() == []
|
|
452
|
+
|
|
453
|
+
for x in range(4):
|
|
454
|
+
assert next(folded) == f" {x},"
|
|
455
|
+
assert input_lines.new_consumed() == []
|
|
456
|
+
|
|
457
|
+
assert next(folded) == " 4"
|
|
458
|
+
assert input_lines.new_consumed() == []
|
|
459
|
+
|
|
460
|
+
assert next(folded) == " ],"
|
|
461
|
+
assert input_lines.new_consumed() == []
|
|
462
|
+
|
|
463
|
+
# Time for "ten": also doesn't fit. Buffer is empty at this point,
|
|
464
|
+
# so we have to consume a bit too.
|
|
465
|
+
assert next(folded) == ' "ten": ['
|
|
466
|
+
assert input_lines.new_consumed() == [
|
|
467
|
+
' "ten": [',
|
|
468
|
+
" 0,",
|
|
469
|
+
" 1,",
|
|
470
|
+
" 2,",
|
|
471
|
+
" 3,",
|
|
472
|
+
" 4,",
|
|
473
|
+
" 5,",
|
|
474
|
+
" 6,",
|
|
475
|
+
" 7,",
|
|
476
|
+
" 8,",
|
|
477
|
+
" 9",
|
|
478
|
+
" ]",
|
|
479
|
+
]
|
|
480
|
+
|
|
481
|
+
for x in range(9):
|
|
482
|
+
assert next(folded) == f" {x},"
|
|
483
|
+
assert input_lines.new_consumed() == []
|
|
484
|
+
|
|
485
|
+
assert next(folded) == " 9"
|
|
486
|
+
assert input_lines.new_consumed() == []
|
|
487
|
+
|
|
488
|
+
assert next(folded) == " ]"
|
|
489
|
+
assert input_lines.new_consumed() == []
|
|
490
|
+
|
|
491
|
+
# Final closing brace
|
|
492
|
+
assert next(folded) == "}"
|
|
493
|
+
assert input_lines.new_consumed() == [
|
|
494
|
+
"}",
|
|
495
|
+
]
|
|
496
|
+
|
|
497
|
+
with pytest.raises(StopIteration):
|
|
498
|
+
_ = next(folded)
|
|
499
|
+
assert input_lines.new_consumed() == []
|