Created
July 3, 2012 13:27
-
-
Save pmclanahan/3039696 to your computer and use it in GitHub Desktop.
A Compressed JSON Field for Django.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zlib | |
from base64 import b64decode, b64encode | |
from django.db import models | |
# django-jsonfield | |
from jsonfield import JSONField | |
class CompressedJSONField(JSONField): | |
""" | |
Django model field that stores JSON data compressed with zlib. | |
""" | |
__metaclass__ = models.SubfieldBase | |
def to_python(self, value): | |
if isinstance(value, basestring): | |
try: | |
value = zlib.decompress(b64decode(value)) | |
except zlib.error: | |
pass | |
return super(CompressedJSONField, self).to_python(value) | |
def get_db_prep_value(self, value, connection=None, prepared=None): | |
value = super(CompressedJSONField, self).get_db_prep_value(value, | |
connection, | |
prepared) | |
return b64encode(zlib.compress(value, 9)) | |
# South support | |
try: | |
from south.modelsinspector import add_introspection_rules | |
# you may need to modify this regex to match where you install this file | |
add_introspection_rules([], ['fields\.CompressedJSONField']) | |
except ImportError: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found that compression rates are better for JSON encodable data using zlib on the JSON string than a compressed pickle or marshal.