|
1 | 1 | from base64 import decode |
2 | 2 | import copy |
3 | 3 | from ctypes import ArgumentError |
| 4 | +from datetime import datetime |
4 | 5 | from json import encoder |
5 | 6 | from pathlib import Path |
6 | 7 | import uuid |
|
10 | 11 | from json import JSONEncoder, JSONDecoder |
11 | 12 | from .logging import CONSOLE_LOGGER |
12 | 13 | from . import BHOM_VERSION |
| 14 | +from .util import bson_unix_ticks, bson_unix_ticks_to_datetime |
13 | 15 | import pandas as pd |
14 | 16 | import numpy as np |
15 | 17 |
|
@@ -45,6 +47,10 @@ def __init__(self, *args, **kwargs): |
45 | 47 | json.JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs) |
46 | 48 |
|
47 | 49 | 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 | + |
48 | 54 | if "_t" not in d: |
49 | 55 | 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}") |
50 | 56 | return self.deserialise_unknown(d) |
@@ -140,7 +146,9 @@ def default(self, o): |
140 | 146 | elif isinstance(o, np.ndarray): |
141 | 147 | return o.tolist() |
142 | 148 | 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)} |
144 | 152 | elif isinstance(o, pd.Series): |
145 | 153 | return dict(zip(o.index.astype(str), o)) |
146 | 154 | elif isinstance(o, Path): |
|
0 commit comments