Skip to content

Commit 1e3590d

Browse files
PoByBolekgetify
authored andcommitted
keep newlines and replace comments with white space (#42)
* keep newlines and replace comments with white space If strip_space is False, keep newlines and replace comments with white space so that the JSON parser reports the correct line and column numbers on parsing errors. This fixes issue #5 for the python version. * incorporate _space_template() into the main template() remove the tiny (redundant) white space tests. Check that the JSON with white space preserved can be further minimized and produces the same result when fed to the JSON parser.
1 parent 9c5b3d6 commit 1e3590d

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

json_minify/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def json_minify(string, strip_space=True):
3535
# replace white space as defined in standard
3636
tmp = re.sub('[ \t\n\r]+', '', tmp)
3737
new_str.append(tmp)
38+
elif not strip_space:
39+
# Replace comments with white space so that the JSON parser reports
40+
# the correct column numbers on parsing errors.
41+
new_str.append(' ' * (match.start() - index))
3842

3943
index = match.end()
4044
val = match.group()
@@ -53,10 +57,18 @@ def json_minify(string, strip_space=True):
5357
in_single = True
5458
elif val == '*/' and in_multi and not (in_string or in_single):
5559
in_multi = False
60+
if not strip_space:
61+
new_str.append(' ' * len(val))
5662
elif val in '\r\n' and not (in_multi or in_string) and in_single:
5763
in_single = False
5864
elif not ((in_multi or in_single) or (val in ' \r\n\t' and strip_space)): # noqa
5965
new_str.append(val)
6066

67+
if not strip_space:
68+
if val in '\r\n':
69+
new_str.append(val)
70+
elif in_multi or in_single:
71+
new_str.append(' ' * len(val))
72+
6173
new_str.append(string[index:])
6274
return ''.join(new_str)

json_minify/test_json_minify.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,32 @@ class JsonMinifyTestCase(unittest.TestCase):
3131
from such a file with the JSON.minify() project hosted
3232
here on github at http://github.com/getify/JSON.minify
3333
*/''',
34+
'''
35+
\x20
36+
{
37+
"foo": "bar", \x20
38+
"bar": [
39+
"baz", "bum"
40+
],
41+
\x20
42+
\x20
43+
"something": 10,
44+
"else": 20
45+
}
46+
47+
\x20
48+
\x20
49+
\x20
50+
''',
3451
'{"foo":"bar","bar":["baz","bum"],"something":10,"else":20}'
3552
],
3653
[
3754
'''
3855
{"/*":"*/","//":"",/*"//"*/"/*/"://
3956
"//"}''',
57+
'''
58+
{"/*":"*/","//":"", "/*/": \x20
59+
"//"}''',
4060
'{"/*":"*/","//":"","/*/":"//"}'
4161
],
4262
[
@@ -51,6 +71,19 @@ class JsonMinifyTestCase(unittest.TestCase):
5171
, "b\"az":/*
5272
something else */"blah"
5373
74+
}
75+
''',
76+
'''
77+
\x20
78+
\x20
79+
{
80+
81+
"foo"
82+
:
83+
"bar/*" \x20
84+
, "b\\\"az": \x20
85+
"blah"
86+
5487
}
5588
''',
5689
r'{"foo":"bar/*","b\"az":"blah"}'
@@ -61,16 +94,30 @@ class JsonMinifyTestCase(unittest.TestCase):
6194
"baz\\\\": /* yay */ "fo\\\\\"*/o"
6295
}
6396
''',
97+
r'''
98+
{"foo": "ba\"r//", "bar\\": "b\\\"a/*z",
99+
"baz\\\\": "fo\\\\\"*/o"
100+
}
101+
''',
64102
r'{"foo":"ba\"r//","bar\\":"b\\\"a/*z","baz\\\\":"fo\\\\\"*/o"}' # noqa
65103
]
66104
]
67105

68106
def template(self, index):
69-
in_string, expected = self.tests[index - 1]
107+
in_string, expected_whitespace, expected = self.tests[index - 1]
70108
in_dict = json.loads(json_minify(in_string))
71109
expected_dict = json.loads(textwrap.dedent(expected))
72110
self.assertEqual(in_dict, expected_dict)
73111

112+
with_whitespace = json_minify(in_string, strip_space=False)
113+
with_whitespace_dict = json.loads(with_whitespace)
114+
self.assertEqual(expected_whitespace, with_whitespace)
115+
self.assertEqual(with_whitespace_dict, expected_dict)
116+
117+
no_whitespace = json_minify(with_whitespace)
118+
no_whitespace_dict = json.loads(no_whitespace)
119+
self.assertEqual(no_whitespace_dict, expected_dict)
120+
74121
def test_1(self):
75122
self.template(1)
76123

@@ -83,5 +130,6 @@ def test_3(self):
83130
def test_4(self):
84131
self.template(4)
85132

133+
86134
if __name__ == '__main__':
87135
unittest.main()

0 commit comments

Comments
 (0)