jsonstar


Namejsonstar JSON
Version 1.1.1 PyPI version JSON
download
home_pageNone
SummaryExtensible JSON module to serialize all objects.
upload_time2025-03-01 01:01:57
maintainerNone
docs_urlNone
authorHenrique Bastos
requires_python>=3.9
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # JSON* is an extensible json module to serialize all objects!

`jsonstar` extends Python's standard JSON encoder and decoder to easily handle your custom types.

This 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.

## How to install it?

```bash
pip install jsonstar
```

## How to start using it?

The `jsonstar` module provides the same API as the standard `json` module, so you can use it as a drop in replacement.

Simply change your import from `import json` to `import jsonstar as json` and you're good to go.

## Why use it?

Consider you have a pydantic `Employee` class that you want to serialize to JSON.

```python
from decimal import Decimal
from datetime import date
from pydantic import BaseModel

class Employee(BaseModel):
    name: str
    salary: Decimal
    birthday: date
    roles: set

employee = Employee(
    name="John Doe",
    salary=Decimal("1000.00"),
    birthday=date(1990, 1, 1),
    roles={"A", "B", "C"},
)
```

The standard `json` module can't serialize the `employee` instance, requiring you to call its `dict` method.
This will not suffice, because the standard `json` module don't know how to encode `Decimal`, `date` and `set`.
Your solution would include some transfomation of the `employee` instance and its attributes before encoding it to JSON.

That is where `jsonstar` shines by providing default encoder for common types like `pydantic.BaseModel`,
`decimal.Decimal`, `datetime.date` and `set`. And allowing you to easily add your own encoders.

```python
from jsonstar as json
print(json.dumps(employee))
# {"name": "John Doe", "salary": "1000.00", "birthday": "1990-01-01", "roles": ["A", "B", "C"]}
```

## What default encoders are provided?

By default, `jsonstar` provides encoders for the following types:

- `attrs` classes
- `dataclasses.dataclass` classes
- `datetime.date`
- `datetime.datetime`
- `datetime.time`
- `datetime.timedelta`
- `decimal.Decimal`
- `frozenset`
- `pydantic.BaseModel`
- `set`
- `uuid.UUID`

### Can `jsonstar` add more default encoders?

Yes. If you think that a default encoder for a common type is missing, please open an issue or a pull request.
See the *How to contribute* section for more details.

## How do I add my own encoder?

First you need to decide where you want your encoder to be available:

1. *Class default encoders* happen when your `MyEncoder` class inherits from `JSONEncoderStar` and you add encoders to it.
2. *Library-wide default encoder* are added directly to `JSONEncoderStar` class and is available everywhere in your project.

Also you have two types of encoders to choose from:

- *Typed encoders* are used to encode a specific type identified by `isinstance`.
- *Functional encoders* are used to encode an object based on arbitraty logic.

*Note:* From experience we find that *class encoders* are the most common use case.

### How to add class default encoders?

```python
import jsonstar as json
from decimal import Decimal
from datetime import date


# You can declare it on the special class attributes
class MyEncoder(json.JSONEncoderStar):
    _default_typed_encoders = {Decimal: lambda o: str(o.quantize(Decimal("1.00")))}

# Or you can register it after the class is declared
MyEncoder.register_default_encoder(lambda o: o.strftime("%Y-%m-%d"), date)
```

### How to add a library-wide default encoder?

```python
import jsonstar as json
from decimal import Decimal


def two_decimals_encoder(obj):
    """Encodes a decimal with only two decimal places."""
    return str(obj.quantize(Decimal("1.00")))


json.register_default_encoder(Decimal, two_decimals_encoder)
```

### How to add a typed encoder?

*Typed encoders* are specific to a type and it's inherited types.

When registering a typed encoder, you simply pass the encoder and the type to the chosen registration method.

When 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).

### How to add a functional encoder?

*Functional encoders* are used to encode an object based on arbitraty logic and not specific to a type.

To register a functional encoder, you simply pass the encoder to the chosen registration method omiting the type.

All functional encoders are called only for objects that do not have a registered typed encoder.

## Contributing

Pull requests are welcome and must have associated tests.

For major changes, please open an issue first to discuss what you would like to change.

## License

[MIT](https://choosealicense.com/licenses/mit/)

## Author

Henrique Bastos <henrique@bastos.net>

## Project links

- [Homepage](https://github.com/henriquebastos/python-jsonstar)
- [Repository](https://github.com/henriquebastos/python-jsonstar)
- [Documentation](https://github.com/henriquebastos/python-jsonstar)


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "jsonstar",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "Henrique Bastos",
    "author_email": "henrique@bastos.net",
    "download_url": "https://files.pythonhosted.org/packages/30/8f/afb04336376dbf3b7f8c9abb66e3375de13879253fd3421c76967dc5bd60/jsonstar-1.1.1.tar.gz",
    "platform": null,
    "description": "# 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 <henrique@bastos.net>\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\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Extensible JSON module to serialize all objects.",
    "version": "1.1.1",
    "project_urls": {
        "Documentation": "https://github.com/henriquebastos/python-jsonstar",
        "Homepage": "https://github.com/henriquebastos/python-jsonstar",
        "Issues": "https://github.com/henriquebastos/python-jsonstar/issues",
        "Repository": "https://github.com/henriquebastos/python-jsonstar"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "11386e1b14f0d76e9bf6431487a496d56bbc7d0995ab2f592796aac6288ec47c",
                "md5": "19390a6c172cefb6fb5c732c8b58e368",
                "sha256": "cac260de44154150720c0b83f1f7068677b20595c130a9943f277e3bb5a9e8b7"
            },
            "downloads": -1,
            "filename": "jsonstar-1.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "19390a6c172cefb6fb5c732c8b58e368",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 6914,
            "upload_time": "2025-03-01T01:01:55",
            "upload_time_iso_8601": "2025-03-01T01:01:55.522388Z",
            "url": "https://files.pythonhosted.org/packages/11/38/6e1b14f0d76e9bf6431487a496d56bbc7d0995ab2f592796aac6288ec47c/jsonstar-1.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "308fafb04336376dbf3b7f8c9abb66e3375de13879253fd3421c76967dc5bd60",
                "md5": "2c9c9287a482f477a99410f1302ffd02",
                "sha256": "a287391f46a09af218b94c9a763bfc428e76f1dc8ed5a1fbf70b8ab1be49bf02"
            },
            "downloads": -1,
            "filename": "jsonstar-1.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "2c9c9287a482f477a99410f1302ffd02",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 5705,
            "upload_time": "2025-03-01T01:01:57",
            "upload_time_iso_8601": "2025-03-01T01:01:57.288984Z",
            "url": "https://files.pythonhosted.org/packages/30/8f/afb04336376dbf3b7f8c9abb66e3375de13879253fd3421c76967dc5bd60/jsonstar-1.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-03-01 01:01:57",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "henriquebastos",
    "github_project": "python-jsonstar",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "jsonstar"
}
        
Elapsed time: 3.49740s