【发布时间】:2021-12-29 01:43:47
【问题描述】:
我在 VSCode 中运行代码并得到一个 TypeError:Object of type set is not JSON serializable。刚开始学写代码,真的不懂,google了一下,也不知道JSON serializable是什么意思。
from solcx import compile_standard
import json
# get the contract content
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
# compile the contract
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {
"*": {"abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"}
}
}
},
},
solc_version="0.6.0",
)
# creat json file dump the comiled code in it to make it more readable.
with open("compiled_code.json", "w") as file:
json.dump(compiled_sol, file)
print(compiled_sol)
完整的错误信息如下:
(env) (base) liwei@liweideMacBook-Pro practice % python3 deploy.py
Traceback (most recent call last):
File "deploy.py", line 10, in <module>
compiled_sol = compile_standard(
File "/Users/liwei/Desktop/demos/practice/env/lib/python3.8/site-packages/solcx/main.py", line 375, in compile_standard
stdin=json.dumps(input_data),
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type set is not JSON serializable
【问题讨论】:
-
欢迎欢迎。序列化 -
process of translating data structures or object state into a format that can be stored and reconstructed later in the same or another computer environment。 Source。这也给出了一点提示为什么有些东西是不可序列化的。在这种情况下,set类型足够独特,以至于 JSON 并不熟悉它。更复杂的例子可以是开放的网络连接;如何在另一个环境中可靠地恢复它?也许收件人不再可用,并且 open 无法存储 -
你做过研究吗?看看How to JSON serialize sets?
-
谢谢,之前不知道python中的“set”这个概念,好像是数据类型不一样,让我处理一下,谢谢你的帮助!
标签: python solidity smartcontracts nsjsonserialization