@@ -147,21 +147,6 @@ def write_config(outfile, info):
147
147
fp .writelines (PrettyPrinter ().pformat (info ))
148
148
149
149
150
- def _canonical_dumps (json_obj , ** kwargs ):
151
- """ Dump `json_obj` to string, allowing for Python newline bug
152
-
153
- Runs ``json.dumps(json_obj, \*\*kwargs), then removes trailing whitespaces
154
- added when doing indent in some Python versions. See
155
- https://bugs.python.org/issue16333. Bug seems to be fixed in 3.4, for now
156
- fixing manually not only for aestetics but also to guarantee the same
157
- result across versions of Python.
158
- """
159
- out = json .dumps (json_obj , ** kwargs )
160
- if 'indent' in kwargs :
161
- out = out .replace (' \n ' , '\n ' )
162
- return out
163
-
164
-
165
150
def load_json (filename ):
166
151
"""Load data from a json file
167
152
@@ -220,19 +205,25 @@ def save_json(filename, data, indent=2, sort_keys=True, pretty=False):
220
205
% (str (exc ), filename )
221
206
)
222
207
if not pretty :
223
- j = _canonical_dumps (data , ** dumps_kw )
208
+ j = json_dumps (data , ** dumps_kw )
224
209
assert j is not None # one way or another it should have been set to a str
225
210
with open (filename , 'w' ) as fp :
226
211
fp .write (j )
227
212
228
213
214
+ def json_dumps (json_obj , indent = 2 , sort_keys = True ):
215
+ """Unified (default indent and sort_keys) invocation of json.dumps
216
+ """
217
+ return json .dumps (json_obj , indent = indent , sort_keys = sort_keys )
218
+
219
+
229
220
def json_dumps_pretty (j , indent = 2 , sort_keys = True ):
230
221
"""Given a json structure, pretty print it by colliding numeric arrays
231
222
into a line.
232
223
233
224
If resultant structure differs from original -- throws exception
234
225
"""
235
- js = _canonical_dumps (j , indent = indent , sort_keys = sort_keys )
226
+ js = json_dumps (j , indent = indent , sort_keys = sort_keys )
236
227
# trim away \n and spaces between entries of numbers
237
228
js_ = re .sub (
238
229
'[\n ]+("?[-+.0-9e]+"?,?) *\n (?= *"?[-+.0-9e]+"?)' , r' \1' ,
0 commit comments