Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nocarryr/json-object-factory
Simplify JSON Encoders and Object Hooks
https://github.com/nocarryr/json-object-factory
Last synced: about 1 month ago
JSON representation
Simplify JSON Encoders and Object Hooks
- Host: GitHub
- URL: https://github.com/nocarryr/json-object-factory
- Owner: nocarryr
- License: mit
- Created: 2016-05-31T17:19:58.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-06-01T15:50:00.000Z (over 8 years ago)
- Last Synced: 2024-10-31T02:07:45.236Z (3 months ago)
- Language: Python
- Size: 15.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/nocarryr/json-object-factory.svg?branch=master)](https://travis-ci.org/nocarryr/json-object-factory) [![Coverage Status](https://coveralls.io/repos/github/nocarryr/json-object-factory/badge.svg?branch=master)](https://coveralls.io/github/nocarryr/json-object-factory?branch=master)
# json-object-factory
Simplifies building custom encoders and object-hooks for Python's default JSON implementation.## Installation
```
$ pip install json-object-factory
```
## Usage
Add encoders and decoders to the registry:
```python
import jsonfactoryclass MyJsonHandler(object):
def encode(self, o):
if isinstance(o, MyCustomClass):
return o.serialize()
return Nonedef decode(self, d):
if 'some_custom_key' in d:
return MyCustomClass(**d)
return djsonfactory.Registry.register(MyJsonHandler)
```
Or use the included decorators:
```python
@jsonfactory.register
class MyOtherJsonHandler(object):
...@jsonfactory.encoder
def an_encoder_function(o):
...@jsonfactory.decoder
def a_decoder_function(d):
...
```
Then use the module's `dumps` and `loads` functions:
```python
json_str = jsonfactory.dumps(obj_dict, indent=2)new_obj_dict = jsonfactory.loads(json_str)
```## Notes
* The calling signature for encoder functions follows that of the built-in [JSONEncoder](https://docs.python.org/3.5/library/json.html#json.JSONEncoder) with one exception:
* If no modifications are needed and the object should be passed to the base encoder's handler, `None` should be returned. This differs from the normal method of calling `super(MyEncoder, self).default(o)` (that would most likely be an error since subclassing `JSONEncoder` isn't necessary).
* The signature for decoder functions follows the `object_hook` signature in [the built-in implementation](https://docs.python.org/3.5/library/json.html#json.load)