json-advanced


Namejson-advanced JSON
Version 0.12.5 PyPI version JSON
download
home_pageNone
SummaryThis Python package provides an extended JSON encoder class, `JSONSerializer`, that enables encoding of complex Python data types such as `datetime.datetime`, `datetime.date`, `datetime.time`, `bytes` and `uuid`. It also supports objects that have a `to_json` method, allowing for customizable JSON encoding.
upload_time2024-10-02 14:43:09
maintainerNone
docs_urlNone
authorNone
requires_python>=3
licenseCopyright (c) 2024 Mahdi Kiani Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords json serialization datetime bytes uuid path
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Extended JSON Encoder

The `json-advanced` is Python package provides an extended JSON encoder class, `JSONSerializer`, that enables encoding of complex Python data types such as `datetime.datetime`, `datetime.date`, `datetime.time`, `bytes` and `uuid`. It also supports objects that have a `to_json` method, allowing for customizable JSON encoding.

## Features

- **Datetime Handling**: Automatically converts `datetime.datetime`, `datetime.date`, and `datetime.time` objects to their string representation.
- **UUID Encoding**: Encodes `uuid` objects as uuid strings.
- **Bytes Encoding**: Encodes `bytes` objects as base64 strings, prefixed with `b64:`.
- **Custom Object Support**: Encodes any object that has a `to_json` method by calling that method.

## Installation

You can install the package directly from source:

```bash
pip install json-advanced
```

## Usage
To use the JSONSerializer in your project, you need to import it and use it with the standard json module's dump or dumps functions:

```python
import json
import datetime
import uuid

from json_advanced.json_encoder import JSONSerializer

# Example object containing various complex data types
data = {
    "now": datetime.datetime.now(),
    "today": datetime.date.today(),
    "time": datetime.datetime.now().time(),
    "bytes_data": b"example bytes",
    "uuid": uuid.uuid4(),
}

# Serialize the object to a JSON string
json_string = json.dumps(data, cls=JSONSerializer)
print(json_string)
```

## Extending the Serializer
If you have custom types that you want to serialize, you can extend JSONSerializer by overriding the default method. Ensure you call super().default(obj) for types you do not handle:

```python
class MyCustomSerializer(JSONSerializer):
    def default(self, obj):
        if isinstance(obj, MyCustomType):
            return obj.custom_serialize()
        return super().default(obj)
```

## Contributions
Contributions are welcome! Please open an issue or pull request on GitHub if you have suggestions or improvements.

