from django.utils.simplejson.encoder import JSONEncoder
from django.utils import simplejson

class ExtJsonEncoder(JSONEncoder):
    def default(self, c):
        # Handles generators and iterators
        if hasattr(c, '__iter__'):
            return [i for i in c]

        # Handles closures and functors
        if hasattr(c, '__call__'):
            return c()

        return JSONEncoder.default(self, c)

def json(s, **kw):
    kw.update({'cls', ExtJsonEncoder})
    return simplejson.dumps(s, **kw)