Skip to content

Commit 753e6b2

Browse files
committed
RF: _canonical_dump -> json_dumps and remove python <3.4 workaround
we no longer need workaround since minimal supported python is 3.5. I followed with json_dumps name to parallel json_dumps_pretty although we have save_json and not json_save
1 parent a5e6718 commit 753e6b2

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

heudiconv/utils.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,6 @@ def write_config(outfile, info):
147147
fp.writelines(PrettyPrinter().pformat(info))
148148

149149

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-
165150
def load_json(filename):
166151
"""Load data from a json file
167152
@@ -220,19 +205,25 @@ def save_json(filename, data, indent=2, sort_keys=True, pretty=False):
220205
% (str(exc), filename)
221206
)
222207
if not pretty:
223-
j = _canonical_dumps(data, **dumps_kw)
208+
j = json_dumps(data, **dumps_kw)
224209
assert j is not None # one way or another it should have been set to a str
225210
with open(filename, 'w') as fp:
226211
fp.write(j)
227212

228213

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+
229220
def json_dumps_pretty(j, indent=2, sort_keys=True):
230221
"""Given a json structure, pretty print it by colliding numeric arrays
231222
into a line.
232223
233224
If resultant structure differs from original -- throws exception
234225
"""
235-
js = _canonical_dumps(j, indent=indent, sort_keys=sort_keys)
226+
js = json_dumps(j, indent=indent, sort_keys=sort_keys)
236227
# trim away \n and spaces between entries of numbers
237228
js_ = re.sub(
238229
'[\n ]+("?[-+.0-9e]+"?,?) *\n(?= *"?[-+.0-9e]+"?)', r' \1',

0 commit comments

Comments
 (0)