from django.utils import simplejson


class JSONFieldDescriptor(object):
    def __init__(self, field):
        self.field = field

    def __get__(self, obj, objtype):
        if not hasattr(self, '_cached'):
            try:
                self._cached = simplejson.loads(getattr(obj, self.field))
            except (TypeError, ValueError):
                self._cached = {}
        return self._cached

    def __set__(self, obj, value):
        self._cached = value
        setattr(obj, self.field, simplejson.dumps(value))

from django.utils import simplejson


class JSONFieldDescriptor(object):
    def __init__(self, field):
        self.field = field

    def __get__(self, obj, objtype):
        cache_field = '_cached_jsonfield_%s' % self.field
        if not hasattr(obj, cache_field):
            try:
                setattr(obj, cache_field, simplejson.loads(getattr(obj, self.field)))
            except (TypeError, ValueError):
                setattr(obj, cache_field, {})
        return getattr(obj, cache_field)

    def __set__(self, obj, value):
        setattr(obj, '_cached_jsonfield_%s' % self.field, value)
        setattr(obj, self.field, simplejson.dumps(value))

Fix JSONFieldDescriptor caching