Skip to content

Commit

Permalink
Make custom JSON encoder support Decimal (apache#16383)
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr authored and Jorricks committed Jun 24, 2021
1 parent 790f35d commit b44913a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion airflow/utils/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# under the License.

from datetime import date, datetime
from decimal import Decimal

import numpy as np
from flask.json import JSONEncoder
Expand All @@ -37,12 +38,19 @@ def __init__(self, *args, **kwargs):
self.default = self._default

@staticmethod
def _default(obj):
def _default(obj): # pylint: disable=too-many-return-statements
"""Convert dates and numpy objects in a json serializable format."""
if isinstance(obj, datetime):
return obj.strftime('%Y-%m-%dT%H:%M:%SZ')
elif isinstance(obj, date):
return obj.strftime('%Y-%m-%d')
elif isinstance(obj, Decimal):
_, _, exponent = obj.as_tuple()
if exponent >= 0: # No digits after the decimal point.
return int(obj)
# Technically lossy due to floating point errors, but the best we
# can do without implementing a custom encode function.
return float(obj)
elif isinstance(
obj,
(
Expand Down
8 changes: 8 additions & 0 deletions tests/utils/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
# specific language governing permissions and limitations
# under the License.

import decimal
import json
import unittest
from datetime import date, datetime

import numpy as np
import parameterized
import pytest

from airflow.utils import json as utils_json
Expand All @@ -34,6 +36,12 @@ def test_encode_datetime(self):
def test_encode_date(self):
assert json.dumps(date(2017, 5, 21), cls=utils_json.AirflowJsonEncoder) == '"2017-05-21"'

@parameterized.parameterized.expand(
[("1", "1"), ("52e4", "520000"), ("2e0", "2"), ("12e-2", "0.12"), ("12.34", "12.34")],
)
def test_encode_decimal(self, expr, expected):
assert json.dumps(decimal.Decimal(expr), cls=utils_json.AirflowJsonEncoder) == expected

def test_encode_numpy_int(self):
assert json.dumps(np.int32(5), cls=utils_json.AirflowJsonEncoder) == '5'

Expand Down

0 comments on commit b44913a

Please sign in to comment.