Skip to content

Commit 5ded5c3

Browse files
committed
Add extended JSON encoder for datetime objects in message encoding.
1 parent 59e22b5 commit 5ded5c3

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/aleph/sdk/client/authenticated_http.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
import hashlib
23
import json
34
import logging
@@ -50,6 +51,16 @@
5051
magic = None # type:ignore
5152

5253

54+
def extended_json_encoder(obj: Any) -> str:
55+
if (
56+
isinstance(obj, datetime.datetime)
57+
or isinstance(obj, datetime.date)
58+
or isinstance(obj, datetime.time)
59+
):
60+
return obj.isoformat() # or any other format you prefer
61+
return pydantic_encoder(obj)
62+
63+
5364
class AuthenticatedAlephHttpClient(AlephHttpClient, AuthenticatedAlephClient):
5465
account: Account
5566

@@ -604,7 +615,7 @@ async def _prepare_aleph_message(
604615

605616
# Use the Pydantic encoder to serialize types like UUID, datetimes, etc.
606617
item_content: str = json.dumps(
607-
content, separators=(",", ":"), default=pydantic_encoder
618+
content, separators=(",", ":"), default=extended_json_encoder
608619
)
609620

610621
if allow_inlining and (len(item_content) < settings.MAX_INLINE_SIZE):

0 commit comments

Comments
 (0)