【问题标题】:solidity TypeError: Object of type set is not JSON serializablesolidity TypeError:类型集的对象不是 JSON 可序列化的
【发布时间】: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 environmentSource。这也给出了一点提示为什么有些东西是不可序列化的。在这种情况下,set 类型足够独特,以至于 JSON 并不熟悉它。更复杂的例子可以是开放的网络连接;如何在另一个环境中可靠地恢复它?也许收件人不再可用,并且 open 无法存储
  • 你做过研究吗?看看How to JSON serialize sets?
  • 谢谢,之前不知道python中的“set”这个概念,好像是数据类型不一样,让我处理一下,谢谢你的帮助!

标签: python solidity smartcontracts nsjsonserialization


【解决方案1】:

而不是这个:

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

你应该使用这个:

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

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

【讨论】:

  • 是的,就是这个问题。修复后,代码运行正确,谢谢!
  • 欢迎来到 Stack Overflow!如果我的回答帮助您考虑将其标记为正确。
  • 对不起菜鸟的错误,我只找到了投票向上箭头,没有找到如何标记它,而且帮助指导员告诉我,我可以接受这个答案作为提出的人问题,也没有找到按钮,请您给点提示。
  • 完全没问题 :) 见this
  • 知道了,有趣的东西!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-11
  • 2019-11-21
  • 2021-11-13
  • 2018-09-22
  • 2019-01-17
  • 2019-12-07
  • 2023-01-20
相关资源
最近更新 更多