Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Code contributions:
- Jesper Schlegel (jesperschlegel)
- J vanBemmel (jbemmel)
- m-janicki
- shguild


Suggestions and bug reporting:
Expand Down
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

Unreleased
----------

* Honor ``multiline=True`` for BoxList JSON string input and output, matching file behavior.

Version 7.4.1
-------------

Expand Down
14 changes: 9 additions & 5 deletions box/box_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,18 @@ def to_json(
:param filename: If provided will save to file
:param encoding: File encoding
:param errors: How to handle encoding errors
:param multiline: Put each item in list onto it's own line
:param multiline: Put each item in list onto its own line, in a string or file
:param json_kwargs: additional arguments to pass to json.dump(s)
:return: string of JSON or return of `json.dump`
"""
if filename and multiline:
if multiline:
lines = [_to_json(item, filename=None, encoding=encoding, errors=errors, **json_kwargs) for item in self]
with open(filename, "w", encoding=encoding, errors=errors) as f:
f.write("\n".join(lines))
text = "\n".join(lines)
if filename:
with open(filename, "w", encoding=encoding, errors=errors) as f:
f.write(text)
else:
return text
else:
return _to_json(self.to_list(), filename=filename, encoding=encoding, errors=errors, **json_kwargs)

Expand All @@ -239,7 +243,7 @@ def from_json(
:param filename: filename to open and pass to `json.load`
:param encoding: File encoding
:param errors: How to handle encoding errors
:param multiline: One object per line
:param multiline: One object per line, in a string or file
:param kwargs: parameters to pass to `Box()` or `json.loads`
:return: BoxList object from json data
"""
Expand Down
11 changes: 9 additions & 2 deletions box/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,15 @@ def _from_json(
]
else:
data = json.load(f, **kwargs)
elif json_string:
data = json.loads(json_string, **kwargs)
elif json_string or (multiline and json_string is not None):
if multiline:
data = [
json.loads(line.strip(), **kwargs)
for line in StringIO(json_string, newline=None)
if line.strip() and not line.strip().startswith("#")
]
else:
data = json.loads(json_string, **kwargs)
else:
raise BoxError("from_json requires a string or filename")
return data
Expand Down
63 changes: 63 additions & 0 deletions test/test_json_lines_strings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json

import pytest

from box import BoxList


@pytest.mark.parametrize("data", [[], [{"a": 1}], [{"a": 1}, {"b": 2}], [None, False, 3, "café\u2028text"]])
def test_multiline_string_output_matches_file(tmp_path, data):
values = BoxList(data)
path = tmp_path / "values.jsonl"
values.to_json(filename=path, multiline=True)

result = values.to_json(multiline=True)

assert result == path.read_text(encoding="utf-8")
assert result == "\n".join(json.dumps(value, ensure_ascii=False) for value in data)


@pytest.mark.parametrize("newline", ["\n", "\r\n", "\r"])
def test_multiline_string_input_matches_file(tmp_path, newline):
text = newline.join(["# comment", "", '{"FirstKey": 1}', " ", ' {"SecondKey": "café\u2028text"} ', "# end"])
path = tmp_path / "values.jsonl"
path.write_bytes(text.encode("utf-8"))

result = BoxList.from_json(text, multiline=True, camel_killer_box=True)
from_file = BoxList.from_json(filename=path, multiline=True, camel_killer_box=True)

assert result == from_file
assert result[0].first_key == 1
assert result[1].second_key == "café\u2028text"


@pytest.mark.parametrize("text", ["", "\n \r\n", "# comment\n # another\n"])
def test_empty_multiline_string_input(text):
assert BoxList.from_json(text, multiline=True) == []


def test_multiline_string_json_options():
values = BoxList([{"b": 2, "a": 1}, {"c": 3}])
text = values.to_json(multiline=True, sort_keys=True, separators=(",", ":"))

assert text == '{"a":1,"b":2}\n{"c":3}'
assert BoxList.from_json(text, multiline=True, parse_int=str) == [{"a": "1", "b": "2"}, {"c": "3"}]


@pytest.mark.parametrize("text", ['{"a": 1}\nnot json', '{"a": 1}\n{"b":'])
def test_multiline_string_rejects_invalid_json(text):
with pytest.raises(json.JSONDecodeError):
BoxList.from_json(text, multiline=True)


def test_multiple_objects_require_multiline():
with pytest.raises(json.JSONDecodeError):
BoxList.from_json('{"a": 1}\n{"b": 2}')


def test_default_json_array_behavior():
values = BoxList([{"a": 1}, {"b": 2}])
assert json.loads(values.to_json()) == values.to_list()
assert BoxList.from_json(values.to_json()) == values