Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Arrows for_json for JSON output #330

Merged
merged 6 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion watson/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
sorted_groupby,
style,
parse_tags,
json_arrow_encoder,
)


Expand Down Expand Up @@ -605,7 +606,8 @@ def report(watson, current, from_, to, projects, tags, ignore_projects,
luna=luna, all=all)

if 'json' in output_format and not aggregated:
click.echo(json.dumps(report, indent=4, sort_keys=True))
click.echo(json.dumps(report, indent=4, sort_keys=True,
default=json_arrow_encoder))
return
elif 'csv' in output_format and not aggregated:
click.echo(build_csv(flatten_report_for_csv(report)))
Expand Down
18 changes: 18 additions & 0 deletions watson/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import shutil
import sys
import tempfile

try:
from StringIO import StringIO
except ImportError:
Expand All @@ -22,6 +23,8 @@

PY2 = sys.version_info[0] == 2

if not PY2:
from builtins import TypeError, isinstance

try:
text_type = (str, unicode)
Expand Down Expand Up @@ -397,3 +400,18 @@ def flatten_report_for_csv(report):
'time': tag['time']
})
return result


def json_arrow_encoder(obj):
"""
Encodes Arrow objects for JSON output.
This function can be used with
`json.dumps(..., default=json_arrow_encoder)`, for example.
If the object is not an Arrow type, a TypeError is raised
:param obj: Object to encode
:return: JSON representation of Arrow object as defined by Arrow
"""
if isinstance(obj, arrow.Arrow):
return obj.for_json()

raise TypeError("Object {} is not JSON serializable".format(obj))