-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathtest_orjson_encoder.py
More file actions
31 lines (24 loc) · 1.02 KB
/
Copy pathtest_orjson_encoder.py
File metadata and controls
31 lines (24 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import datetime
from decimal import Decimal
import numpy as np
import orjson
import pandas as pd
from kaleido._kaleido_tab._tab import _orjson_default
def test_orjson_default_handles_datetime_like():
# A pandas Timestamp has no ``.tolist()`` and used to raise TypeError (#458);
# _orjson_default should fall back to an ISO string, like Plotly's encoder.
ts = pd.Timestamp("2026-06-03 12:00:00")
assert _orjson_default(ts) == ts.isoformat()
a_date = datetime.date(2026, 1, 2)
assert _orjson_default(a_date) == a_date.isoformat()
# A figure spec carrying a Timestamp now round-trips through orjson.
spec = {"x": [ts]}
dumped = orjson.dumps(
spec, default=_orjson_default, option=orjson.OPT_SERIALIZE_NUMPY
)
assert ts.isoformat().encode() in dumped
# Existing fallbacks are unaffected.
decimal_value = Decimal("1.5")
assert _orjson_default(decimal_value) == float(decimal_value)
array_values = [1, 2, 3]
assert _orjson_default(np.array(array_values)) == array_values