changeset 3013:83159d73b13b beta

merge ext_json with upstream
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 18 Nov 2012 20:07:45 +0100
parents 58f3bba0ddc1
children 02bbd574fa14
files rhodecode/lib/ext_json.py
diffstat 1 files changed, 30 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/lib/ext_json.py	Sun Nov 18 00:38:35 2012 +0100
+++ b/rhodecode/lib/ext_json.py	Sun Nov 18 20:07:45 2012 +0100
@@ -1,8 +1,9 @@
 import datetime
 import functools
 import decimal
+import imp
 
-__all__ = ['json', 'simplejson', 'stdjson']
+__all__ = ['json', 'simplejson', 'stdlibjson']
 
 
 def _is_aware(value):
@@ -60,7 +61,7 @@
 # Import simplejson
 try:
     # import simplejson initially
-    import simplejson
+    _sj = imp.load_module('_sj', *imp.find_module('simplejson'))
 
     def extended_encode(obj):
         try:
@@ -70,12 +71,23 @@
         raise TypeError("%r is not JSON serializable" % (obj,))
     # we handle decimals our own it makes unified behavior of json vs
     # simplejson
-    simplejson.dumps = functools.partial(simplejson.dumps,
-                                         default=extended_encode,
-                                         use_decimal=False)
-    simplejson.dump = functools.partial(simplejson.dump,
-                                        default=extended_encode,
-                                        use_decimal=False)
+    sj_version = [int(x) for x in _sj.__version__.split('.')]
+    major, minor = sj_version[0], sj_version[1]
+    if major < 2 or (major == 2 and minor < 1):
+        # simplejson < 2.1 doesnt support use_decimal
+        _sj.dumps = functools.partial(_sj.dumps,
+                                             default=extended_encode)
+        _sj.dump = functools.partial(_sj.dump,
+                                            default=extended_encode)
+    else:
+        _sj.dumps = functools.partial(_sj.dumps,
+                                             default=extended_encode,
+                                             use_decimal=False)
+        _sj.dump = functools.partial(_sj.dump,
+                                            default=extended_encode,
+                                            use_decimal=False)
+    simplejson = _sj
+
 except ImportError:
     # no simplejson set it to None
     simplejson = None
@@ -83,10 +95,10 @@
 
 try:
     # simplejson not found try out regular json module
-    import json
+    _json = imp.load_module('_json', *imp.find_module('json'))
 
     # extended JSON encoder for json
-    class ExtendedEncoder(json.JSONEncoder):
+    class ExtendedEncoder(_json.JSONEncoder):
         def default(self, obj):
             try:
                 return _obj_dump(obj)
@@ -94,18 +106,17 @@
                 pass
             raise TypeError("%r is not JSON serializable" % (obj,))
     # monkey-patch JSON encoder to use extended version
-    json.dumps = functools.partial(json.dumps, cls=ExtendedEncoder)
-    json.dump = functools.partial(json.dump, cls=ExtendedEncoder)
+    _json.dumps = functools.partial(_json.dumps, cls=ExtendedEncoder)
+    _json.dump = functools.partial(_json.dump, cls=ExtendedEncoder)
 
+    stdlibjson = _json
 except ImportError:
-    json = None
-
-stdlib = json
+    stdlibjson = None
 
 # set all available json modules
 if simplejson:
-    json = simplejson
-elif json:
-    json = json
+    json = _sj
+elif stdlibjson:
+    json = _json
 else:
-    raise ImportError('Could not find any json modules')
+    raise ImportError('Could not find any json modules')
\ No newline at end of file