Traceback (most recent call last):
File "C:\Python\lib\site-packages\web3\_utils\encoding.py", line 206, in _friendly_json_encode
encoded = json.dumps(obj, cls=cls)
File "C:\Python\lib\json\__init__.py", line 234, in dumps
return cls(
File "C:\Python\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Python\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Python\lib\site-packages\web3\_utils\encoding.py", line 285, in default
return json.JSONEncoder.default(self, obj)
File "C:\Python\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
if isinstance(obj, bytes):
return HexStr(HexBytes(obj).hex())
My reasoning is that, a specific toJSON function for web3py should be able to process anything that other web3py APIs return (TypeDicts or AttributeDict at least).
Can be reproduced by parsing the OrderFulfilled event from the (OpenSea: Seaport 1.1 - Etherscan contract 0x00000000006c3852cbEf3e08E8dF289169EdE581) for example.
Note: We prefer to use issues to track our work. If you think you've encountered a bug in web3py or
have a feature request, you're in the right place. If you have implementation or usage questions,
please refer to our documentation and/or join the conversation
on discord.
pip freeze
outputWhat was wrong?
While attempting to convert some filtered events of
LogReceipt
type (https://github.com/ethereum/web3.py/blob/master/web3/types.py#L164 as returned by https://web3py.readthedocs.io/en/latest/filters.html#web3.utils.filters.Filter.get_new_entries) to json usingWeb3.toJSON
, I encountered events withbytes
as type, that resulted in anTypeError
exception being thrown:I would consider extending the Web3.toJSON used
JsonEncoder
(https://github.com/ethereum/web3.py/blob/master/web3/_utils/encoding.py#L286) to also supportbytes
.How can it be fixed?
Resolving this can be very easy just by reusing the available types and adding this check in https://github.com/ethereum/web3.py/blob/master/web3/_utils/encoding.py#L290 :
My reasoning is that, a specific
toJSON
function for web3py should be able to process anything that other web3py APIs return (TypeDicts or AttributeDict at least).Can be reproduced by parsing the
OrderFulfilled
event from the (OpenSea: Seaport 1.1 - Etherscan contract 0x00000000006c3852cbEf3e08E8dF289169EdE581) for example.Note: We prefer to use issues to track our work. If you think you've encountered a bug in web3py or have a feature request, you're in the right place. If you have implementation or usage questions, please refer to our documentation and/or join the conversation on discord.