bigchaindb / bigchaindb-driver

Official Python driver for BigchainDB
https://www.bigchaindb.com
Apache License 2.0
105 stars 104 forks source link

Handcrafting guide is broken while using json dumps to serialize handcrafted tx to json str #313

Closed muawiakh closed 7 years ago

muawiakh commented 7 years ago

Description

While following the handcrafting guide for a CREATE transaction, came across an issue after creating a transaction ID right after the Up to now section. I was trying to serialize the handcrafted_creation_tx to json string as per the guide and it fails by complaining that the transaction ID is not JSON serializable. Same scenario with prepared_creation_tx

What I Did

json_str_handcrafted_tx = json.dumps(handcrafted_creation_tx, sort_keys=True)
AND
json_str_prepared_tx = json.dumps(prepared_creation_tx, sort_keys=True)
Traceback
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/json/__init__.py", line 237, in dumps
    **kw).encode(obj)
  File "/usr/lib/python3.5/json/encoder.py", line 198, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.5/json/encoder.py", line 179, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b'be9cd8825d6d7cf5f1ac81883e9c6b7c43f7ada92a4321c135c5ec5a2f9c637b' is not JSON serializable
Solution
  1. One fix can be to use rapidjson for the aforementioned commands:
    import rapidjson
    json_str_handcrafted_tx = rapidjson.dumps(handcrafted_creation_tx, skipkeys=False, ensure_ascii=False, sort_keys=True)
    json_str_prepared_tx = rapidjson.dumps(prepared_creation_tx, skipkeys=False, ensure_ascii=False, sort_keys=True)
  2. Another fix can be to decode the 'id' to utf-8 before calling json.dumps
    
    handcrafted_creation_tx['id'] = handcrafted_creation_tx['id'].decode('utf-8')
    prepared_creation_tx['id'] = prepared_creation_tx['id'].decode('utf-8')

json_str_handcrafted_tx = json.dumps(handcrafted_creation_tx, sort_keys=True) json_str_prepared_tx = json.dumps(prepared_creation_tx, sort_keys=True)

handcrafted_creation_tx['id'] = handcrafted_creation_tx['id'].encode('utf-8') prepared_creation_tx['id'] = prepared_creation_tx['id'].encode('utf-8')

muawiakh commented 7 years ago

Same issue in the The Fulfilled Transaction, when I run:

    In [0]: message = json.dumps(
       ...:     handcrafted_creation_tx,
       ...:     sort_keys=True,
       ...:     separators=(',', ':'),
       ...:     ensure_ascii=False,
       ...: )
muawiakh commented 7 years ago
Section: The Fulfilled Transaction
 In [0]: json.dumps(fulfilled_creation_tx, sort_keys=True) == json.dumps(handcrafted_creation_tx, sort_keys=True)
muawiakh commented 7 years ago
Section: The fulfilled Transaction

In a nutshell, code snippet.

muawiakh commented 7 years ago
Section: The fulfilled transaction
returned_creation_tx = bdb.transactions.send(handcrafted_creation_tx)
muawiakh commented 7 years ago

I verified the guide on two different setups and it seems like I had some packaging/versioning issue with some python modules(on the setup that I was using). The guide works fine with the current set of instructions. Verified it. Closing the ticket.