{"id":18369623,"url":"https://github.com/mathspp/extendedjson","last_synced_at":"2025-04-06T17:32:16.416Z","repository":{"id":37467339,"uuid":"505836902","full_name":"mathspp/extendedjson","owner":"mathspp","description":"Easily extend JSON to encode and decode arbitrary Python objects.","archived":false,"fork":false,"pushed_at":"2022-07-28T09:23:50.000Z","size":58,"stargazers_count":10,"open_issues_count":1,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-22T04:03:41.592Z","etag":null,"topics":["extensible","json","python","python3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mathspp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-21T12:37:56.000Z","updated_at":"2023-06-26T17:29:55.000Z","dependencies_parsed_at":"2022-08-02T01:00:07.539Z","dependency_job_id":null,"html_url":"https://github.com/mathspp/extendedjson","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathspp%2Fextendedjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathspp%2Fextendedjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathspp%2Fextendedjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathspp%2Fextendedjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mathspp","download_url":"https://codeload.github.com/mathspp/extendedjson/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247522594,"owners_count":20952581,"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":["extensible","json","python","python3"],"created_at":"2024-11-05T23:29:58.927Z","updated_at":"2025-04-06T17:32:13.758Z","avatar_url":"https://github.com/mathspp.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# extendedjson\n\n \u003e Easily extend JSON to encode and decode arbitrary Python objects.\n\n[![PyPI version][pypi-image]][pypi-url]\n[![Build status][build-image]][build-url]\n[![Code coverage][coverage-image]][coverage-url]\n[![GitHub stars][stars-image]][stars-url]\n[![Support Python versions][versions-image]][versions-url]\n\n\n## Getting started\n\nYou can [get `extendedjson` from PyPI](https://pypi.org/project/extendedjson),\nwhich means it's easily installable with `pip`:\n\n```bash\npython -m pip install extendedjson\n```\n\n\n## Example usage\n\nSuppose you want to extend the JSON format to handle complex numbers,\nwhich corresponds to the type `complex` in Python.\n\nTo do that, you need to:\n\n 1. Determine how a complex number could look like as a JSON dictionary.\n For example, a dictionary with keys `\"real\"` and `\"imag\"` is enough to determine what complex number we are talking about.\n 2. Subclass `ExtendedEncoder` and implement the method `encode_complex` that accepts a complex number and returns a dictionary with the format you defined.\n 3. Subclass `ExtendedDecoder` and implement a method `decode_complex` that accepts a dictionary with the format you described and returns an instance of a `complex` number.\n\nHere is the code:\n\n```py\nimport extendedjson as xjson\n\n\nclass MyEncoder(xjson.ExtendedEncoder):\n    def encode_complex(self, c):\n        return {\"real\": c.real, \"imag\": c.imag}\n\n\nclass MyDecoder(xjson.ExtendedDecoder):\n    def decode_complex(self, dict_):\n        return complex(dict_[\"real\"], dict_[\"imag\"])\n```\n\nThen, you can use your classes with the standard module `json`,\nby specifying the `cls` keyword argument in the functions `json.load`, `json.loads`, `json.dump`, and `json.dumps`:\n\n```py\nimport json\n\nc = complex(1, 2)\nc_json = json.dumps(c, cls=MyEncoder)\nc_ = json.loads(c_json, cls=MyDecoder)\nprint(c_)  # (1+2j)\nprint(c_ == c)  # True\n```\n\nRefer to [this article](https://mathspp.com/blog/custom-json-encoder-and-decoder) to learn more about the internal details of `extendedjson`.\n\n\n## Changelog\n\nRefer to the [CHANGELOG.md](CHANGELOG.md) file.\n\n\n\u003c!-- Badges --\u003e\n\n[pypi-image]: https://img.shields.io/pypi/v/extendedjson\n[pypi-url]: https://pypi.org/project/extendedjson/\n[build-image]: https://github.com/mathspp/extendedjson/actions/workflows/build.yaml/badge.svg\n[build-url]: https://github.com/mathspp/extendedjson/actions/workflows/build.yaml\n[coverage-image]: https://codecov.io/gh/mathspp/extendedjson/branch/main/graph/badge.svg\n[coverage-url]: https://codecov.io/gh/mathspp/extendedjson/\n[stars-image]: https://img.shields.io/github/stars/mathspp/extendedjson\n[stars-url]: https://github.com/mathspp/extendedjson\n[versions-image]: https://img.shields.io/pypi/pyversions/extendedjson\n[versions-url]: https://pypi.org/project/extendedjson/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathspp%2Fextendedjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmathspp%2Fextendedjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathspp%2Fextendedjson/lists"}