Skip to content

Instantly share code, notes, and snippets.

@pmclanahan
Created July 3, 2012 13:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmclanahan/3039696 to your computer and use it in GitHub Desktop.
Save pmclanahan/3039696 to your computer and use it in GitHub Desktop.
A Compressed JSON Field for Django.
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
@pmclanahan
Copy link
Author

I found that compression rates are better for JSON encodable data using zlib on the JSON string than a compressed pickle or marshal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment