mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
Output binary columns as base64 in JSON, closes #125
This commit is contained in:
parent
bc8409941f
commit
20e543e9a4
3 changed files with 46 additions and 1 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import base64
|
||||
import click
|
||||
from click_default_group import DefaultGroup
|
||||
import sqlite_utils
|
||||
|
|
@ -748,7 +749,7 @@ def output_rows(iterator, headers, nl, arrays, json_cols):
|
|||
data = dict(zip(headers, data))
|
||||
line = "{firstchar}{serialized}{maybecomma}{lastchar}".format(
|
||||
firstchar=("[" if first else " ") if not nl else "",
|
||||
serialized=json.dumps(data),
|
||||
serialized=json.dumps(data, default=json_binary),
|
||||
maybecomma="," if (not nl and not is_last) else "",
|
||||
lastchar="]" if (is_last and not nl) else "",
|
||||
)
|
||||
|
|
@ -766,3 +767,10 @@ def maybe_json(value):
|
|||
return json.loads(stripped)
|
||||
except ValueError:
|
||||
return value
|
||||
|
||||
|
||||
def json_binary(value):
|
||||
if isinstance(value, bytes):
|
||||
return {"$base64": True, "encoded": base64.b64encode(value).decode("latin-1")}
|
||||
else:
|
||||
raise TypeError
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue