Skip to content

Commit

Permalink
Added support for custom JSON serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandermorozov committed Nov 29, 2011
1 parent ac9e0fb commit a6d435f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
8 changes: 7 additions & 1 deletion docs/apidocs/index.txt
Expand Up @@ -70,6 +70,12 @@ functionality of RPC4Django
allowing cross site requests. See the Mozilla documentation on
`preflighted requests`_ for more details. Defaults to the empty string.

.. envvar:: RPC4DJANGO_JSON_OBJECT_SERIALIZER

This function converts its single argument into object, which could be
serialized to JSON, or raises ``TypeError``. Can be used to convert
instances of ``datetime.datetime`` into strings with required format.

.. _requests with credentials: https://developer.mozilla.org/en/HTTP_access_control#Requests_with_credentials
.. _preflighted requests: https://developer.mozilla.org/en/HTTP_access_control#Preflighted_requests

Expand Down Expand Up @@ -102,4 +108,4 @@ Template tags

.. automodule:: rpc4django.templatetags.rpctags
:members:


9 changes: 5 additions & 4 deletions rpc4django/jsonrpcdispatcher.py
Expand Up @@ -40,7 +40,8 @@ class JSONRPCDispatcher:
or error.
'''

def __init__(self):
def __init__(self, object_serializer):
self.object_serializer = object_serializer
self.methods = {}

def register_function(self, method, external_name):
Expand All @@ -63,15 +64,15 @@ def _encode_result(self, jsonid, result, error):
res['result'] = None

try:
return json.dumps(res, indent=JSON_INDENT)
return json.dumps(res, indent=JSON_INDENT, default=self.object_serializer)
except:
err = {'message': 'failed to encode return value',
'code': JSONRPC_SERVICE_ERROR,
'name': 'JSONRPCError'}

res['result'] = None
res['error'] = err
return json.dumps(res, indent=JSON_INDENT)
return json.dumps(res, indent=JSON_INDENT, default=self.object_serializer)

def dispatch(self, json_data, **kwargs):
'''
Expand Down Expand Up @@ -146,4 +147,4 @@ def dispatch(self, json_data, **kwargs):
'" is not supported',
'code': JSONRPC_PROCEDURE_NOT_FOUND_ERROR})



5 changes: 3 additions & 2 deletions rpc4django/rpcdispatcher.py
Expand Up @@ -210,11 +210,12 @@ class RPCDispatcher:
'''

def __init__(self, url='', apps=[], restrict_introspection=False, restrict_ootb_auth=True):
def __init__(self, url='', apps=[], restrict_introspection=False,
restrict_ootb_auth=True, json_object_serializer=None):
version = platform.python_version_tuple()
self.url = url
self.rpcmethods = [] # a list of RPCMethod objects
self.jsonrpcdispatcher = JSONRPCDispatcher()
self.jsonrpcdispatcher = JSONRPCDispatcher(json_object_serializer)
self.xmlrpcdispatcher = XMLRPCDispatcher()

if not restrict_introspection:
Expand Down
5 changes: 4 additions & 1 deletion rpc4django/views.py
Expand Up @@ -42,6 +42,8 @@
'RPC4DJANGO_HTTP_ACCESS_CREDENTIALS', False)
HTTP_ACCESS_ALLOW_ORIGIN = getattr(settings,
'RPC4DJANGO_HTTP_ACCESS_ALLOW_ORIGIN', '')
JSON_OBJECT_SERIALIZER = getattr(settings,
'RPC4DJANGO_JSON_OBJECT_SERIALIZER', None)

# get a list of the installed django applications
# these will be scanned for @rpcmethod decorators
Expand Down Expand Up @@ -235,5 +237,6 @@ def serve_rpc_request(request):

# instantiate the rpcdispatcher -- this examines the INSTALLED_APPS
# for any @rpcmethod decorators and adds them to the callable methods
dispatcher = RPCDispatcher(URL, APPS, RESTRICT_INTROSPECTION, RESTRICT_OOTB_AUTH)
dispatcher = RPCDispatcher(URL, APPS, RESTRICT_INTROSPECTION,
RESTRICT_OOTB_AUTH, JSON_OBJECT_SERIALIZER)

0 comments on commit a6d435f

Please sign in to comment.