You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`fractured-json` is primarily a python wrapper of of [FracturedJson](https://github.com/j-brooke/FracturedJson) by [j-brooke](https://github.com/j-brooke).
8
+
`fractured-json` is a python wrapper of [FracturedJson](https://github.com/j-brooke/FracturedJson) by [j-brooke](https://github.com/j-brooke). The package fully follows the .NET version and includes the required assembly to run as long as you have installed a suitabe .NET runtime.
9
9
10
-
There is a pure Python implementation of a JSON compactor called [`compact-json`](https://github.com/masaccio/compact-json) however this is unlikely to get maintenance beyond critical bugfixes.
10
+
## Installation
11
11
12
-
## Plans
12
+
You must install a valid .NET runtime that is compatible with [Python.NET](https://pythonnet.github.io) (`pythonnet`). The package honors the environment variable `PYTHONNET_RUNTIME` for selecting the runtime variant but defaults to `coreclr`. As of current testing, Python versions 3.11 through 3.12 and .NET versions 7.0 and 8.0 are supported. Later versions are currently not supported by `pythonnet`.
13
13
14
-
**THE PACKAGE IS CURRENTLY UNDER DEVELOPMENT**
14
+
You can download the Core .NET runtime from the [Microsoft .NET website](https://dotnet.microsoft.com/en-us/download/dotnet/8.0) and version 8.0 is recommended as the stable and long-term supported version. Once installed, installation is simply:
15
15
16
-
Feel free to [join the discussion about the python wrapper](https://github.com/j-brooke/FracturedJson/discussions/48). The goal of the python wrapper is to track the .NET core of the JSON formatter and provide all the features of the .NET version in python. The command-line capabilities of `compact-json` and the FracturedJson CLI are expected to merge.
16
+
```shell
17
+
python3 -m pip install fractured-json
18
+
```
17
19
18
-
The API naming style of the python implementation is deliberately different to the .NET core to be as pythonic as possible. Specifically:
20
+
There is a pure Python implementation of a JSON compactor called [`compact-json`](https://github.com/masaccio/compact-json) however this has now been archived on PyPI and will receive no further development.
21
+
22
+
The [FracturedJson Wiki](https://github.com/j-brooke/FracturedJson/wiki) provides full documentation of intent, and a description of the options. This README is untended to cover only the Python specific elements of the wrapper.
23
+
24
+
## Command-line
25
+
26
+
The package installs a command-line script `fractured-json` which can compact one or more JSON files according to command-line switches.
Where to place commas in table-formatted elements.
154
+
(default=BEFORE_PADDING_EXCEPT_NUMBERS)
155
+
--use-tab-to-indent If true, a single tab character is used per
156
+
indentation level, instead of spaces.
157
+
(default=False)
158
+
--east-asian-chars Treat strings as unicode East Asian characters
159
+
```
19
160
20
-
* All classes are Pascal case, e.g. FracturedJsonOptions
21
-
* All properties are snake case, e.g. json_eol_style
22
-
* All enumerations are upper-case snake case within a Python Object, e.g. EolStyle.CRLF. They are not derived from `Enum` but have the same behaviors.
161
+
The option `--east-asian-chars` indicates that `fractured-json` should take account of variable width East-Asian character sets when reformatting JSON.
23
162
24
-
## Installation
163
+
Multiple files and output files can be processed at once but the number of input and output files must match:
25
164
26
-
You will need to install a .NET runtime that is compatible with [Python.NET](https://pythonnet.github.io) (`pythonnet`). The package honors the environment variable `PYTHONNET_RUNTIME` for selecting the runtime variant but defaults to `coreclr`. As of current testing, Python 3.14 and .NET 10.0 are not supported by `pythonnet`. You can download the Core .NET runtime from the [Microsoft .NET website](https://dotnet.microsoft.com/en-us/download/dotnet/8.0) and version 8.0 is recommended as the stable and long-term supported version.
A full description of the options available can be found in the [FracturedJson Wiki](https://github.com/j-brooke/FracturedJson/wiki/Options) and these are dynamically created from the .NET library so will always match the .NET implementation.
190
+
191
+
``` python
192
+
from fractured_json import Formatter, FracturedJsonOptions, CommentPolicy
193
+
from pathlib import Path
194
+
195
+
options = FracturedJsonOptions(
196
+
allow_trailing_commas=True,
197
+
always_expand_depth=2,
198
+
colon_before_prop_name_padding=True,
199
+
comment_policy=CommentPolicy.PRESERVE
200
+
indent_spaces=2,
201
+
)
202
+
formatter = Formatter(options=options)
203
+
json_input = Path("example.jsonc").read_text()
204
+
json_output = formatter.reformat(json_input)
205
+
```
206
+
207
+
Enumerations can be passed to `FracturedJsonOptions` as strings or as Python-style enums:
208
+
209
+
``` python
210
+
>>> from fractured_json import NumberListAlignment
<fractured_json.FracturedJsonOptions object at 0x10966f9d0>
215
+
```
216
+
217
+
### Wide character support
218
+
219
+
When formatting dictionaries, FracturedJson needs to know the length of strings and for some East-Asian characters, the rendering width needs to be adjusted. The `Formatter.string_length_func` property is used to specify an alternative functionto calculate strings lengths. The easiest approach is to use `wcwidth.wcswidth` which is packaged with `fractured-json` as a dependency:
@@ -38,3 +231,5 @@ All code in this repository is licensed under the [MIT License](https://github.c
38
231
## Contribute
39
232
40
233
Contributions are greatly appreciated and welcomed. Please follow the [project guidance](CONTRIBUTING.md) on how to contribute.
234
+
235
+
Feel free to [join the discussion about the python wrapper](https://github.com/j-brooke/FracturedJson/discussions/48). The goal of the python wrapper is to track the .NET core of the JSON formatter and provide all the features of the .NET version in python.
0 commit comments