Skip to content

Commit

Permalink
Merge pull request #96 from KyeRussell/issue-93
Browse files Browse the repository at this point in the history
Fix crash when serializing some reports to JSON
  • Loading branch information
ashishbijlani authored Mar 29, 2024
2 parents 0b1d391 + da2dbb6 commit 99a6440
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions packj/util/files.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from packj.util.json_wrapper import json_loads
from packj.util.job_util import md5_digest_file
import os
import json
import git

import magic

Expand Down Expand Up @@ -117,10 +119,6 @@ def read_dict_from_file(filename):
raise Exception("Failed to read dict from file %s: %s" % (filename, str(e)))

def read_json_from_file(filepath):
try:
import json
except ImportError as e:
raise Exception("'json' module not available. Please install.")
try:
with open(filepath, "r") as f:
return json_loads(f.read())
Expand All @@ -137,14 +135,39 @@ def read_from_csv(filename, skip_header=False):
if len(row) and not row[0].startswith('#'):
yield row


class JSONEncoder(json.JSONEncoder):
"""A custom JSON encoder that supports additional types."""
def default(self, obj):
# gitpython's 'Commit' class.
if isinstance(obj, git.Commit):
return {
'hash': obj.hexsha,
'author_name': obj.author.name,
'author_email': obj.author.email,
'committer_name': obj.committer.name,
'committer_email': obj.committer.email,
'message': obj.message,
'committed_date': obj.committed_datetime.isoformat(),
}
# Python standard library sets.
#
# Sorting the resulting list in an attempt to ensure
# deterministic output.
if isinstance(obj, set):
return sorted(list(obj))
# Leave everything else up to the base class.
return super().default(obj)


def write_json_to_file(filepath, data_json, indent=0):
try:
import json
except ImportError as e:
raise Exception("'json' module not available. Please install.")
try:
with open(filepath, "w+") as f:
json.dump(data_json, f, indent=indent)
json.dump(data_json, f, indent=indent, cls=JSONEncoder)
except Exception as e:
raise Exception("Failed to dump json content to file %s: %s" % (filepath, str(e)))

Expand Down

0 comments on commit 99a6440

Please sign in to comment.