## License
This package is licensed under the MIT License - see the LICENSE file for details.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "json-advanced",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3",
    "maintainer_email": "Mahdi Kiani <mahdikiany@gmail.com>",
    "keywords": "json, serialization, datetime, bytes, uuid, path",
    "author": null,
    "author_email": "Mahdi Kiani <mahdikiany@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/0c/7a/2c1d59e7793c54648dd2c585c57b10cb77c1e5bfef9bb6a6ec1ee829b1ef/json_advanced-0.12.5.tar.gz",
    "platform": null,
    "description": "# Extended JSON Encoder\n\nThe `json-advanced` is Python package provides an extended JSON encoder class, `JSONSerializer`, that enables encoding of complex Python data types such as `datetime.datetime`, `datetime.date`, `datetime.time`, `bytes` and `uuid`. It also supports objects that have a `to_json` method, allowing for customizable JSON encoding.\n\n## Features\n\n- **Datetime Handling**: Automatically converts `datetime.datetime`, `datetime.date`, and `datetime.time` objects to their string representation.\n- **UUID Encoding**: Encodes `uuid` objects as uuid strings.\n- **Bytes Encoding**: Encodes `bytes` objects as base64 strings, prefixed with `b64:`.\n- **Custom Object Support**: Encodes any object that has a `to_json` method by calling that method.\n\n## Installation\n\nYou can install the package directly from source:\n\n```bash\npip install json-advanced\n```\n\n## Usage\nTo use the JSONSerializer in your project, you need to import it and use it with the standard json module's dump or dumps functions:\n\n```python\nimport json\nimport datetime\nimport uuid\n\nfrom json_advanced.json_encoder import JSONSerializer\n\n# Example object containing various complex data types\ndata = {\n    \"now\": datetime.datetime.now(),\n    \"today\": datetime.date.today(),\n    \"time\": datetime.datetime.now().time(),\n    \"bytes_data\": b\"example bytes\",\n    \"uuid\": uuid.uuid4(),\n}\n\n# Serialize the object to a JSON string\njson_string = json.dumps(data, cls=JSONSerializer)\nprint(json_string)\n```\n\n## Extending the Serializer\nIf you have custom types that you want to serialize, you can extend JSONSerializer by overriding the default method. Ensure you call super().default(obj) for types you do not handle:\n\n```python\nclass MyCustomSerializer(JSONSerializer):\n    def default(self, obj):\n        if isinstance(obj, MyCustomType):\n            return obj.custom_serialize()\n        return super().default(obj)\n```\n\n## Contributions\nContributions are welcome! Please open an issue or pull request on GitHub if you have suggestions or improvements.\n\n## License\nThis package is licensed under the MIT License - see the LICENSE file for details.\n",
    "bugtrack_url": null,
    "license": "Copyright (c) 2024 Mahdi Kiani  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "This Python package provides an extended JSON encoder class, `JSONSerializer`, that enables encoding of complex Python data types such as `datetime.datetime`, `datetime.date`, `datetime.time`, `bytes` and `uuid`. It also supports objects that have a `to_json` method, allowing for customizable JSON encoding.",
    "version": "0.12.5",
    "project_urls": {
        "Bug Reports": "https://github.com/mahdikiani/json-advanced/issues",
        "Homepage": "https://github.com/mahdikiani/json-advanced",
        "Say Thanks!": "https://saythanks.io/to/mahdikiani",
        "Source": "https://github.com/mahdikiani/json-advanced"
    },
    "split_keywords": [
        "json",
        " serialization",
        " datetime",
        " bytes",
        " uuid",
        " path"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1f572df1f5c2c4ee29a19a9736ea9db1dd26f4567e3170d546765b2c5749806d",
                "md5": "df312638ebd9a302669ea73784e2e964",
                "sha256": "bcaf86272741394c277bbdffd6c4d645574786e29e811b793ab077c9b3e8b9ad"
            },
            "downloads": -1,
            "filename": "json_advanced-0.12.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "df312638ebd9a302669ea73784e2e964",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3",
            "size": 5711,
            "upload_time": "2024-10-02T14:43:08",
            "upload_time_iso_8601": "2024-10-02T14:43:08.515504Z",
            "url": "https://files.pythonhosted.org/packages/1f/57/2df1f5c2c4ee29a19a9736ea9db1dd26f4567e3170d546765b2c5749806d/json_advanced-0.12.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0c7a2c1d59e7793c54648dd2c585c57b10cb77c1e5bfef9bb6a6ec1ee829b1ef",
                "md5": "cf893e888d463c96dcaba3d9f375c8f0",
                "sha256": "644e3b1d95a9e868d0db166ef03c5c29ef3b47d085c6fd1e1f0b7f885894309e"
            },
            "downloads": -1,
            "filename": "json_advanced-0.12.5.tar.gz",
            "has_sig": false,
            "md5_digest": "cf893e888d463c96dcaba3d9f375c8f0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3",
            "size": 5497,
            "upload_time": "2024-10-02T14:43:09",
            "upload_time_iso_8601": "2024-10-02T14:43:09.426335Z",
            "url": "https://files.pythonhosted.org/packages/0c/7a/2c1d59e7793c54648dd2c585c57b10cb77c1e5bfef9bb6a6ec1ee829b1ef/json_advanced-0.12.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-02 14:43:09",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "mahdikiani",
    "github_project": "json-advanced",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "json-advanced"
}
        
Elapsed time: 0.32668s