diff --git a/datasette/renderer.py b/datasette/renderer.py index 3f921fe7..27a5092f 100644 --- a/datasette/renderer.py +++ b/datasette/renderer.py @@ -84,7 +84,7 @@ def json_renderer(args, data, view_name): # Handle _nl option for _shape=array nl = args.get("_nl", "") if nl and shape == "array": - body = "\n".join(json.dumps(item) for item in data) + body = "\n".join(json.dumps(item, cls=CustomJSONEncoder) for item in data) content_type = "text/plain" else: body = json.dumps(data, cls=CustomJSONEncoder) diff --git a/tests/fixtures.py b/tests/fixtures.py index 139eff83..5bd063d9 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -663,7 +663,8 @@ CREATE VIEW searchable_view_configured_by_metadata AS ) ) TABLE_PARAMETERIZED_SQL = [ - ("insert into binary_data (data) values (?);", [b"this is binary data"]) + ("insert into binary_data (data) values (?);", [b"\x15\x1c\x02\xc7\xad\x05\xfe"]), + ("insert into binary_data (data) values (?);", [b"\x15\x1c\x03\xc7\xad\x05\xfe"]), ] EXTRA_DATABASE_SQL = """ diff --git a/tests/test_api.py b/tests/test_api.py index 1f93c1a7..22fa87d4 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -105,7 +105,7 @@ def test_database_page(app_client): "name": "binary_data", "columns": ["data"], "primary_keys": [], - "count": 1, + "count": 2, "hidden": False, "fts_table": None, "foreign_keys": {"incoming": [], "outgoing": []}, @@ -1793,3 +1793,32 @@ def test_null_foreign_keys_are_not_expanded(app_client): def test_inspect_file_used_for_count(app_client_immutable_and_inspect_file): response = app_client_immutable_and_inspect_file.get("/fixtures/sortable.json") assert response.json["filtered_table_rows_count"] == 100 + + +@pytest.mark.parametrize( + "path,expected_json,expected_text", + [ + ( + "/fixtures/binary_data.json?_shape=array", + [ + {"rowid": 1, "data": {"$base64": True, "encoded": "FRwCx60F/g=="}}, + {"rowid": 2, "data": {"$base64": True, "encoded": "FRwDx60F/g=="}}, + ], + None, + ), + ( + "/fixtures/binary_data.json?_shape=array&_nl=on", + None, + ( + '{"rowid": 1, "data": {"$base64": true, "encoded": "FRwCx60F/g=="}}\n' + '{"rowid": 2, "data": {"$base64": true, "encoded": "FRwDx60F/g=="}}' + ), + ), + ], +) +def test_binary_data_in_json(app_client, path, expected_json, expected_text): + response = app_client.get(path) + if expected_json: + assert response.json == expected_json + else: + assert response.text == expected_text diff --git a/tests/test_html.py b/tests/test_html.py index 89aa4d06..1a12b3ce 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -1134,8 +1134,13 @@ def test_binary_data_display(app_client): [ '