##// END OF EJS Templates
merge ext_json with upstream
marcink -
r3013:83159d73 beta
parent child Browse files
Show More
@@ -1,8 +1,9 b''
1 import datetime
1 import datetime
2 import functools
2 import functools
3 import decimal
3 import decimal
4 import imp
4
5
5 __all__ = ['json', 'simplejson', 'stdjson']
6 __all__ = ['json', 'simplejson', 'stdlibjson']
6
7
7
8
8 def _is_aware(value):
9 def _is_aware(value):
@@ -60,7 +61,7 b' def _obj_dump(obj):'
60 # Import simplejson
61 # Import simplejson
61 try:
62 try:
62 # import simplejson initially
63 # import simplejson initially
63 import simplejson
64 _sj = imp.load_module('_sj', *imp.find_module('simplejson'))
64
65
65 def extended_encode(obj):
66 def extended_encode(obj):
66 try:
67 try:
@@ -70,12 +71,23 b' try:'
70 raise TypeError("%r is not JSON serializable" % (obj,))
71 raise TypeError("%r is not JSON serializable" % (obj,))
71 # we handle decimals our own it makes unified behavior of json vs
72 # we handle decimals our own it makes unified behavior of json vs
72 # simplejson
73 # simplejson
73 simplejson.dumps = functools.partial(simplejson.dumps,
74 sj_version = [int(x) for x in _sj.__version__.split('.')]
74 default=extended_encode,
75 major, minor = sj_version[0], sj_version[1]
75 use_decimal=False)
76 if major < 2 or (major == 2 and minor < 1):
76 simplejson.dump = functools.partial(simplejson.dump,
77 # simplejson < 2.1 doesnt support use_decimal
77 default=extended_encode,
78 _sj.dumps = functools.partial(_sj.dumps,
78 use_decimal=False)
79 default=extended_encode)
80 _sj.dump = functools.partial(_sj.dump,
81 default=extended_encode)
82 else:
83 _sj.dumps = functools.partial(_sj.dumps,
84 default=extended_encode,
85 use_decimal=False)
86 _sj.dump = functools.partial(_sj.dump,
87 default=extended_encode,
88 use_decimal=False)
89 simplejson = _sj
90
79 except ImportError:
91 except ImportError:
80 # no simplejson set it to None
92 # no simplejson set it to None
81 simplejson = None
93 simplejson = None
@@ -83,10 +95,10 b' except ImportError:'
83
95
84 try:
96 try:
85 # simplejson not found try out regular json module
97 # simplejson not found try out regular json module
86 import json
98 _json = imp.load_module('_json', *imp.find_module('json'))
87
99
88 # extended JSON encoder for json
100 # extended JSON encoder for json
89 class ExtendedEncoder(json.JSONEncoder):
101 class ExtendedEncoder(_json.JSONEncoder):
90 def default(self, obj):
102 def default(self, obj):
91 try:
103 try:
92 return _obj_dump(obj)
104 return _obj_dump(obj)
@@ -94,18 +106,17 b' try:'
94 pass
106 pass
95 raise TypeError("%r is not JSON serializable" % (obj,))
107 raise TypeError("%r is not JSON serializable" % (obj,))
96 # monkey-patch JSON encoder to use extended version
108 # monkey-patch JSON encoder to use extended version
97 json.dumps = functools.partial(json.dumps, cls=ExtendedEncoder)
109 _json.dumps = functools.partial(_json.dumps, cls=ExtendedEncoder)
98 json.dump = functools.partial(json.dump, cls=ExtendedEncoder)
110 _json.dump = functools.partial(_json.dump, cls=ExtendedEncoder)
99
111
112 stdlibjson = _json
100 except ImportError:
113 except ImportError:
101 json = None
114 stdlibjson = None
102
103 stdlib = json
104
115
105 # set all available json modules
116 # set all available json modules
106 if simplejson:
117 if simplejson:
107 json = simplejson
118 json = _sj
108 elif json:
119 elif stdlibjson:
109 json = json
120 json = _json
110 else:
121 else:
111 raise ImportError('Could not find any json modules')
122 raise ImportError('Could not find any json modules') No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now