[![Coverage Status](https://coveralls.io/repos/github/zincware/ZnJSON/badge.svg?branch=main)](https://coveralls.io/github/zincware/ZnJSON?branch=main)
[![Code Style](https://img.shields.io/badge/code%20style-black-black)](https://github.com/psf/black/)
[![Tests](https://github.com/zincware/ZnJSON/actions/workflows/pytest.yaml/badge.svg)](https://coveralls.io/github/zincware/ZnJSON?branch=main)
[![PyPI version](https://badge.fury.io/py/znjson.svg)](https://badge.fury.io/py/znjson)
# ZnJSON
Package to Encode/Decode some common file formats to json
Available via ``pip install znjson``
In comparison to `pickle` this allows having readable json files combined with some
serialized data.
# Example
````python
import numpy as np
import json
import znjson
data = json.dumps(
obj={"data_np": np.arange(2), "data": [x for x in range(10)]},
cls=znjson.ZnEncoder,
indent=4
)
_ = json.loads(data, cls=znjson.ZnDecoder)
````
The resulting ``*.json`` file is partially readable and looks like this:
````json
{
"data_np": {
"_type": "np.ndarray_small",
"value": [
0,
1
]
},
"data": [
0,
1,
2,
3,
4
]
}
````
# Custom Converter
ZnJSON allows you to easily add custom converters.
Let's write a serializer for ``datetime.datetime``.
````python
from znjson import ConverterBase
from datetime import datetime
class DatetimeConverter(ConverterBase):
"""Encode/Decode datetime objects
Attributes
----------
level: int
Priority of this converter over others.
A higher level will be used first, if there
are multiple converters available
representation: str
An unique identifier for this converter.
instance:
Used to select the correct converter.
This should fulfill isinstance(other, self.instance)
or __eq__ should be overwritten.
"""
level = 100
representation = "datetime"
instance = datetime
def encode(self, obj: datetime) -> str:
"""Convert the datetime object to str / isoformat"""
return obj.isoformat()
def decode(self, value: str) -> datetime:
"""Create datetime object from str / isoformat"""
return datetime.fromisoformat(value)
````
This allows us to use this new serializer:
````python
znjson.config.register(DatetimeConverter) # we need to register the new converter first
json_string = json.dumps(dt, cls=znjson.ZnEncoder, indent=4)
json.loads(json_string, cls=znjson.ZnDecoder)
````
and will result in
````json
{
"_type": "datetime",
"value": "2022-03-11T09:47:35.280331"
}
````
If you don't want to register your converter to be used everywhere, simply use:
```python
json_string = json.dumps(dt, cls=znjson.ZnEncoder.from_converters(DatetimeConverter))
```
Raw data
{
"_id": null,
"home_page": null,
"name": "znjson",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": "json, zntrack, jsonpickle, serialization, deserialization",
"author": "zincwarecode",
"author_email": "zincwarecode@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/e2/d6/6041b375b2e5e789bf99ff4b2f4f179f1328fe8221e209c87376461b8676/znjson-0.2.6.tar.gz",
"platform": null,
"description": "[![Coverage Status](https://coveralls.io/repos/github/zincware/ZnJSON/badge.svg?branch=main)](https://coveralls.io/github/zincware/ZnJSON?branch=main)\n[![Code Style](https://img.shields.io/badge/code%20style-black-black)](https://github.com/psf/black/)\n[![Tests](https://github.com/zincware/ZnJSON/actions/workflows/pytest.yaml/badge.svg)](https://coveralls.io/github/zincware/ZnJSON?branch=main)\n[![PyPI version](https://badge.fury.io/py/znjson.svg)](https://badge.fury.io/py/znjson)\n\n\n# ZnJSON\n\nPackage to Encode/Decode some common file formats to json\n\nAvailable via ``pip install znjson``\n\nIn comparison to `pickle` this allows having readable json files combined with some\nserialized data.\n\n# Example\n\n````python\nimport numpy as np\nimport json\nimport znjson\n\ndata = json.dumps(\n obj={\"data_np\": np.arange(2), \"data\": [x for x in range(10)]},\n cls=znjson.ZnEncoder,\n indent=4\n)\n_ = json.loads(data, cls=znjson.ZnDecoder)\n````\nThe resulting ``*.json`` file is partially readable and looks like this:\n\n````json\n{\n \"data_np\": {\n \"_type\": \"np.ndarray_small\",\n \"value\": [\n 0,\n 1\n ]\n },\n \"data\": [\n 0,\n 1,\n 2,\n 3,\n 4\n ]\n}\n````\n\n# Custom Converter\n\nZnJSON allows you to easily add custom converters.\nLet's write a serializer for ``datetime.datetime``. \n\n````python\nfrom znjson import ConverterBase\nfrom datetime import datetime\n\nclass DatetimeConverter(ConverterBase):\n \"\"\"Encode/Decode datetime objects\n\n Attributes\n ----------\n level: int\n Priority of this converter over others.\n A higher level will be used first, if there\n are multiple converters available\n representation: str\n An unique identifier for this converter.\n instance:\n Used to select the correct converter.\n This should fulfill isinstance(other, self.instance)\n or __eq__ should be overwritten.\n \"\"\"\n level = 100\n representation = \"datetime\"\n instance = datetime\n\n def encode(self, obj: datetime) -> str:\n \"\"\"Convert the datetime object to str / isoformat\"\"\"\n return obj.isoformat()\n def decode(self, value: str) -> datetime:\n \"\"\"Create datetime object from str / isoformat\"\"\"\n return datetime.fromisoformat(value)\n````\n\nThis allows us to use this new serializer:\n````python\nznjson.config.register(DatetimeConverter) # we need to register the new converter first\njson_string = json.dumps(dt, cls=znjson.ZnEncoder, indent=4)\njson.loads(json_string, cls=znjson.ZnDecoder)\n````\n\nand will result in\n````json\n{\n \"_type\": \"datetime\",\n \"value\": \"2022-03-11T09:47:35.280331\"\n}\n````\n\nIf you don't want to register your converter to be used everywhere, simply use:\n\n```python\njson_string = json.dumps(dt, cls=znjson.ZnEncoder.from_converters(DatetimeConverter))\n```",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "A Python Package to Encode/Decode some common file formats to json",
"version": "0.2.6",
"project_urls": {
"repository": "https://github.com/zincware/ZnJSON"
},
"split_keywords": [
"json",
" zntrack",
" jsonpickle",
" serialization",
" deserialization"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "35ed4874212e62e00dac96e84e68fe8465a3e50e512e8e2984c0a874298522da",
"md5": "7f74050c7f4ec4b3fbf165c99d7fa197",
"sha256": "144cc842161cabf6bc97ec8908dd99b2ef37faa4ecc4083d1ad816654a5527e0"
},
"downloads": -1,
"filename": "znjson-0.2.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7f74050c7f4ec4b3fbf165c99d7fa197",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 15303,
"upload_time": "2024-10-11T17:01:07",
"upload_time_iso_8601": "2024-10-11T17:01:07.922578Z",
"url": "https://files.pythonhosted.org/packages/35/ed/4874212e62e00dac96e84e68fe8465a3e50e512e8e2984c0a874298522da/znjson-0.2.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e2d66041b375b2e5e789bf99ff4b2f4f179f1328fe8221e209c87376461b8676",
"md5": "0ce2810ebe9da70aff7395b2967949f6",
"sha256": "b9a5360bc598018ea82972c595b953edc6cc0b3f860fbaff46034266461372ea"
},
"downloads": -1,
"filename": "znjson-0.2.6.tar.gz",
"has_sig": false,
"md5_digest": "0ce2810ebe9da70aff7395b2967949f6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 12496,
"upload_time": "2024-10-11T17:01:09",
"upload_time_iso_8601": "2024-10-11T17:01:09.286920Z",
"url": "https://files.pythonhosted.org/packages/e2/d6/6041b375b2e5e789bf99ff4b2f4f179f1328fe8221e209c87376461b8676/znjson-0.2.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-11 17:01:09",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "zincware",
"github_project": "ZnJSON",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "znjson"
}