diff --git a/jsonrpc/base.py b/jsonrpc/base.py index f9d4196..ce33abb 100644 --- a/jsonrpc/base.py +++ b/jsonrpc/base.py @@ -1,3 +1,4 @@ +from inspect import isawaitable from .utils import JSONSerializable @@ -85,3 +86,9 @@ def data(self, value): @property def json(self): return self.serialize(self.data) + + @property + async def async_json(self): + if isawaitable(self.data.get('result')): + self._data['result'] = await self.data['result'] + return self.serialize(self.data) diff --git a/jsonrpc/tests/test_jsonrpc2.py b/jsonrpc/tests/test_jsonrpc2.py index 32c1639..e3c2273 100644 --- a/jsonrpc/tests/test_jsonrpc2.py +++ b/jsonrpc/tests/test_jsonrpc2.py @@ -625,7 +625,17 @@ def test_data_result(self): "result": "", "id": None, }) - + def test_async_json_result(self): + # Make a coroutine + import asyncio + async def test(): return "" + r= JSONRPC20Response(result=test()) + r=asyncio.run(r.async_json) + self.assertEqual(json.loads(r), { + "jsonrpc": "2.0", + "result": "", + "id": None, + }) def test_data_result_id_none(self): r = JSONRPC20Response(result="", _id=None) self.assertEqual(json.loads(r.json), r.data)