99from dataclasses import dataclass
1010from collections .abc import Generator
1111
12+ from pyfuse .core .task import _TaskEncoder , _resolve
1213from pyfuse .core .errors import RemoteError , TaskStalled , TaskCancelled , ThrottleError
1314from pyfuse .core .progress import ProgressInfo
1415from pyfuse .worker .backends .base import Backend
@@ -58,7 +59,12 @@ def failure(cls, task_id: str, exc: BaseException) -> Self:
5859 )
5960
6061 def to_json (self ) -> str :
61- """Serialize to JSON string."""
62+ """Serialize to JSON string.
63+
64+ Result payloads are encoded with the same sentinel-based encoder
65+ used for task arguments, so ``bytes``, ``datetime``, ``Decimal``,
66+ ``Path`` etc. round-trip transparently.
67+ """
6268 d : dict [str , Any ] = {
6369 "task_id" : self .task_id ,
6470 "status" : self .status ,
@@ -69,16 +75,17 @@ def to_json(self) -> str:
6975 d ["error_type" ] = self .error_type
7076 d ["error_message" ] = self .error_message
7177 d ["error_traceback" ] = self .error_traceback
72- return json .dumps (d )
78+ return json .dumps (d , cls = _TaskEncoder )
7379
7480 @classmethod
7581 def from_json (cls , raw : str | bytes ) -> Self :
7682 """Deserialize from a JSON string or bytes."""
7783 data = json .loads (raw )
84+ result = _resolve (data .get ("result" ), {}) if data .get ("status" ) == "ok" else None
7885 return cls (
7986 task_id = data ["task_id" ],
8087 status = data ["status" ],
81- result = data . get ( " result" ) ,
88+ result = result ,
8289 error_type = data .get ("error_type" ),
8390 error_message = data .get ("error_message" ),
8491 error_traceback = data .get ("error_traceback" ),
0 commit comments