Skip to content
Draft
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
3 changes: 2 additions & 1 deletion sqlite_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import json
import os
import sys
from codecs import BOM_UTF8
from collections.abc import Callable, Generator, Iterable, Iterator
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -382,7 +383,7 @@ class Format(enum.Enum):
if not first_bytes:
buffered.close()
return (), Format.CSV
if first_bytes.startswith((b"[", b"{")):
if first_bytes.removeprefix(BOM_UTF8).lstrip().startswith((b"[", b"{")):
# JSON is read eagerly, so the detection wrapper can close now,
# including when parsing raises an error.
# TODO: Detect newline-JSON
Expand Down
4 changes: 4 additions & 0 deletions tests/test_rows_from_file.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io
import json
from codecs import BOM_UTF8
from io import BytesIO, StringIO

import pytest
Expand All @@ -13,6 +14,9 @@
(b"id,name\n1,Cleo", Format.CSV),
(b"id\tname\n1\tCleo", Format.TSV),
(b'[{"id": "1", "name": "Cleo"}]', Format.JSON),
(BOM_UTF8 + b'[{"id": "1", "name": "Cleo"}]', Format.JSON),
(BOM_UTF8 + b'\n{"id": "1", "name": "Cleo"}', Format.JSON),
(BOM_UTF8 + b"id,name\n1,Cleo", Format.CSV),
),
)
def test_rows_from_file_detect_format(input, expected_format):
Expand Down
Loading