{ "info": { "author": "Sebastian Schaffer", "author_email": "", "bugtrack_url": null, "classifiers": [ "Development Status :: 2 - Pre-Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Natural Language :: English", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": "======\njsoner\n======\n\n.. image:: https://img.shields.io/travis/sschaffer92/jsoner.svg\n :target: https://travis-ci.org/sschaffer92/jsoner\n\n.. image:: https://readthedocs.org/projects/jsoner/badge/?version=latest\n :target: https://jsoner.readthedocs.io/en/latest/?badge=latest\n :alt: Documentation Status\n\n.. image:: https://coveralls.io/repos/github/sschaffer92/jsoner/badge.svg\n :target: https://coveralls.io/github/sschaffer92/jsoner\n :alt: Coverage\n\n* Free software: MIT license\n\n* Documentation: https://jsoner.readthedocs.io.\n\n*Jsoner* is a package aiming for making conversion to and from json easier.\n\n\nInstallation\n------------\n\n\nStable release\n~~~~~~~~~~~~~~\n\nTo install jsoner, run this command in your terminal:\n\n.. code-block:: console\n\n $ pip install jsoner\n\nThis is the preferred method to install jsoner, as it will always install the most recent stable release.\n\n\nFrom sources\n~~~~~~~~~~~~\n\nThe sources for jsoner can be downloaded from the `Github repo`_.\n\nYou can either clone the public repository:\n\n.. code-block:: console\n\n $ git clone git://github.com/sschaffer92/jsoner\n\nOr download the `tarball`_:\n\n.. code-block:: console\n\n $ curl -OL https://github.com/sschaffer92/jsoner/tarball/master\n\nOnce you have a copy of the source, you can install it with:\n\n.. code-block:: console\n\n $ python setup.py install\n\n\n.. _Github repo: https://github.com/sschaffer92/jsoner\n.. _tarball: https://github.com/sschaffer92/jsoner/tarball/master\n\n\nUsage\n-----\n\n*Jsoner* builds on the builtin *json* python package. Since you cannot serialize object to json by\ndefault it can be useful to have a nice way for doing so. This package provides three different ways to\nachieve this:\n\n- provide an ``to_dict`` and ``from_dict`` method:\n\n.. code-block:: python\n\n from jsoner import dumps, loads\n class A:\n def __init__(self, a):\n self.a = a\n\n def to_dict(self) -> dict:\n return {'a': self.a}\n\n @classmethod\n def from_dict(cls, data: dict) -> 'A':\n return A(**data)\n\n a = A(42)\n data = dumps(a)\n a = loads(data)\n\n\n- or provide an ``to_str`` and ``from_str`` method:\n\n.. code-block:: python\n\n from jsoner import dumps, loads\n class A:\n def __init__(self, a):\n self.a = a\n\n def to_str(self) -> str:\n return str(self.a)\n\n @classmethod\n def from_str(cls, data: str) -> 'A':\n return A(data)\n\n a = A('foo')\n data = dumps(a)\n a = loads(data)\n\n\n- or implement a conversion function pair (This way is especially useful if\n you don't have direct access to the class definition):\n\n.. code-block:: python\n\n from jsoner import dumps, loads\n from jsoner import encoders, decoders\n class A:\n def __init__(self, a):\n self.a = a\n\n @encoders.register(A)\n def encode_a(a: 'A') -> str:\n return a.a\n\n @decoders.register(A)\n def decode_a(data: str) -> str:\n return A(data)\n\n a = A('foo')\n data = dumps(a)\n a = loads(data)\n\n*Jsoner* can also deal with nested objects as long they are also serializable as described above.\n\n\n*Celery* and *Django*\n~~~~~~~~~~~~~~~~~~~~~\n\nOne good use case for the *Jsoner* package is the *Celery* serialization of tasks and task results.\n\nTo make *Celery* use *Jsoner* you can apply the following settings:\n\n.. code-block:: python\n\n from celery import app\n from kombu import serialization\n\n from jsoner import dumps, loads\n\n # register Jsoner\n serialization.register('jsoner', dumps, loads, content_type='application/json')\n\n app = Celery('Test')\n\n # tell celery to use Jsoner\n app.conf.update(\n accept_content=['jsoner'],\n task_serializer='jsoner',\n result_serializer='jsoner',\n result_backend='rpc'\n )\n\n # Celery can now serialize objects which can be serialized by Jsoner.\n class A:\n def __init__(self, foo):\n self.foo = foo\n\n @classmethod\n def from_dict(cls, data: dict) -> 'A':\n return A(**data)\n\n def to_dict(self):\n return {'foo': self.foo}\n\n a = A('bar')\n\n @app.task\n def task(obj: A) -> 'A':\n ...\n return obj\n\n a = task.delay(a).get()\n\n\nThis way you can easily serialize django model instances and pass them to the\n*Celery* task.\n\n.. code-block:: python\n :name: models.py\n\n from django.db import models\n\n class Person(models.Model):\n first_name = models.CharField(max_length=30)\n last_name = models.CharField(max_length=30)\n\n\nThen you can just pass the model to the celery task directly:\n\n.. code-block:: python\n\n from django.db.models import Model\n from jsoner import encoders, decoders\n\n from .models import Person\n\n # Create a conversion function pair which just saved the primary key.\n @encoders.register(Model)\n def to_primary_key(model: Model) -> int:\n return model.pk\n\n # Load object from the primary key.\n @decoders.register(Model)\n def from_primary_key(pk: int, model_cls: Model) -> Model:\n return model_cls.objects.get(pk=pk)\n\n p = Person(first_name=\"Foo\", last_name=\"Bar\")\n p = task.delay(p).get()\n\n\nSimilar you could create a conversion function pair for querysets.\n\n\n=======\nHistory\n=======\n\n0.1.0 (2019-02-18)\n------------------\n\n* First release on PyPI.", "description_content_type": "text/x-rst", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/sschaffer92/jsoner", "keywords": "jsoner json celery django serialization", "license": "MIT license", "maintainer": "", "maintainer_email": "", "name": "jsoner", "package_url": "https://pypi.org/project/jsoner/", "platform": "", "project_url": "https://pypi.org/project/jsoner/", "project_urls": { "Documentation": "https://jsoner.readthedocs.io/en/latest/", "Homepage": "https://github.com/sschaffer92/jsoner", "Source": "https://github.com/sschaffer92/jsoner/", "Tracker": "https://github.com/sschaffer92/jsoner/issues" }, "release_url": "https://pypi.org/project/jsoner/0.2.0/", "requires_dist": null, "requires_python": ">=3.4", "summary": "Jsoner allows you to easily convert your classes to json and back.", "version": "0.2.0" }, "last_serial": 5005211, "releases": { "0.2.0": [ { "comment_text": "", "digests": { "md5": "64110a3e0243aa8cf6127094e931a628", "sha256": "d838411db82ff0bf5674e04852664762158b3a4c61144d318005e0fd3828f2ce" }, "downloads": -1, "filename": "jsoner-0.2.0.tar.gz", "has_sig": false, "md5_digest": "64110a3e0243aa8cf6127094e931a628", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.4", "size": 26105, "upload_time": "2019-03-29T21:49:56", "url": "https://files.pythonhosted.org/packages/8b/f1/b9154c7bb3fd86dc1c4de6484f378d25acdc0c05fcb0f3411bcbcc98eefe/jsoner-0.2.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "64110a3e0243aa8cf6127094e931a628", "sha256": "d838411db82ff0bf5674e04852664762158b3a4c61144d318005e0fd3828f2ce" }, "downloads": -1, "filename": "jsoner-0.2.0.tar.gz", "has_sig": false, "md5_digest": "64110a3e0243aa8cf6127094e931a628", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.4", "size": 26105, "upload_time": "2019-03-29T21:49:56", "url": "https://files.pythonhosted.org/packages/8b/f1/b9154c7bb3fd86dc1c4de6484f378d25acdc0c05fcb0f3411bcbcc98eefe/jsoner-0.2.0.tar.gz" } ] }