Skip to content

Commit

Permalink
Modified return type checking logic to actually support pluggable
Browse files Browse the repository at this point in the history
json encoders.
  • Loading branch information
Marco Pantaleoni committed Apr 7, 2010
1 parent 66c00e4 commit 6b88b0d
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions jsonrpc/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def validate_get(self, request, method):
return True, D
return False, {}

def response_dict(self, request, D, is_batch=False, version_hint='1.0'):
def response_dict(self, request, D, is_batch=False, version_hint='1.0', json_encoder=None):
json_encoder = json_encoder or self.json_encoder
version = version_hint
response = self.empty_response(version=version)
apply_version = {'2.0': lambda f, r, p: f(r, **encode_kw(p)) if type(p) is dict else f(r, *p),
Expand Down Expand Up @@ -144,10 +145,16 @@ def response_dict(self, request, D, is_batch=False, version_hint='1.0'):
validate_params(method, D)
R = apply_version[version](method, request, D['params'])

assert sum(map(lambda e: isinstance(R, e),
(dict, str, unicode, int, long, list, set, NoneType, bool))), \
"Return type not supported"

encoder = json_encoder()
try:
rs = encoder.default(R)
except TypeError, exc:
raise TypeError("Return type not supported, for %r" % R)

if not sum(map(lambda e: isinstance(rs, e),
(dict, str, unicode, int, long, list, set, NoneType, bool))):
raise TypeError("Return type not supported, for %r" % rs)

if 'id' in D and D['id'] is not None: # regular request
response['result'] = R
response['id'] = D['id']
Expand Down Expand Up @@ -197,10 +204,10 @@ def dispatch(self, request, method='', json_encoder=None):
raise InvalidRequestError

if type(D) is list:
response = [self.response_dict(request, d, is_batch=True)[0] for d in D]
response = [self.response_dict(request, d, is_batch=True, json_encoder=json_encoder)[0] for d in D]
status = 200
else:
response, status = self.response_dict(request, D)
response, status = self.response_dict(request, D, json_encoder=json_encoder)
if response is None and (not u'id' in D or D[u'id'] is None): # a notification
return HttpResponse('', status=status)

Expand Down

0 comments on commit 6b88b0d

Please sign in to comment.