{ "info": { "author": "Bastien Pietropaoli", "author_email": "bastien.pietropaoli@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Topic :: Utilities" ], "description": "# Universal JSON Encoder/Decoder for Python Objects #\n\nThis small Python package enables encoding / decoding most Python objects into / from JSON. It offers an API similar to the `json` standard library package but provides automated functionalities to encode / decode most objects that cannot be converted into JSON by default (e.g. datetimes or custom classes). To do this, it adds two properties `__class__` and `__module__` to the generated JSON for all types normally not supported.\n\nThe rationale for this package is pretty simple: I like to be able to dump/load my objects in an easily readable format pretty much anywhere (e.g. a DB, another program, another Python program). This is particularly practical to use with applications using MongoDB.\n\nThis package works with Python version 2.7, 3.4, 3.5, 3.6 and 3.7 (the unit tests passed for all those versions). To install it, clone the git and simply use `python setup.py install` OR you can also install it via `pip install unijson` since it's also registered on PyPi [here](https://pypi.org/project/unijson/).\n\nHere is what you can do with this little package.\n\n## The types already supported by `json` are unchanged ##\n\nAll the types already supported by `json` are supported by `unijson` and behave in the exact same way since `UniversalJSONEncoder` uses the default `JSONEncoder` for all the types already supported.\n\n```python\nimport json, unijson\n\nd = {\"peuh\": 123, \"pouet\": [True, False, None, \"ruguhregr\"]}\n\n# Both will return the same string:\njson.dumps(d)\nunijson.dumps(d)\n```\n\n## Automatically find a way to encode your custom classes ##\n\nUsing `unijson`, converting your custom classes instances into JSON objects has never been more simple.\nFor most simple classes, the `UniversalJSONEncoder` will find a way to transform them into JSON compliant objects without the need for extra methods.\n\n```python\n# Define a custom class\nclass Test(object):\n def __init__(self, a1, a2):\n self.a1, self.a2 = a1, a2\n\no1 = Test(12, 34)\n\n# json.dumps(o1) would raise an exception.\n# TypeError: Object of type 'Test' is not JSON serializable\n\n# unijson won't blink:\nunijson.dumps(o1) # {\"a1\": 12, \"a2\": 34, \"__class__\": \"Test\", \"__module__\": \"examples\"}\n```\n\n## Define encoders / decoders for your custom classes ##\n\nWith `unijson`, providing encoding / decoding functions for your custom classes is really simple. You just implement `__json_encode__()` / `__json_decode__()` in your custom classes. They will then be taken into account automatically by the `UniversalJSONEncoder` / `UniversalJSONDecoder`. This can be used to add some extra information into the resulting JSON object, or to provide a way to transform complex object into a JSON-serialisable dictionnary.\n\n```python\n# Define a custom class that defines methods for the universal serialiser:\nclass DefineMethods(object):\n def __init__(self, a1, a2):\n self.a1, self.a2 = a1, a2\n\n def __json_encode__(self):\n return {\"a1\":self.a1, \"a2\":self.a2, \"extra\":\"for the fun\"}\n\n @staticmethod\n def __json_decode__(d):\n return DefineMethods(d[\"a1\"], d[\"a2\"])\n\n def __eq__(self, other):\n return self.__dict__ == other.__dict__\n\no = DefineMethods(\"whatever\", False)\ns = unijson.dumps(o) # '{\"a1\": \"whatever\", \"a2\": false, \"extra\": \"for the fun\", \"__class__\": \"DefineMethods\", \"__module__\": \"examples\"}'\nd = unijson.loads(s) # o == d should be True\n```\n\nHere is what you need to know on the two methods illustrated above:\n* `__json_encode__()`: should take no argument and return a dictionary of unijson-serialisable objects (remember that many objects are unijson-serialisable by default!).\n* `__json_decode__()`: should be static, take a single argument (a dict), and return an instance of the class it's defined in.\n\n## Define and register encoders / decoders for any class ##\n\nIf you use external libraries, or non-JSON-serialisable types from the standard library (e.g. datetime), you might find yourself stuck on a simple way to convert those objects into JSON. Worry no more, my friend, for `unijson` has a solution for that. You can register encoding / decoding functions to the `UniversalJSONEncoder` / `UniversalJSONDecoder` to specify how to encode / decode anything. `unijson` already offers a few extra encoders / decoders for all the types found in `datetime` and `pytz` (for the management of timezones). If you have a look at `unijson`'s code, you will find those encoders. Here is an example that demonstrates how to register encoding functions:\n\n```python\n#Encoding function:\ndef json_encode_date(d):\n \"\"\"Encoder for dates (from module datetime).\"\"\"\n return {\"day\" : d.day,\n \"month\" : d.month,\n \"year\" : d.year}\nUniversalJSONEncoder.register(datetime.date, json_encode_date)\n# Won't even require a decoding function since the created dictionnary can be used as arguments to the constructor (that's one of the automated methods used by UniversalJSONDecoder to build objects).\n```\n\nIf you want to add your own encoders / decoders and register them, the functions you register must do the following:\n* encoders should take single argument (the object to encode) and return a dictionary of UniJSON-serialisable objects.\n* decoders should take a single argument (the dict extracted by the decoder) and return an instance of the decoded object.\n\n# Additional information #\n\nAuthor: Bastien Pietropaoli\n\nContact: bastien.pietropaoli@gmail.com\n\nLicense: [Apache v2.0](http://www.apache.org/licenses/LICENSE-2.0)\n\nIf you have suggestions, remarks, or questions, do not hesitate to contact me.\nAlso, if you have made nice encoders/decoders that you think could be a good addition to this package, feel free to submit a pull request.\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/bpietropaoli/unijson", "keywords": "", "license": "Apache v2.0", "maintainer": "", "maintainer_email": "", "name": "unijson", "package_url": "https://pypi.org/project/unijson/", "platform": "", "project_url": "https://pypi.org/project/unijson/", "project_urls": { "Homepage": "https://github.com/bpietropaoli/unijson" }, "release_url": "https://pypi.org/project/unijson/1.0.0/", "requires_dist": [ "pytz", "parse" ], "requires_python": "", "summary": "Universal JSON encoder/decoder for Python objects", "version": "1.0.0" }, "last_serial": 4170508, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "94fa955ab3b500710c73c573b047b7e8", "sha256": "3eb5be368c816313b8509440044b9f7d464c10e262432b9ec43e228d713769cf" }, "downloads": -1, "filename": "unijson-1.0.0-py3-none-any.whl", "has_sig": false, "md5_digest": "94fa955ab3b500710c73c573b047b7e8", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8908, "upload_time": "2018-08-14T18:40:25", "url": "https://files.pythonhosted.org/packages/fc/57/23f2d15825c637e8159c57da7c14090df76fa78efcb4200cbc4e8fb65089/unijson-1.0.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ba02b43fd7726c440fc279a032d9a38e", "sha256": "3e8342f8c859e4608335ad0ff8c24fc130d45d327c58e6b22139ffaf8b63fd34" }, "downloads": -1, "filename": "unijson-1.0.0.tar.gz", "has_sig": false, "md5_digest": "ba02b43fd7726c440fc279a032d9a38e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8614, "upload_time": "2018-08-14T18:40:26", "url": "https://files.pythonhosted.org/packages/be/6c/e266fa1990623639b6eb8e5dd5105ef8ad08de1f8f753a4eea2a6eed5897/unijson-1.0.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "94fa955ab3b500710c73c573b047b7e8", "sha256": "3eb5be368c816313b8509440044b9f7d464c10e262432b9ec43e228d713769cf" }, "downloads": -1, "filename": "unijson-1.0.0-py3-none-any.whl", "has_sig": false, "md5_digest": "94fa955ab3b500710c73c573b047b7e8", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8908, "upload_time": "2018-08-14T18:40:25", "url": "https://files.pythonhosted.org/packages/fc/57/23f2d15825c637e8159c57da7c14090df76fa78efcb4200cbc4e8fb65089/unijson-1.0.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ba02b43fd7726c440fc279a032d9a38e", "sha256": "3e8342f8c859e4608335ad0ff8c24fc130d45d327c58e6b22139ffaf8b63fd34" }, "downloads": -1, "filename": "unijson-1.0.0.tar.gz", "has_sig": false, "md5_digest": "ba02b43fd7726c440fc279a032d9a38e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8614, "upload_time": "2018-08-14T18:40:26", "url": "https://files.pythonhosted.org/packages/be/6c/e266fa1990623639b6eb8e5dd5105ef8ad08de1f8f753a4eea2a6eed5897/unijson-1.0.0.tar.gz" } ] }