Skip to content

Commit 8d276ba

Browse files
committed
fix date serialisation to/from bhom
1 parent 3429c3c commit 8d276ba

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

‎Python_Engine/Python/src/python_toolkit/bhom/bhom_object.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from base64 import decode
22
import copy
33
from ctypes import ArgumentError
4+
from datetime import datetime
45
from json import encoder
56
from pathlib import Path
67
import uuid
@@ -10,6 +11,7 @@
1011
from json import JSONEncoder, JSONDecoder
1112
from .logging import CONSOLE_LOGGER
1213
from . import BHOM_VERSION
14+
from .util import bson_unix_ticks, bson_unix_ticks_to_datetime
1315
import pandas as pd
1416
import numpy as np
1517

@@ -45,6 +47,10 @@ def __init__(self, *args, **kwargs):
4547
json.JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs)
4648

4749
def object_hook(self, d):
50+
if "$date" in d:
51+
CONSOLE_LOGGER.debug(f"deserialising timestamp {d}")
52+
return bson_unix_ticks_to_datetime(d["$date"])
53+
4854
if "_t" not in d:
4955
CONSOLE_LOGGER.debug(f"BHoMJSONDecoder could not convert the following dictionary into a BHoMObject due to a missing '_t' property. Falling back to dictionary: {d}")
5056
return self.deserialise_unknown(d)
@@ -140,7 +146,9 @@ def default(self, o):
140146
elif isinstance(o, np.ndarray):
141147
return o.tolist()
142148
elif isinstance(o, pd.Timestamp):
143-
return o.isoformat()
149+
return {"$date": bson_unix_ticks(o.to_pydatetime(), True)}
150+
elif isinstance(o, datetime):
151+
return {"$date": bson_unix_ticks(o, True)}
144152
elif isinstance(o, pd.Series):
145153
return dict(zip(o.index.astype(str), o))
146154
elif isinstance(o, Path):

‎Python_Engine/Python/src/python_toolkit/bhom/decorators/_bhom_callable_decorator.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from typing import Any, Callable, Union, Dict
33
from functools import wraps
4-
from ..bhom_object import CONSOLE_LOGGER, BHoMJSONDecoder, BHoMJSONEncoder, BHoMObject
4+
from ..bhom_object import CONSOLE_LOGGER, BHoMJSONDecoder, BHoMJSONEncoder, BHoMObject, IObject
55

66
class _BHoMWrapper:
77
_registered_methods: Dict[str, Callable] = {}

0 commit comments

Comments
 (0)