jsonreflow 0.4.0__py3-none-any.whl
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/__init__.py +5 -0
- jsonreflow/cli.py +46 -0
- jsonreflow/reflow.py +59 -0
- jsonreflow-0.4.0.dist-info/METADATA +51 -0
- jsonreflow-0.4.0.dist-info/RECORD +8 -0
- jsonreflow-0.4.0.dist-info/WHEEL +4 -0
- jsonreflow-0.4.0.dist-info/entry_points.txt +2 -0
- jsonreflow-0.4.0.dist-info/licenses/LICENSE.txt +21 -0
jsonreflow/__init__.py
ADDED
jsonreflow/cli.py
ADDED
|
@@ -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()
|
jsonreflow/reflow.py
ADDED
|
@@ -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,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,8 @@
|
|
|
1
|
+
jsonreflow/__init__.py,sha256=rAFqZy-dSA1mf2i6hPJ6QmXGPAZR3RmKUcosAn-OWbs,141
|
|
2
|
+
jsonreflow/cli.py,sha256=SvIl8EDZw6cQB-Cni-ZEFlfbceYOiEy0-MDPPENDG6c,1331
|
|
3
|
+
jsonreflow/reflow.py,sha256=-MUHZ020Oy3fSZs8UXpsVQDCznY-dRaZmSiYyuf_pYc,2100
|
|
4
|
+
jsonreflow-0.4.0.dist-info/METADATA,sha256=Fam8xuoT61dBVPtC1FDguwoZFZCS1OoIa8PBip7amYg,1813
|
|
5
|
+
jsonreflow-0.4.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
6
|
+
jsonreflow-0.4.0.dist-info/entry_points.txt,sha256=UbNM5UCPSrn70prnh3AeirzkshMhys1Bv3XwQ1ueNhI,51
|
|
7
|
+
jsonreflow-0.4.0.dist-info/licenses/LICENSE.txt,sha256=dVpx0cdQf9FvNaZj3OkAvWy98QknX9DXGnnPjNsphyQ,1072
|
|
8
|
+
jsonreflow-0.4.0.dist-info/RECORD,,
|
|
@@ -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.
|