Package: jsoner
jsonerFree software: MIT licenseDocumentation:https://jsoner.readthedocs.io.Jsoneris a package aiming for making conversion to and from json easier.InstallationStable releaseTo install jsoner, run this command in your terminal:$pipinstalljsonerThis is the preferred method to install jsoner, as it will always install the most recent stable release.From sourcesThe sources for jsoner can be downloaded from theGithub repo.You can either clone the public repository:$gitclonegit://github.com/sschaffer92/jsonerOr download thetarball:$curl-OLhttps://github.com/sschaffer92/jsoner/tarball/masterOnce you have a copy of the source, you can install it with:$pythonsetup.pyinstallUsageJsonerbuilds on the builtinjsonpython 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 anto_dictandfrom_dictmethod:fromjsonerimportdumps,loadsclassA:def__init__(self,a):self.a=adefto_dict(self)->dict:return{'a':self.a}@classmethoddeffrom_dict(cls,data:dict)->'A':returnA(**data)a=A(42)data=dumps(a)a=loads(data)or provide anto_strandfrom_strmethod:fromjsonerimportdumps,loadsclassA:def__init__(self,a):self.a=adefto_str(self)->str:returnstr(self.a)@classmethoddeffrom_str(cls,data:str)->'A':returnA(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):fromjsonerimportdumps,loadsfromjsonerimportencoders,decodersclassA:def__init__(self,a):self.a=a@encoders.register(A)defencode_a(a:'A')->str:returna.a@decoders.register(A)defdecode_a(data:str)->str:returnA(data)a=A('foo')data=dumps(a)a=loads(data)Jsonercan also deal with nested objects as long they are also serializable as described above.CeleryandDjangoOne good use case for theJsonerpackage is theCeleryserialization of tasks and task results.To makeCeleryuseJsoneryou can apply the following settings:fromceleryimportappfromkombuimportserializationfromjsonerimportdumps,loads# register Jsonerserialization.register('jsoner',dumps,loads,content_type='application/json')app=Celery('Test')# tell celery to use Jsonerapp.conf.update(accept_content=['jsoner'],task_serializer='jsoner',result_serializer='jsoner',result_backend='rpc')# Celery can now serialize objects which can be serialized by Jsoner.classA:def__init__(self,foo):self.foo=foo@classmethoddeffrom_dict(cls,data:dict)->'A':returnA(**data)defto_dict(self):return{'foo':self.foo}a=A('bar')@app.taskdeftask(obj:A)->'A':...returnobja=task.delay(a).get()This way you can easily serialize django model instances and pass them to theCelerytask.fromdjango.dbimportmodelsclassPerson(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:fromdjango.db.modelsimportModelfromjsonerimportencoders,decodersfrom.modelsimportPerson# Create a conversion function pair which just saved the primary key.@encoders.register(Model)defto_primary_key(model:Model)->int:returnmodel.pk# Load object from the primary key.@decoders.register(Model)deffrom_primary_key(pk:int,model_cls:Model)->Model:returnmodel_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.History0.1.0 (2019-02-18)First release on PyPI.