Show More
@@ -60,7 +60,7 b' def _obj_dump(obj):' | |||||
60 | # Import simplejson |
|
60 | # Import simplejson | |
61 | try: |
|
61 | try: | |
62 | # import simplejson initially |
|
62 | # import simplejson initially | |
63 |
import simplejson |
|
63 | import simplejson | |
64 |
|
64 | |||
65 | def extended_encode(obj): |
|
65 | def extended_encode(obj): | |
66 | try: |
|
66 | try: | |
@@ -70,37 +70,42 b' try:' | |||||
70 | raise TypeError("%r is not JSON serializable" % (obj,)) |
|
70 | raise TypeError("%r is not JSON serializable" % (obj,)) | |
71 | # we handle decimals our own it makes unified behavior of json vs |
|
71 | # we handle decimals our own it makes unified behavior of json vs | |
72 | # simplejson |
|
72 | # simplejson | |
73 |
|
|
73 | simplejson.dumps = functools.partial(simplejson.dumps, | |
|
74 | default=extended_encode, | |||
74 | use_decimal=False) |
|
75 | use_decimal=False) | |
75 |
|
|
76 | simplejson.dump = functools.partial(simplejson.dump, | |
|
77 | default=extended_encode, | |||
76 | use_decimal=False) |
|
78 | use_decimal=False) | |
77 | simplejson = _sj |
|
|||
78 |
|
||||
79 | except ImportError: |
|
79 | except ImportError: | |
80 | # no simplejson set it to None |
|
80 | # no simplejson set it to None | |
81 |
|
|
81 | simplejson = None | |
82 |
|
82 | |||
83 |
|
83 | |||
84 | try: |
|
84 | try: | |
85 | # simplejson not found try out regular json module |
|
85 | # simplejson not found try out regular json module | |
86 |
import json |
|
86 | import json | |
87 |
|
87 | |||
88 | # extended JSON encoder for json |
|
88 | # extended JSON encoder for json | |
89 |
class ExtendedEncoder( |
|
89 | class ExtendedEncoder(json.JSONEncoder): | |
90 | def default(self, obj): |
|
90 | def default(self, obj): | |
91 | try: |
|
91 | try: | |
92 | return _obj_dump(obj) |
|
92 | return _obj_dump(obj) | |
93 | except NotImplementedError: |
|
93 | except NotImplementedError: | |
94 | pass |
|
94 | pass | |
95 |
return |
|
95 | return json.JSONEncoder.default(self, obj) | |
96 | # monkey-patch JSON encoder to use extended version |
|
96 | # monkey-patch JSON encoder to use extended version | |
97 |
|
|
97 | json.dumps = functools.partial(json.dumps, cls=ExtendedEncoder) | |
98 |
|
|
98 | json.dump = functools.partial(json.dump, cls=ExtendedEncoder) | |
99 | stdlib = _json |
|
99 | ||
100 | except ImportError: |
|
100 | except ImportError: | |
101 |
|
|
101 | json = None | |
|
102 | ||||
|
103 | stdlib = json | |||
102 |
|
104 | |||
103 | # set all available json modules |
|
105 | # set all available json modules | |
104 |
simplejson |
|
106 | if simplejson: | |
105 |
|
|
107 | json = simplejson | |
106 | json = _sj if _sj else _json |
|
108 | elif json: | |
|
109 | json = json | |||
|
110 | else: | |||
|
111 | raise ImportError('Could not find any json modules') |
General Comments 0
You need to be logged in to leave comments.
Login now