compact-json is a Python library that provides a configurable JSON formatter, producing compact yet human-readable output. It is a pure Python port of FracturedJsonJs. As of its latest version, 1.8.2, the package is deprecated, and users are strongly encouraged to migrate to the `fractured-json` library, which directly tracks the original FracturedJson .NET assembly. The library is currently in maintenance mode with no further development planned.
pip install compact-jsonVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage of the `Formatter` class to serialize a Python dictionary into a compact JSON string with custom formatting options. The example uses a simple dictionary and prints the formatted output to the console.
Migrate to `fractured-json`. The API for `fractured-json` is similar but may require adjustments to imports and configuration options.
Ensure all dictionary keys are strings before passing the object to `Formatter.serialize()` or `Formatter.dump()` if key coercion is undesirable. Review `RuntimeWarning` messages carefully.
Experiment with formatter properties (e.g., `formatter.max_inline_complexity`, `formatter.indent_spaces`, `formatter.max_total_line_length`) to fine-tune the output format for your specific JSON data. Consult the GitHub README for a full list of configuration options.
Before formatting, ensure all keys in your Python dictionaries are strings. For example, explicitly convert integer keys to strings: `{str(k): v for k, v in my_dict.items()}`.Pre-process your Python object to convert non-serializable types into JSON-compatible types (e.g., `datetime` to ISO 8601 strings, `set`s to `list`s). Alternatively, provide a custom `default` function to the underlying `json.dumps` call, if `compact-json` exposes such a mechanism (check the `Formatter`'s `serialize` method documentation).