Permalink
Browse files

Replaced serializing function with encoder class

  • Loading branch information...
1 parent 699c08b commit c53e64f28e008d24311b858d399eb7df4725b2c4 @alexandermorozov committed Jan 25, 2012
Showing with 19 additions and 24 deletions.
  1. +5 −6 docs/apidocs/index.txt
  2. +4 −5 rpc4django/jsonrpcdispatcher.py
  3. +2 −2 rpc4django/rpcdispatcher.py
  4. +8 −11 rpc4django/views.py
@@ -70,13 +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
+.. envvar:: RPC4DJANGO_JSON_ENCODER
- Function or string with path to function, which is called when JSON
- serializer cannot convert an object to a string for transmission. It's
- only argument is the object. Function must return its serializeble
- representation, e.g. string. This option cauld be used to convert
- instances of ``datetime.datetime`` into strings with required format.
+ Subclass of ``rpc4django.jsonrpcdispatcher.json.JSONEncoder`` or string
+ pointing to the subclass. It can be used to serialize objects that can't
+ otherwise be serialized. Defaults to
+ ``django.core.serializers.json.DjangoJSONEncoder``.
.. _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
@@ -40,8 +40,8 @@ class JSONRPCDispatcher:
or error.
'''
- def __init__(self, object_serializer):
- self.object_serializer = object_serializer
+ def __init__(self,json_encoder):
+ self.json_encoder = json_encoder
self.methods = {}
def register_function(self, method, external_name):
@@ -62,17 +62,16 @@ def _encode_result(self, jsonid, result, error):
res['error'] = error
res['error']['name'] = 'JSONRPCError'
res['result'] = None
-
try:
- return json.dumps(res, indent=JSON_INDENT, default=self.object_serializer)
+ return json.dumps(res, indent=JSON_INDENT, cls=self.json_encoder)
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, default=self.object_serializer)
+ return json.dumps(res, indent=JSON_INDENT, cls=self.json_encoder)
def dispatch(self, json_data, **kwargs):
'''
@@ -211,11 +211,11 @@ class RPCDispatcher:
'''
def __init__(self, url='', apps=[], restrict_introspection=False,
- restrict_ootb_auth=True, json_object_serializer=None):
+ restrict_ootb_auth=True, json_encoder=None):
version = platform.python_version_tuple()
self.url = url
self.rpcmethods = [] # a list of RPCMethod objects
- self.jsonrpcdispatcher = JSONRPCDispatcher(json_object_serializer)
+ self.jsonrpcdispatcher = JSONRPCDispatcher(json_encoder)
self.xmlrpcdispatcher = XMLRPCDispatcher()
if not restrict_introspection:
View
@@ -43,8 +43,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)
+JSON_ENCODER = getattr(settings, 'RPC4DJANGO_JSON_ENCODER',
+ 'django.core.serializers.json.DjangoJSONEncoder')
# get a list of the installed django applications
# these will be scanned for @rpcmethod decorators
@@ -236,18 +236,15 @@ def serve_rpc_request(request):
except NoReverseMatch:
URL = ''
-# resolve JSON_OBJECT_SERIALIZER to function if required
-if JSON_OBJECT_SERIALIZER == None or callable(JSON_OBJECT_SERIALIZER):
- serializer = JSON_OBJECT_SERIALIZER
+# resolve JSON_ENCODER to class if it's a string
+if isinstance(JSON_ENCODER, basestring):
+ mod_name, cls_name = get_mod_func(JSON_ENCODER)
+ json_encoder = getattr(import_module(mod_name), cls_name)
else:
- mod_name, func_name = get_mod_func(JSON_OBJECT_SERIALIZER)
- serializer = getattr(import_module(mod_name), func_name)
- if not callable(serializer):
- raise AttributeError("'%s.%s' is not a callable." %
- (mod_name, func_name))
+ json_encoder = JSON_ENCODER
# 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, serializer)
+ RESTRICT_OOTB_AUTH, json_encoder)

0 comments on commit c53e64f

Please sign in to comment.