Solidity TypeError:类型集的对象不可 JSON 序列化

mik*_*515 4 python nsjsonserialization solidity smartcontracts

我在 VSCode 中运行代码并得到 TypeError: Object of type set is not JSON serializable。我刚开始学习编码,实在不懂,google了一下,也不知道JSON可序列化是什么意思。

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)
Run Code Online (Sandbox Code Playgroud)

完整的错误信息如下:

(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
Run Code Online (Sandbox Code Playgroud)

Yev*_*mak 6

而不是这个:

{"abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"}
Run Code Online (Sandbox Code Playgroud)

你应该使用这个:

["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"]
Run Code Online (Sandbox Code Playgroud)

Python 中的集合不可进行 JSON 序列化。