Skip to content

Commit

Permalink
Merge pull request #364 from codecov/scott/json-encode-decimal
Browse files Browse the repository at this point in the history
JSON encode Decimal objects
  • Loading branch information
scott-codecov authored May 26, 2023
2 parents 1102fac + a366a22 commit 21bc2a9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions shared/utils/ReportEncoder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dataclasses
from decimal import Decimal
from fractions import Fraction
from json import JSONEncoder
from types import GeneratorType
Expand All @@ -14,6 +15,8 @@ def default(self, obj):
return obj.astuple()
elif isinstance(obj, Fraction):
return str(obj)
elif isinstance(obj, Decimal):
return str(obj)
elif isinstance(obj, ReportTotals):
# reduce totals
return obj.to_database()
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/utils/test_report_encoder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from decimal import Decimal

import pytest

from shared.reports.types import ReportTotals, SessionTotalsArray
Expand Down Expand Up @@ -42,6 +44,7 @@
4: [0, 35, 35, 0, 0, "100", 5],
},
),
(Decimal("85.00"), "85.00"),
],
)
def test_report_encoder(obj, res):
Expand Down

0 comments on commit 21bc2a9

Please sign in to comment.