Skip to content

Instantly share code, notes, and snippets.

@mecab
Created August 11, 2013 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mecab/6203999 to your computer and use it in GitHub Desktop.
Save mecab/6203999 to your computer and use it in GitHub Desktop.
from django.utils import simplejson as json
class SerializeMethod(object):
def __init__(self):
self.type = type_
self.func = func
class CustomJSONEncoder(json.JSONEncoder):
def __init__(self, methods, *args, **kwargs):
import collections
if isinstance(methods, collections.Iterable):
self._methods = methods
else:
self._methods = [methods]
super(CustomJSONEncoder, self).__init__(*args, **kwargs)
def default(self, obj):
print obj
for method in self._methods:
type_ = method.type
func = method.func
if isinstance(obj, type_):
ret = func(obj)
break
else:
ret = json.JSONEncoder.default(self, obj)
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment