{"id":23413175,"url":"https://github.com/trialandsuccess/configurablejson","last_synced_at":"2026-04-25T22:32:36.683Z","repository":{"id":63367305,"uuid":"567372121","full_name":"trialandsuccess/configurablejson","owner":"trialandsuccess","description":"Easily Extend the Python JSON Encoder with Custom Rules","archived":false,"fork":false,"pushed_at":"2023-07-19T11:22:07.000Z","size":21,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-17T06:45:47.609Z","etag":null,"topics":["json","python","python3"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/Configurable-JSON/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/trialandsuccess.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-17T16:43:52.000Z","updated_at":"2023-07-19T08:42:34.000Z","dependencies_parsed_at":"2024-12-22T19:30:13.941Z","dependency_job_id":"9dcc94ac-8b7e-478e-a0f8-d59f6b351c0a","html_url":"https://github.com/trialandsuccess/configurablejson","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trialandsuccess%2Fconfigurablejson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trialandsuccess%2Fconfigurablejson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trialandsuccess%2Fconfigurablejson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trialandsuccess%2Fconfigurablejson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trialandsuccess","download_url":"https://codeload.github.com/trialandsuccess/configurablejson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247980826,"owners_count":21027803,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","python","python3"],"created_at":"2024-12-22T19:25:54.140Z","updated_at":"2026-04-25T22:32:31.654Z","avatar_url":"https://github.com/trialandsuccess.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Configurable JSON Encoder\n\nBy default, the json.Encoder class' logic can only be customized to a certain extent:\n\n- a 'default' method can be overwritten to encode objects that JSON can't serialize by default;\n- an 'encode' method can be overwritten to fully rewrite the encoding logic, which is powerful but can be hard to change\n  if you need to change the specific behavior for a type in a nested structure;\n- an 'iterencode' method can be overwritten to handle the recursive behavior of the JSON encoder, but this method uses\n  functions under the hood that are not defined as class methods but rather as functions in the encoder\n  module (`c_make_encoder` and `_make_iterencode`), which thus can not be overwritten by class inheritance.\n\nThis module aims to help make adding custom rules to the JSON encoder easier, by injecting a middle\nlayer `ConfigurableJsonEncoder` which essentially uses a copied and slightly modified version of `_make_iterencode` from\nthe original module.\n\nSome examples of things you can do with this module (see examples.py for the actual rules)\n\n```python\nfrom examples import *\n\ndata = {\n    'original': ['behavior'],\n    'set': {1, 2, 3},\n    'namedtuple': Letters('a', 'b', 'c'),\n    \"class\": MyClass()\n}\ntry:\n    print(json.dumps(data))\nexcept TypeError:\n    ...  # Object of type set is not JSON serializable\n\n# default behavior without type error:\nprint(json.dumps(data, default=str))\n# {\"original\": [\"behavior\"], \"set\": \"{1, 2, 3}\", \"namedtuple\": [\"a\", \"b\", \"c\"], \"class\": \"\u003c__main__.MyClass object at 0x...\u003e\"}\n\n# the same behavior as above\nprint(json.dumps(data, cls=DummyEncoder))\n# {\"original\": [\"behavior\"], \"set\": \"{1, 2, 3}\", \"namedtuple\": [\"a\", \"b\", \"c\"], \"class\": \"\u003c__main__.MyClass object at 0x...\u003e\"}\n\n# encodes set into a list:\nprint(json.dumps(data, cls=SetEncoder))\n# {\"original\": [\"behavior\"], \"set\": [1, 2, 3], \"namedtuple\": [\"a\", \"b\", \"c\"], \"class\": \"\u003c__main__.MyClass object at 0x...\u003e\"}\n\n# calls .tojson() which uses transform to output a string\nprint(json.dumps(data, cls=ToJSONEncoder))\n# {\"original\": [\"behavior\"], \"set\": \"{1, 2, 3}\", \"namedtuple\": [\"a\", \"b\", \"c\"], \"class\": [\"my\", \"data\", \"as\", \"json\"]}\n\n# converts namedtuple to a dictionary instead of a list (the default behavior)\nprint(json.dumps(data, cls=MyEncoder))\n# {\"original\": [\"behavior\"], \"set\": [1, 2, 3], \"namedtuple\": {\"a\": \"a\", \"b\": \"b\", \"c\": \"c\"}, \"class\": {\"my data\": [\"as\", \"a\", \"dict\"]}}\n\n```\n\n## Usage:\n\nSimply extend `ConfigurableJsonEncoder` with a `rules` method that returns a `JSONRule` or `None` based on the\ninput `o` (which is one unit in the nested data tree). Again, see `examples.py` for inspiration.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrialandsuccess%2Fconfigurablejson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrialandsuccess%2Fconfigurablejson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrialandsuccess%2Fconfigurablejson/lists"}