Output binary columns as base64 in JSON, closes #125

This commit is contained in:
Simon Willison 2020-07-26 17:48:36 -07:00
commit 20e543e9a4
3 changed files with 46 additions and 1 deletions

View file

@ -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