4

I ran the code in VSCode and got a TypeError: Object of type set is not JSON serializable. I just start to learn to code, really don't get it, and googled it, also didn't know what does JSON serializable means.

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)

The full error information is below:

(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
Yevgeniy Kosmak
  • 3,561
  • 2
  • 10
  • 26
mike515
  • 183
  • 1
  • 14
  • 1
    Welcome welcome. Serialization - `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](https://en.wikipedia.org/wiki/Serialization). Which also gives a little hint _why_ some things aren't serializable. In this case, the `set` type is unique enough that JSON isn't familiar with it. More complex examples can be a *open network connection*; How can you reliably restore it in another environment? Maybe the recipient is no longer available and *open* is impossible to store – edd Dec 29 '21 at 02:04
  • Have you done any research? Take a look at [How to JSON serialize sets?](https://stackoverflow.com/questions/8230315/how-to-json-serialize-sets) – martineau Dec 29 '21 at 02:17
  • thanks , didn't know about the concept"set" in python before ,seems it's different data type , let me work on that, thank you for the helpl! – mike515 Dec 29 '21 at 03:49

1 Answers1

6

Instead of this:

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

you should use this:

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

Sets in Python aren't JSON serializable.

Yevgeniy Kosmak
  • 3,561
  • 2
  • 10
  • 26
  • yes,it's this problem .After fix it , the code runs correct,thank you ! – mike515 Dec 29 '21 at 03:44
  • Welcome to Stack Overflow! If my answer helped you consider marking it as right. – Yevgeniy Kosmak Dec 29 '21 at 03:46
  • Sorry for the rookie mistake , I only find the Vote up arrow ,didn't find how to mark it right , also the help instructor told me I can accept this anwer as the the one who raises the question , also no button found ,could you please give a hint. – mike515 Dec 30 '21 at 03:26
  • It's entirely ok :) See [this](https://meta.stackexchange.com/questions/147531/how-mark-my-question-as-answered-on-stack-overflow). – Yevgeniy Kosmak Dec 30 '21 at 03:28
  • Got it,interesting stuff! – mike515 Dec 30 '21 at 03:38