Skip to content

Commit

Permalink
Allow for blank lines and comments to be ignored in multiline json.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdgriffith committed Jul 19, 2017
1 parent b0abb88 commit e0f7c5d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
3 changes: 2 additions & 1 deletion box.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def _from_json(json_string=None, filename=None,
if filename:
with open(filename, 'r', encoding=encoding, errors=errors) as f:
if multiline:
data = [json.loads(line, **kwargs) for line in f]
data = [json.loads(line.strip(), **kwargs) for line in f
if line.strip() and not line.strip().startswith("#")]
else:
data = json.load(f, **kwargs)
elif json_string:
Expand Down
7 changes: 4 additions & 3 deletions test/test_functional_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ def test_to_multiline(self):
assert count == 3

def test_from_multiline(self):
content = '{"a": 2}\n{"b": 3}'
content = '{"a": 2}\n{"b": 3}\r\n \n'
with open(tmp_json_file, 'w') as f:
f.write(content)

Expand Down Expand Up @@ -532,9 +532,10 @@ def test_custom_key_errors(self):
my_box['g']

def test_pickle(self):
pic_file = os.path.join(tmp_dir, 'test.p')
bb = Box(movie_data, conversion_box=False)
pickle.dump(bb, open('/tmp/test.p', 'wb'))
loaded = pickle.load(open('/tmp/test.p', 'rb'))
pickle.dump(bb, open(pic_file, 'wb'))
loaded = pickle.load(open(pic_file, 'rb'))
assert bb == loaded
assert loaded._box_config['conversion_box'] is False

Expand Down

0 comments on commit e0f7c5d

Please sign in to comment.