{"id":17044904,"url":"https://github.com/henriquebastos/python-jsonstar","last_synced_at":"2025-04-12T15:20:52.156Z","repository":{"id":221079971,"uuid":"753359644","full_name":"henriquebastos/python-jsonstar","owner":"henriquebastos","description":"Extensible JSON module to serialize all objects.","archived":false,"fork":false,"pushed_at":"2025-03-01T01:00:45.000Z","size":130,"stargazers_count":22,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-26T09:51:11.573Z","etag":null,"topics":["api-client","json","python","serialization"],"latest_commit_sha":null,"homepage":"https://pypi.org/p/jsonstar","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/henriquebastos.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2024-02-06T00:41:12.000Z","updated_at":"2025-03-25T14:14:05.000Z","dependencies_parsed_at":"2024-02-10T17:48:51.735Z","dependency_job_id":null,"html_url":"https://github.com/henriquebastos/python-jsonstar","commit_stats":null,"previous_names":["henriquebastos/python-jsonplus","henriquebastos/python-jsonstar"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henriquebastos%2Fpython-jsonstar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henriquebastos%2Fpython-jsonstar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henriquebastos%2Fpython-jsonstar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henriquebastos%2Fpython-jsonstar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/henriquebastos","download_url":"https://codeload.github.com/henriquebastos/python-jsonstar/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248586217,"owners_count":21128998,"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":["api-client","json","python","serialization"],"created_at":"2024-10-14T09:35:52.524Z","updated_at":"2025-04-12T15:20:52.149Z","avatar_url":"https://github.com/henriquebastos.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON* is an extensible json module to serialize all objects!\n\n`jsonstar` extends Python's standard JSON encoder and decoder to easily handle your custom types.\n\nThis means you won't have to transform your custom types into dictionaries with primitive types before encoding them to JSON. And you won't have to parse back the encoded strings into your custom types after decoding them from JSON.\n\n## How to install it?\n\n```bash\npip install jsonstar\n```\n\n## How to start using it?\n\nThe `jsonstar` module provides the same API as the standard `json` module, so you can use it as a drop in replacement.\n\nSimply change your import from `import json` to `import jsonstar as json` and you're good to go.\n\n## Why use it?\n\nConsider you have a pydantic `Employee` class that you want to serialize to JSON.\n\n```python\nfrom decimal import Decimal\nfrom datetime import date\nfrom pydantic import BaseModel\n\nclass Employee(BaseModel):\n    name: str\n    salary: Decimal\n    birthday: date\n    roles: set\n\nemployee = Employee(\n    name=\"John Doe\",\n    salary=Decimal(\"1000.00\"),\n    birthday=date(1990, 1, 1),\n    roles={\"A\", \"B\", \"C\"},\n)\n```\n\nThe standard `json` module can't serialize the `employee` instance, requiring you to call its `dict` method.\nThis will not suffice, because the standard `json` module don't know how to encode `Decimal`, `date` and `set`.\nYour solution would include some transfomation of the `employee` instance and its attributes before encoding it to JSON.\n\nThat is where `jsonstar` shines by providing default encoder for common types like `pydantic.BaseModel`,\n`decimal.Decimal`, `datetime.date` and `set`. And allowing you to easily add your own encoders.\n\n```python\nfrom jsonstar as json\nprint(json.dumps(employee))\n# {\"name\": \"John Doe\", \"salary\": \"1000.00\", \"birthday\": \"1990-01-01\", \"roles\": [\"A\", \"B\", \"C\"]}\n```\n\n## What default encoders are provided?\n\nBy default, `jsonstar` provides encoders for the following types:\n\n- `attrs` classes\n- `dataclasses.dataclass` classes\n- `datetime.date`\n- `datetime.datetime`\n- `datetime.time`\n- `datetime.timedelta`\n- `decimal.Decimal`\n- `frozenset`\n- `pydantic.BaseModel`\n- `set`\n- `uuid.UUID`\n\n### Can `jsonstar` add more default encoders?\n\nYes. If you think that a default encoder for a common type is missing, please open an issue or a pull request.\nSee the *How to contribute* section for more details.\n\n## How do I add my own encoder?\n\nFirst you need to decide where you want your encoder to be available:\n\n1. *Class default encoders* happen when your `MyEncoder` class inherits from `JSONEncoderStar` and you add encoders to it.\n2. *Library-wide default encoder* are added directly to `JSONEncoderStar` class and is available everywhere in your project.\n\nAlso you have two types of encoders to choose from:\n\n- *Typed encoders* are used to encode a specific type identified by `isinstance`.\n- *Functional encoders* are used to encode an object based on arbitraty logic.\n\n*Note:* From experience we find that *class encoders* are the most common use case.\n\n### How to add class default encoders?\n\n```python\nimport jsonstar as json\nfrom decimal import Decimal\nfrom datetime import date\n\n\n# You can declare it on the special class attributes\nclass MyEncoder(json.JSONEncoderStar):\n    _default_typed_encoders = {Decimal: lambda o: str(o.quantize(Decimal(\"1.00\")))}\n\n# Or you can register it after the class is declared\nMyEncoder.register_default_encoder(lambda o: o.strftime(\"%Y-%m-%d\"), date)\n```\n\n### How to add a library-wide default encoder?\n\n```python\nimport jsonstar as json\nfrom decimal import Decimal\n\n\ndef two_decimals_encoder(obj):\n    \"\"\"Encodes a decimal with only two decimal places.\"\"\"\n    return str(obj.quantize(Decimal(\"1.00\")))\n\n\njson.register_default_encoder(Decimal, two_decimals_encoder)\n```\n\n### How to add a typed encoder?\n\n*Typed encoders* are specific to a type and it's inherited types.\n\nWhen registering a typed encoder, you simply pass the encoder and the type to the chosen registration method.\n\nWhen you add a typed encoder, `jsonstar` will check if any base class already has a registered encoder make sure the more generic encoder is used last, respecting Python's Method Resolution Order (MRO).\n\n### How to add a functional encoder?\n\n*Functional encoders* are used to encode an object based on arbitraty logic and not specific to a type.\n\nTo register a functional encoder, you simply pass the encoder to the chosen registration method omiting the type.\n\nAll functional encoders are called only for objects that do not have a registered typed encoder.\n\n## Contributing\n\nPull requests are welcome and must have associated tests.\n\nFor major changes, please open an issue first to discuss what you would like to change.\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)\n\n## Author\n\nHenrique Bastos \u003chenrique@bastos.net\u003e\n\n## Project links\n\n- [Homepage](https://github.com/henriquebastos/python-jsonstar)\n- [Repository](https://github.com/henriquebastos/python-jsonstar)\n- [Documentation](https://github.com/henriquebastos/python-jsonstar)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhenriquebastos%2Fpython-jsonstar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhenriquebastos%2Fpython-jsonstar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhenriquebastos%2Fpython-jsonstar/lists"}