Skip to content

Commit

Permalink
Merge pull request #8 from fcollonval/fix/serialize-date-object
Browse files Browse the repository at this point in the history
Fix JSON Encoder
  • Loading branch information
ildipo committed Aug 1, 2022
2 parents dd275aa + 017750f commit 9a97e5f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ jobs:
shell: bash -l {0}
run: |
conda env create -n beakerx -f configuration.yml
- name: Test BeakerX Base
shell: bash -l {0}
run: |
conda activate beakerx
pytest
- name: Build BeakerX Base
shell: bash -l {0}
run: |
Expand Down
Empty file added beakerx_base/tests/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions beakerx_base/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from email import utils
import json
from datetime import date, datetime

import pytest
from pandas import Timestamp

from ..utils import ObjectEncoder, date_to_int

@pytest.mark.parametrize("obj,expected", (
(date(2021, 10, 22), '{"type": "Date", "timestamp": 1634860800000}'),
(datetime(2021, 10, 22, 5, 38, 42), '{"type": "Date", "timestamp": 1634881122000}'),
(Timestamp(datetime(2021, 10, 22, 5, 38, 42)), '{"type": "Date", "timestamp": 1634881122000}'),
))
def test_ObjectEncoder(obj, expected):
assert json.dumps(obj, cls=ObjectEncoder) == expected
32 changes: 16 additions & 16 deletions beakerx_base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import inspect
import json
import time
from datetime import datetime
from datetime import date, datetime
from enum import Enum

import numpy as np
Expand Down Expand Up @@ -54,13 +54,13 @@ def datetime_to_number(value):

def unix_time(dt):
if isinstance(dt, Timestamp):
j_object = {
'type': 'Date',
'timestamp': pandas_timestamp_to_int(dt)
}
return j_object
timestamp = pandas_timestamp_to_int(dt)
else:
return date_to_int(dt)
timestamp = date_to_int(str(dt))
return {
'type': 'Date',
'timestamp': timestamp
}


def date_time_2_millis(dt):
Expand Down Expand Up @@ -187,18 +187,18 @@ def padYs(g, gMax):

class ObjectEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return self.default(date_time_2_millis(obj))
if isinstance(obj, (date, datetime)):
return date_time_2_millis(obj)
elif isinstance(obj, Enum):
return self.default(obj.value)
return obj.value
elif isinstance(obj, Color):
return self.default(obj.hex())
return obj.hex()
elif isinstance(obj, pd.Series):
return self.default(obj.tolist())
return obj.tolist()
elif isinstance(obj, np.ndarray):
return self.default(obj.tolist())
return obj.tolist()
elif isinstance(obj, (np.int64, np.bool_)):
return self.default(obj.item())
return obj.item()
elif hasattr(obj, "__dict__"):
d = dict(
(key, value)
Expand All @@ -218,9 +218,9 @@ def default(self, obj):
and not inspect.ismethoddescriptor(value)
and not inspect.isroutine(value)
)
return self.default(d)
return obj
return d

return json.JSONEncoder.default(self, obj)

class ColorUtils:
@staticmethod
Expand Down
1 change: 1 addition & 0 deletions configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ dependencies:
- pandas
- python=3.7.5
- pytz
- pytest

0 comments on commit 9a97e5f

Please sign in to comment.