https://github.com/zmap/zson
A python library that allows easily encoding and decoding objects into JSON
https://github.com/zmap/zson
Last synced: about 1 year ago
JSON representation
A python library that allows easily encoding and decoding objects into JSON
- Host: GitHub
- URL: https://github.com/zmap/zson
- Owner: zmap
- Created: 2014-03-18T20:54:00.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2015-06-30T15:54:54.000Z (about 11 years ago)
- Last Synced: 2024-03-26T19:08:22.401Z (over 2 years ago)
- Language: Python
- Homepage:
- Size: 194 KB
- Stars: 7
- Watchers: 21
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ZSON
## Installation
Install the egg as you would any other egg.
```
pip install zson
```
or
```
easy_install zson
```
## Usage
If you want an object to be JSON decodable, you need to define a to_json instance method that returns a dict of json-encodable objects. If you want your object to be encodable, define a from_json class method that takes back that dictionary. Yes, you can do that recursively and put a json-encodable object in your dict. And that's it. You must also use a newstyle class. It's 2014... if you're not already, you're _actually_ doing something wrong. You also get free json-able datetime objects... because really, it's pretty freaking nonsensical that this doesn't work out of the box. An example is below if it would help.
```python
class MyObject(object):
def __init__(self, name):
self._name = name
def to_json(self):
return {'name':self._name}
@classmethod
def from_json(self, obj):
return cls(obj["name"])
```
## Celery Usage
Zson was originally written to allow objects to be passed in Celery. If you want to use zson as your serializer in Celery, you can set this by creating a configuration file and adding
```
CELERY_TASK_SERIALIZER = 'zson'
CELERY_RESULT_SERIALIZER = 'zson'
CELERY_ACCEPT_CONTENT = ["zson"]
```
and then loading this configuration file when you configure your Celery app:
```python
c = celery.Celery('zsearch', backend='amqp', broker='amqp://')
c.config_from_object('celeryconfig')
```