Created
August 11, 2013 08:35
-
-
Save mecab/6203999 to your computer and use it in GitHub Desktop.
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
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