solidity TypeError: Object of type set is not JSON serializable

solidity TypeError: Object of type set is not JSON serializable

我 运行 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

而不是这个:

{"abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"}

你应该使用这个:

["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"]

Python 中的集 JSON 不可序列化。