jackjack-jj / pywallet

bitcoin wallet importer/exporter
499 stars 346 forks source link

'bytes' is not JSON serializable bug #30

Open adrastee opened 3 years ago

adrastee commented 3 years ago

Using python3 - i know its experimental,

Traceback (most recent call last): File "/home/a/pywallet/pywallet.py", line 3936, in wallet = json.dumps(json_db, sort_keys=True, indent=4) File "/usr/lib/python3.6/json/init.py", line 238, in dumps **kw).encode(obj) File "/usr/lib/python3.6/json/encoder.py", line 201, in encode chunks = list(chunks) File "/usr/lib/python3.6/json/encoder.py", line 430, in _iterencode yield from _iterencode_dict(o, _current_indent_level) File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict yield from chunks File "/usr/lib/python3.6/json/encoder.py", line 325, in _iterencode_list yield from chunks File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict yield from chunks File "/usr/lib/python3.6/json/encoder.py", line 437, in _iterencode o = _default(o) File "/usr/lib/python3.6/json/encoder.py", line 180, in default o.class.name) TypeError: Object of type 'bytes' is not JSON serializable

Any chance it can be debugged? Thanks!

alcalawil commented 3 years ago

same issue

alcalawil commented 3 years ago

I add this function to the end of the file

def dump_wallet(path_to_wallet):
    dumpbalance = False
    wallet_dir, wallet_name = os.path.split(path_to_wallet)

    db_env = create_env(wallet_dir)

    read_wallet(json_db, db_env, wallet_name, True, True, "", not(dumpbalance is None))
    wallet = json.dumps(json_db, sort_keys=True, indent=4)
    return wallet

and also modified the balance function to support python3

def balance(site, address):
    page = ""
    try:
        page=urllib.urlopen("%s%s" % (site, address))
    except:
        import urllib.request
        print('Python3 error: urllib.urlopen is not supported. Trying urllib.request.urlopen')
        page=urllib.request.urlopen("%s%s" % (site, address))
    return page.read()

and works

wnayes commented 3 years ago

I got past the 'bytes is not JSON serializable error by installing simplejson.

pip install simplejson

This works because the script first tries to load simplejson, and then falls back to the built-in json, and apparently that fallback is not python 3 compatible.