pyfracturedjson 2.2.2__cp315-cp315-win32.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.
- _rust_wrapper/__init__.py +5 -0
- _rust_wrapper/_rust_wrapper.cp315-win32.pyd +0 -0
- fracturedjson/__init__.py +239 -0
- fracturedjson/_rust_wrapper.pyi +22 -0
- pyfracturedjson-2.2.2.dist-info/METADATA +68 -0
- pyfracturedjson-2.2.2.dist-info/RECORD +9 -0
- pyfracturedjson-2.2.2.dist-info/WHEEL +4 -0
- pyfracturedjson-2.2.2.dist-info/licenses/LICENSE +20 -0
- pyfracturedjson-2.2.2.dist-info/sboms/pyfracturedjson.cyclonedx.json +1405 -0
|
Binary file
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from collections.abc import Iterator
|
|
3
|
+
from typing import Callable
|
|
4
|
+
from typing import TYPE_CHECKING
|
|
5
|
+
|
|
6
|
+
if TYPE_CHECKING:
|
|
7
|
+
from fracturedjson import _rust_wrapper # type: ignore[reportMissingModuleSource]
|
|
8
|
+
else:
|
|
9
|
+
import _rust_wrapper
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Encoder(json.encoder.JSONEncoder):
|
|
13
|
+
def __init__(
|
|
14
|
+
self,
|
|
15
|
+
*,
|
|
16
|
+
skipkeys: bool = False,
|
|
17
|
+
ensure_ascii: bool = True,
|
|
18
|
+
check_circular: bool = True,
|
|
19
|
+
allow_nan: bool = True,
|
|
20
|
+
sort_keys: bool = False,
|
|
21
|
+
indent = 4,
|
|
22
|
+
separators: tuple[str, str] | None = None,
|
|
23
|
+
default: Callable | None = None,
|
|
24
|
+
max_inline_complexity = 2,
|
|
25
|
+
max_compact_array_complexity = 2,
|
|
26
|
+
max_table_row_complexity = 2,
|
|
27
|
+
number_list_alignment= "decimal",
|
|
28
|
+
max_prop_name_padding = 16,
|
|
29
|
+
**kwargs
|
|
30
|
+
):
|
|
31
|
+
super().__init__(
|
|
32
|
+
skipkeys=skipkeys,
|
|
33
|
+
ensure_ascii=ensure_ascii,
|
|
34
|
+
check_circular=check_circular,
|
|
35
|
+
allow_nan=allow_nan,
|
|
36
|
+
sort_keys=sort_keys,
|
|
37
|
+
indent=indent,
|
|
38
|
+
separators=separators,
|
|
39
|
+
default=default
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
if indent is None:
|
|
43
|
+
indent = 4
|
|
44
|
+
self._indent = indent
|
|
45
|
+
self.line_length = kwargs.get("line_length", 120)
|
|
46
|
+
self.max_inline_complexity = max_inline_complexity
|
|
47
|
+
self.max_compact_array_complexity = max_compact_array_complexity
|
|
48
|
+
self.max_table_row_complexity = max_table_row_complexity
|
|
49
|
+
self.number_list_alignment = number_list_alignment
|
|
50
|
+
self.max_prop_name_padding = max_prop_name_padding
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def encode(self, o) -> str:
|
|
55
|
+
unformatted = json.dumps(o)
|
|
56
|
+
formatted = _rust_wrapper.reformat_string(
|
|
57
|
+
unformatted,
|
|
58
|
+
indent=self._indent,
|
|
59
|
+
line_length=self.line_length,
|
|
60
|
+
max_inline_complexity = self.max_inline_complexity,
|
|
61
|
+
max_compact_array_complexity = self.max_compact_array_complexity,
|
|
62
|
+
max_table_row_complexity = self.max_table_row_complexity,
|
|
63
|
+
number_list_alignment = self.number_list_alignment,
|
|
64
|
+
max_prop_name_padding = self.max_prop_name_padding
|
|
65
|
+
)
|
|
66
|
+
return formatted
|
|
67
|
+
|
|
68
|
+
def iterencode(self, o, *args, **kwargs) -> Iterator[str]:
|
|
69
|
+
return iter(self.encode(o))
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def dump(
|
|
73
|
+
obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True,
|
|
74
|
+
allow_nan=True, cls=None, indent=None, separators=None,
|
|
75
|
+
default=None, sort_keys=False, **kw
|
|
76
|
+
):
|
|
77
|
+
"""Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
|
|
78
|
+
``.write()``-supporting file-like object).
|
|
79
|
+
|
|
80
|
+
If ``skipkeys`` is true then ``dict`` keys that are not basic types
|
|
81
|
+
(``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
|
|
82
|
+
instead of raising a ``TypeError``.
|
|
83
|
+
|
|
84
|
+
If ``ensure_ascii`` is false, then the strings written to ``fp`` can
|
|
85
|
+
contain non-ASCII characters if they appear in strings contained in
|
|
86
|
+
``obj``. Otherwise, all such characters are escaped in JSON strings.
|
|
87
|
+
|
|
88
|
+
If ``check_circular`` is false, then the circular reference check
|
|
89
|
+
for container types will be skipped and a circular reference will
|
|
90
|
+
result in an ``RecursionError`` (or worse).
|
|
91
|
+
|
|
92
|
+
If ``allow_nan`` is false, then it will be a ``ValueError`` to
|
|
93
|
+
serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)
|
|
94
|
+
in strict compliance of the JSON specification, instead of using the
|
|
95
|
+
JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
|
|
96
|
+
|
|
97
|
+
If ``indent`` is a non-negative integer, then JSON array elements and
|
|
98
|
+
object members will be pretty-printed with that indent level. An indent
|
|
99
|
+
level of 0 will only insert newlines. ``None`` is the most compact
|
|
100
|
+
representation.
|
|
101
|
+
|
|
102
|
+
If specified, ``separators`` should be an ``(item_separator, key_separator)``
|
|
103
|
+
tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
|
|
104
|
+
``(',', ': ')`` otherwise. To get the most compact JSON representation,
|
|
105
|
+
you should specify ``(',', ':')`` to eliminate whitespace.
|
|
106
|
+
|
|
107
|
+
``default(obj)`` is a function that should return a serializable version
|
|
108
|
+
of obj or raise TypeError. The default simply raises TypeError.
|
|
109
|
+
|
|
110
|
+
If *sort_keys* is true (default: ``False``), then the output of
|
|
111
|
+
dictionaries will be sorted by key.
|
|
112
|
+
|
|
113
|
+
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
|
|
114
|
+
``.default()`` method to serialize additional types), specify it with
|
|
115
|
+
the ``cls`` kwarg; otherwise ``FracturedJSON.Encoder`` is used.
|
|
116
|
+
|
|
117
|
+
"""
|
|
118
|
+
return json.dump(
|
|
119
|
+
obj, fp,
|
|
120
|
+
cls=Encoder if cls is None else cls,
|
|
121
|
+
skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular,
|
|
122
|
+
allow_nan=allow_nan, indent=indent, separators=separators,
|
|
123
|
+
default=default, sort_keys=sort_keys,
|
|
124
|
+
**kw
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,
|
|
128
|
+
allow_nan=True, cls=None, indent=None, separators=None,
|
|
129
|
+
default=None, sort_keys=False, **kw):
|
|
130
|
+
"""Serialize ``obj`` to a JSON formatted ``str``.
|
|
131
|
+
|
|
132
|
+
If ``skipkeys`` is true then ``dict`` keys that are not basic types
|
|
133
|
+
(``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
|
|
134
|
+
instead of raising a ``TypeError``.
|
|
135
|
+
|
|
136
|
+
If ``ensure_ascii`` is false, then the return value can contain non-ASCII
|
|
137
|
+
characters if they appear in strings contained in ``obj``. Otherwise, all
|
|
138
|
+
such characters are escaped in JSON strings.
|
|
139
|
+
|
|
140
|
+
If ``check_circular`` is false, then the circular reference check
|
|
141
|
+
for container types will be skipped and a circular reference will
|
|
142
|
+
result in an ``RecursionError`` (or worse).
|
|
143
|
+
|
|
144
|
+
If ``allow_nan`` is false, then it will be a ``ValueError`` to
|
|
145
|
+
serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
|
|
146
|
+
strict compliance of the JSON specification, instead of using the
|
|
147
|
+
JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
|
|
148
|
+
|
|
149
|
+
If ``indent`` is a non-negative integer, then JSON array elements and
|
|
150
|
+
object members will be pretty-printed with that indent level. An indent
|
|
151
|
+
level of 0 will only insert newlines. ``None`` is the most compact
|
|
152
|
+
representation.
|
|
153
|
+
|
|
154
|
+
If specified, ``separators`` should be an ``(item_separator, key_separator)``
|
|
155
|
+
tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
|
|
156
|
+
``(',', ': ')`` otherwise. To get the most compact JSON representation,
|
|
157
|
+
you should specify ``(',', ':')`` to eliminate whitespace.
|
|
158
|
+
|
|
159
|
+
``default(obj)`` is a function that should return a serializable version
|
|
160
|
+
of obj or raise TypeError. The default simply raises TypeError.
|
|
161
|
+
|
|
162
|
+
If *sort_keys* is true (default: ``False``), then the output of
|
|
163
|
+
dictionaries will be sorted by key.
|
|
164
|
+
|
|
165
|
+
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
|
|
166
|
+
``.default()`` method to serialize additional types), specify it with
|
|
167
|
+
the ``cls`` kwarg; otherwise ``FracturedJSON.Encoder`` is used.
|
|
168
|
+
|
|
169
|
+
"""
|
|
170
|
+
return json.dumps(
|
|
171
|
+
obj,
|
|
172
|
+
cls=Encoder if cls is None else cls,
|
|
173
|
+
skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular,
|
|
174
|
+
allow_nan=allow_nan, indent=indent, separators=separators,
|
|
175
|
+
default=default, sort_keys=sort_keys,
|
|
176
|
+
**kw
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
def load(fp, *, cls=None, object_hook=None, parse_float=None,
|
|
180
|
+
parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
|
|
181
|
+
"""Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
|
|
182
|
+
a JSON document) to a Python object.
|
|
183
|
+
|
|
184
|
+
``object_hook`` is an optional function that will be called with the
|
|
185
|
+
result of any object literal decode (a ``dict``). The return value of
|
|
186
|
+
``object_hook`` will be used instead of the ``dict``. This feature
|
|
187
|
+
can be used to implement custom decoders (e.g. JSON-RPC class hinting).
|
|
188
|
+
|
|
189
|
+
``object_pairs_hook`` is an optional function that will be called with the
|
|
190
|
+
result of any object literal decoded with an ordered list of pairs. The
|
|
191
|
+
return value of ``object_pairs_hook`` will be used instead of the ``dict``.
|
|
192
|
+
This feature can be used to implement custom decoders. If ``object_hook``
|
|
193
|
+
is also defined, the ``object_pairs_hook`` takes priority.
|
|
194
|
+
|
|
195
|
+
To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
|
|
196
|
+
kwarg; otherwise ``JSONDecoder`` is used.
|
|
197
|
+
"""
|
|
198
|
+
return json.load(fp, cls=cls, object_hook=object_hook, parse_float=parse_float,
|
|
199
|
+
parse_int=parse_int, parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
|
|
200
|
+
|
|
201
|
+
def loads(fp, *, cls=None, object_hook=None, parse_float=None,
|
|
202
|
+
parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
|
|
203
|
+
"""Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance
|
|
204
|
+
containing a JSON document) to a Python object.
|
|
205
|
+
|
|
206
|
+
``object_hook`` is an optional function that will be called with the
|
|
207
|
+
result of any object literal decode (a ``dict``). The return value of
|
|
208
|
+
``object_hook`` will be used instead of the ``dict``. This feature
|
|
209
|
+
can be used to implement custom decoders (e.g. JSON-RPC class hinting).
|
|
210
|
+
|
|
211
|
+
``object_pairs_hook`` is an optional function that will be called with the
|
|
212
|
+
result of any object literal decoded with an ordered list of pairs. The
|
|
213
|
+
return value of ``object_pairs_hook`` will be used instead of the ``dict``.
|
|
214
|
+
This feature can be used to implement custom decoders. If ``object_hook``
|
|
215
|
+
is also defined, the ``object_pairs_hook`` takes priority.
|
|
216
|
+
|
|
217
|
+
``parse_float``, if specified, will be called with the string
|
|
218
|
+
of every JSON float to be decoded. By default this is equivalent to
|
|
219
|
+
float(num_str). This can be used to use another datatype or parser
|
|
220
|
+
for JSON floats (e.g. decimal.Decimal).
|
|
221
|
+
|
|
222
|
+
``parse_int``, if specified, will be called with the string
|
|
223
|
+
of every JSON int to be decoded. By default this is equivalent to
|
|
224
|
+
int(num_str). This can be used to use another datatype or parser
|
|
225
|
+
for JSON integers (e.g. float).
|
|
226
|
+
|
|
227
|
+
``parse_constant``, if specified, will be called with one of the
|
|
228
|
+
following strings: -Infinity, Infinity, NaN.
|
|
229
|
+
This can be used to raise an exception if invalid JSON numbers
|
|
230
|
+
are encountered.
|
|
231
|
+
|
|
232
|
+
To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
|
|
233
|
+
kwarg; otherwise ``JSONDecoder`` is used.
|
|
234
|
+
"""
|
|
235
|
+
return json.loads(fp, cls=cls, object_hook=object_hook, parse_float=parse_float,
|
|
236
|
+
parse_int=parse_int, parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
|
|
237
|
+
|
|
238
|
+
def detect_encoding(b: bytes | bytearray):
|
|
239
|
+
return json.detect_encoding(b)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Internal. Wraps a rust file, that reformats a json string to FracturedJSON"""
|
|
2
|
+
|
|
3
|
+
def reformat_string(
|
|
4
|
+
input_string: str,
|
|
5
|
+
*,
|
|
6
|
+
indent:int,
|
|
7
|
+
line_length:int,
|
|
8
|
+
max_inline_complexity:int,
|
|
9
|
+
max_compact_array_complexity:int,
|
|
10
|
+
max_table_row_complexity:int,
|
|
11
|
+
number_list_alignment:str,
|
|
12
|
+
max_prop_name_padding:int
|
|
13
|
+
) -> str:
|
|
14
|
+
"""Reformat a JSON string.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
input: A JSON string to reformat.
|
|
18
|
+
|
|
19
|
+
Returns:
|
|
20
|
+
The reformatted JSON string.
|
|
21
|
+
"""
|
|
22
|
+
...
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyfracturedjson
|
|
3
|
+
Version: 2.2.2
|
|
4
|
+
Classifier: Programming Language :: Rust
|
|
5
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Summary: A compact yet readable JSON formatter
|
|
8
|
+
Keywords: json,formatter,fracturedjson,compact,readable
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
12
|
+
Project-URL: Changelog, https://github.com/g-b-f/pyFracturedJSON/blob/master/CHANGELOG.md
|
|
13
|
+
Project-URL: Repository, https://github.com/g-b-f/pyFracturedJSON
|
|
14
|
+
|
|
15
|
+
<div align="center">
|
|
16
|
+
|
|
17
|
+
# pyFracturedJSON
|
|
18
|
+
|
|
19
|
+
**Adds [FracturedJSON](https://github.com/j-brooke/FracturedJson) support to python,
|
|
20
|
+
letting you create JSON that is both compact and readable.**
|
|
21
|
+
|
|
22
|
+
<a href="https://pypi.org/project/pyfracturedjson">
|
|
23
|
+
<img alt="PyPI Version" src="https://img.shields.io/pypi/v/pyfracturedjson?cacheSeconds=3600">
|
|
24
|
+
<img alt="minimum python version" src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fpypi.org%2Fpypi%2Fpyfracturedjson%2Fjson&query=%24.info.requires_python&label=requires%20python&cacheSeconds=3600">
|
|
25
|
+
</a>
|
|
26
|
+
</div>
|
|
27
|
+
</br>
|
|
28
|
+
|
|
29
|
+
You can trivially use it as a drop-in replacement for the built-in `json` module:
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
import fracturedjson as json
|
|
33
|
+
|
|
34
|
+
long_list = [f"thing_{i}" for i in range(20)]
|
|
35
|
+
data = {"abcd": "abcd", "long_list": long_list}
|
|
36
|
+
|
|
37
|
+
print(json.dumps(data))
|
|
38
|
+
print(json.dumps(data, line_length=50))
|
|
39
|
+
print(json.dumps(data, indent=2))
|
|
40
|
+
|
|
41
|
+
with open("file.json", "w") as f:
|
|
42
|
+
json.dump(data, f)
|
|
43
|
+
|
|
44
|
+
with open("file.json") as f:
|
|
45
|
+
json.load(f)
|
|
46
|
+
|
|
47
|
+
json.loads('{"foo":"bar"}')
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Or, if you'd prefer:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
import json
|
|
54
|
+
from fracturedjson import Encoder
|
|
55
|
+
|
|
56
|
+
long_list = [f"thing_{i}" for i in range(20)]
|
|
57
|
+
data = {"abcd": "abcd", "long_list": long_list}
|
|
58
|
+
|
|
59
|
+
print(json.dumps(data, cls=Encoder))
|
|
60
|
+
print(json.dumps(data, cls=Encoder, line_length=50))
|
|
61
|
+
print(json.dumps(data, cls=Encoder, indent=2))
|
|
62
|
+
|
|
63
|
+
with open("file.json", "w") as f:
|
|
64
|
+
json.dump(data, f, cls=Encoder)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
This is largely a wrapper around [fracturedjson-rs](https://github.com/fcoury/fracturedjson-rs),
|
|
68
|
+
so credit to the creator.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
_rust_wrapper/__init__.py,sha256=_OZ0ZjGSxq5kGDhgN6USYLwglh4x7KWblKJTRlFDNCo,135
|
|
2
|
+
_rust_wrapper/_rust_wrapper.cp315-win32.pyd,sha256=6BpWRi5wRNt5400yqH6OFuG-58Ibb5gXCImWy3l86Ss,318464
|
|
3
|
+
fracturedjson/__init__.py,sha256=2g2BmK7mNT0AGPiQF52xVkEi32z4z049IWxeqF_8XRk,10830
|
|
4
|
+
fracturedjson/_rust_wrapper.pyi,sha256=yD8YfRPau1-JSlAm0a60sOJZR4L4DxhVlTiAnoYJ9u8,581
|
|
5
|
+
pyfracturedjson-2.2.2.dist-info/METADATA,sha256=TauQVmkRk1PQXnwAG3AZcgZTMjV3Fr5c97clWluk5w4,2156
|
|
6
|
+
pyfracturedjson-2.2.2.dist-info/WHEEL,sha256=EfmT5yHmSsKCMf88H0y_gDJxXP11dyYwZSLaX0aGPEg,93
|
|
7
|
+
pyfracturedjson-2.2.2.dist-info/licenses/LICENSE,sha256=JP_jxkJtN5lbsNOleIbX63niT5A27wFehjCm0X8rCrE,1120
|
|
8
|
+
pyfracturedjson-2.2.2.dist-info/sboms/pyfracturedjson.cyclonedx.json,sha256=lM2rgAPrLlkHfgxrvbH5fFZCQie2yqB2y3Nr_NBF6Xk,43024
|
|
9
|
+
pyfracturedjson-2.2.2.dist-info/RECORD,,
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013-2019 Counsyl/Myriad Genetics Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
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, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|