json encoder uses singledispatch pattern instead of JSONEncoder class overwrites
Project description
json encoder uses singledispatch pattern instead of JSONEncoder class overwrites.
No more json.dumps(data, cls=MyJSONEncoder) everywhere.
Comes with default serialization for time, date, datetime, UUID and Decimal
Easy to use, easy to change serialization behaviour
Not tight to any json implementation json, simplejson, ujson …
It parse json float numbers into Decimal objects to prevent python float precision issues.
Installation
$ pip install json-encoder
Quick start
Use “json_encoder.json” instead of default python json:
from json_encoder import json result = json.dumps(data)
Configuration
Chose json implementation:
# simplejson library is used as default json implementation if present # otherwise standard python json implementation is used # to use other json implementation globally, do: import ujson from json_encoder import use_json_library use_json_library(ujson)
To change json implementation for concrete call do:
from json_encoder import json import simplejson result = json.dumps(data, json=simplejson)
To make your object JSON serializable do:
# example how to make python fraction object json serializable from fractions import Fraction from json_encoder.encoder import json_encoder @json_encoder.register(Fraction) def encode_fraction(obj): return '{}/{}'.format(obj.numerator, obj.denominator)
To overwrite JSON serializer behaviour defined in json_encoder.encoder:
from uuid import UUID from six import text_type from json_encoder.encoder import json_encoder @json_encoder.register(UUID) def encode_uuid(obj): return text_type(obj).replace('-', '')
Requirements
singledispatch >= 3.4.0.3 for python version < 3.4 only
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for json_encoder-0.4.4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca5c9aa211ac79d13f125c7d0554ae55a0c872cebc81a61fc4d228e9cc83efd8 |
|
MD5 | 66f7e19eb81ffe3c31d6fd948ea17899 |
|
BLAKE2b-256 | c41b6825d96e98bb90baf0498fdd6b5d2c735e3caf0f944bcb158bc0bd0ba68c |