Skip to main content

Jsoner allows you to easily convert your classes to json and back.

Project description

jsoner Documentation Status Coverage

Jsoner is a package aiming for making conversion to and from json easier.


Stable release

To install jsoner, run this command in your terminal:

$ pip install jsoner

This is the preferred method to install jsoner, as it will always install the most recent stable release.

From sources

The sources for jsoner can be downloaded from the Github repo.

You can either clone the public repository:

$ git clone git://

Or download the tarball:

$ curl  -OL

Once you have a copy of the source, you can install it with:

$ python install


Jsoner builds on the builtin json python package. Since you cannot serialize object to json by default it can be useful to have a nice way for doing so. This package provides three different ways to achieve this:

  • provide an to_dict and from_dict method:
from jsoner import dumps, loads
class A:
    def __init__(self, a):
        self.a = a

    def to_dict(self) -> dict:
        return {'a': self.a}

    def from_dict(cls, data: dict) -> 'A':
        return A(**data)

a = A(42)
data = dumps(a)
a = loads(data)
  • or provide an to_str and from_str method:
from jsoner import dumps, loads
class A:
    def __init__(self, a):
        self.a = a

    def to_str(self) -> str:
        return str(self.a)

    def from_str(cls, data: str) -> 'A':
        return A(data)

a = A('foo')
data = dumps(a)
a = loads(data)
  • or implement a conversion function pair (This way is especially useful if you don’t have direct access to the class definition):
from jsoner import dumps, loads
from jsoner import encoders, decoders
class A:
    def __init__(self, a):
        self.a = a

def encode_a(a: 'A') -> str:
    return a.a

def decode_a(data: str) -> str:
    return A(data)

a = A('foo')
data = dumps(a)
a = loads(data)

Jsoner can also deal with nested objects as long they are also serializable as described above.

Celery and Django

One good use case for the Jsoner package is the Celery serialization of tasks and task results.

To make Celery use Jsoner you can apply the following settings:

from celery import app
from kombu import serialization

from jsoner import dumps, loads

# register Jsoner
serialization.register('jsoner', dumps, loads, content_type='application/json')

app = Celery('Test')

# tell celery to use Jsoner

# Celery can now serialize objects which can be serialized by Jsoner.
class A:
    def __init__(self, foo): = foo

    def from_dict(cls, data: dict) -> 'A':
        return A(**data)

    def to_dict(self):
        return {'foo':}

a = A('bar')

def task(obj: A) -> 'A':
    return obj

a = task.delay(a).get()

This way you can easily serialize django model instances and pass them to the Celery task.

 from django.db import models

 class Person(models.Model):
     first_name = models.CharField(max_length=30)
     last_name = models.CharField(max_length=30)

Then you can just pass the model to the celery task directly:

from django.db.models import Model
from jsoner import encoders, decoders

from .models import Person

# Create a conversion function pair which just saved the primary key.
def to_primary_key(model: Model) -> int:

# Load object from the primary key.
def from_primary_key(pk: int, model_cls: Model) -> Model:
    return model_cls.objects.get(pk=pk)

p = Person(first_name="Foo", last_name="Bar")
p = task.delay(p).get()

Similar you could create a conversion function pair for querysets.


0.1.0 (2019-02-18)

  • First release on PyPI.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for jsoner, version 0.2.0
Filename, size File type Python version Upload date Hashes
Filename, size jsoner-0.2.0.tar.gz (26.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page