Auto-decode columns declared as JSON in output modes (issue #579)

Columns declared with type JSON now decode automatically wherever nested
JSON is already supported (query/rows/search/memory output, and the
Python API), without needing --json-cols. Uses sqlite3's built-in
detect_types=PARSE_DECLTYPES + a registered JSON converter, so it works
transparently through joins and aliases. Flat formats (CSV/TSV/table/raw)
re-serialize decoded values back to JSON text instead of a Python repr.
Also adds "JSON" as a first-class column type for create()/add_column()/
transform(), and fixes column_affinity("JSON") so transform() doesn't
rewrite JSON columns as REAL.
This commit is contained in:
Parker Gurney 2026-08-05 14:18:25 -07:00
commit f34dfb25da
8 changed files with 99 additions and 10 deletions

View file

@ -335,6 +335,7 @@ COLUMN_TYPE_MAPPING: dict[Any, str] = {
"FLOAT": "FLOAT",
"REAL": "REAL",
"BLOB": "BLOB",
"JSON": "JSON",
"text": "TEXT",
"str": "TEXT",
"integer": "INTEGER",
@ -343,6 +344,7 @@ COLUMN_TYPE_MAPPING: dict[Any, str] = {
"real": "REAL",
"blob": "BLOB",
"bytes": "BLOB",
"json": "JSON",
}
# If numpy is available, add more types
if np:
@ -532,11 +534,14 @@ class Database:
uri,
uri=True,
check_same_thread=False,
detect_types=sqlite3.PARSE_DECLTYPES,
)
self.memory = True
self.memory_name = memory_name
elif memory or filename_or_conn == ":memory:":
self.conn = sqlite3.connect(":memory:")
self.conn = sqlite3.connect(
":memory:", detect_types=sqlite3.PARSE_DECLTYPES
)
self.memory = True
elif isinstance(filename_or_conn, (str, pathlib.Path)):
if recreate and os.path.exists(filename_or_conn):
@ -545,9 +550,13 @@ class Database:
except OSError:
# Avoid mypy and __repr__ errors, see:
# https://github.com/simonw/sqlite-utils/issues/503
self.conn = sqlite3.connect(":memory:")
self.conn = sqlite3.connect(
":memory:", detect_types=sqlite3.PARSE_DECLTYPES
)
raise
self.conn = sqlite3.connect(str(filename_or_conn))
self.conn = sqlite3.connect(
str(filename_or_conn), detect_types=sqlite3.PARSE_DECLTYPES
)
else:
if recreate:
raise ValueError("recreate cannot be used with connections, only paths")