{"id":7102,"library":"compact-json","title":"Compact JSON Formatter","description":"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.","status":"deprecated","version":"1.8.2","language":"en","source_language":"en","source_url":"https://github.com/masaccio/compact-json","tags":["json","formatter","pretty-print","compact","serialization","deprecated"],"install":[{"cmd":"pip install compact-json","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Provides backport of `importlib.resources` for Python <3.9","package":"importlib-resources","optional":true},{"reason":"Standard Python packaging tools","package":"setuptools","optional":false},{"reason":"Calculates printable width of unicode strings, used for formatting alignment","package":"wcwidth","optional":false}],"imports":[{"symbol":"Formatter","correct":"from compact_json import Formatter"},{"symbol":"EolStyle","correct":"from compact_json import EolStyle"}],"quickstart":{"code":"import json\nfrom compact_json import Formatter, EolStyle\n\n# Example Python dictionary\ndata = {\n    \"widget\": {\n        \"debug\": \"on\",\n        \"window\": {\"title\": \"Sample Widget\", \"name\": \"main_window\", \"width\": 500, \"height\": 500},\n        \"image\": {\"src\": \"Images/Sun.png\", \"name\": \"sun1\", \"hOffset\": 250, \"vOffset\": 250, \"alignment\": \"center\"},\n        \"text\": {\n            \"data\": \"Click Here\",\n            \"size\": 36,\n            \"style\": \"bold\",\n            \"name\": \"text1\",\n            \"hOffset\": 250,\n            \"vOffset\": 100,\n            \"alignment\": \"center\",\n            \"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\"\n        }\n    }\n}\n\nformatter = Formatter()\nformatter.indent_spaces = 2\nformatter.max_inline_complexity = 2 # Low complexity to demonstrate compactness\nformatter.json_eol_style = EolStyle.LF\nformatter.simple_bracket_padding = False # To make it more compact\n\nformatted_json_string = formatter.serialize(data)\nprint(formatted_json_string)\n\n# Example of customizing formatter options\nanother_formatter = Formatter()\nanother_formatter.use_tab_to_indent = True\nanother_formatter.max_total_line_length = 60\nprint('\\n--- Formatted with tabs and shorter lines ---')\nprint(another_formatter.serialize(data))","lang":"python","description":"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."},"warnings":[{"fix":"Migrate to `fractured-json`. The API for `fractured-json` is similar but may require adjustments to imports and configuration options.","message":"The `compact-json` library is officially deprecated. Users are strongly advised to migrate to `fractured-json` for ongoing support and new features. `compact-json` will receive no further development and has been archived on PyPI.","severity":"breaking","affected_versions":">=1.8.0"},{"fix":"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.","message":"When dictionary keys are not strings (e.g., integers), `compact-json` will coerce them to strings to ensure valid JSON, issuing a `RuntimeWarning`. This differs from the built-in `json` module's default behavior, which may raise a `TypeError`.","severity":"gotcha","affected_versions":"All"},{"fix":"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.","message":"The output JSON's compactness and readability are highly dependent on formatter options like `max_inline_complexity`, `indent_spaces`, and `max_total_line_length`. Default settings might not produce the desired level of compactness for all data structures.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"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()}`.","cause":"A Python dictionary with non-string keys (e.g., integers, booleans) was passed to the formatter. JSON requires all object keys to be strings.","error":"/path/src/compact_json/formatter.py:XXX: RuntimeWarning: coercing key value XXX to string"},{"fix":"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).","cause":"The input Python object contains elements (e.g., `datetime` objects, `set`s, custom classes) that cannot be directly converted to JSON by the underlying `json` module.","error":"TypeError: Object of type <non-serializable-type> is not JSON serializable"}]}