Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for custom JSON serializers

  • Loading branch information...
commit a6d435f30ee10b7f40bb0efec8eec59da138f4bb 1 parent ac9e0fb
@alexandermorozov authored
View
8 docs/apidocs/index.txt
@@ -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
@@ -102,4 +108,4 @@ Template tags
.. automodule:: rpc4django.templatetags.rpctags
:members:
-
+
View
9 rpc4django/jsonrpcdispatcher.py
@@ -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):
@@ -63,7 +64,7 @@ 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,
@@ -71,7 +72,7 @@ def _encode_result(self, jsonid, result, error):
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):
'''
@@ -146,4 +147,4 @@ def dispatch(self, json_data, **kwargs):
'" is not supported',
'code': JSONRPC_PROCEDURE_NOT_FOUND_ERROR})
-
+
View
5 rpc4django/rpcdispatcher.py
@@ -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:
View
5 rpc4django/views.py
@@ -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
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.