I have switched to orjson since it is faster, but this has caused an issue I have had for a fair amount of time now but never thought anything of it. I finally decided to do tests and these were my tests.
import orjson, json
data = json.dumps({"channel_id" : None, "payment_source_id" : None})
print(data)
data = orjson.dumps({"channel_id" : None, "payment_source_id" : None}).decode("utf-8")
print(data)
{"channel_id": null, "payment_source_id": null}
{"channel_id":null,"payment_source_id":null}
This is my testing file. When you run this you see that the only difference is the space between null and the quotes. When I try to dump json data using orjson and send it in a request , I get a 400 bad request and sometimes nothing back at all, but when trying with the json lib everything works fine, I get a valid response back. I’m not sure what to do because like I said the only difference is the spaces. Has anyone had a similar issue and can tell me what’s happening or what Im doing wrong? Also another thing to note is that if there is no “None” in my code orjson works fine.
orjson saves a few bytes (whitespaces after separators) by emitting : instead of : and , instead of , as the native json module does by default.
The native json module has an option to change this behavior with the separators argument, while orjson does not.
In my opinion, unless you are testing the correctness of what any json modules produce, and should already exist in a test suite of the module you use written by their authors, I would probably not test by asserting the byte/string outputs. Instead - as previous comments hint - assert all/parts of the python object contents after converting it from json. Any json module in your test will fail anyways if it can't deserialize/loads.
(Note: orjson produces bytes while json produces strings.)
>>> import orjson
>>> import json
>>> orjson.dumps({"hello":"world"})
b'{"hello":"world"}'
>>> json.dumps({"hello":"world"})
'{"hello": "world"}'
>>>
>>> type(orjson.dumps({"hello":"world"}))
<class 'bytes'>
>>> type(json.dumps({"hello":"world"}))
<class 'str'>
>>> json.dumps({"hello":"world"}, separators=(',', ':'))
'{"hello":"world"}'
>>> orjson.dumps({"hello":"world"}, separators=(',', ':'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: dumps() got an unexpected keyword argument
I think there isn't any difference. please check this:
import orjson, json
data1 = json.dumps({"channel_id" : None, "payment_source_id" : None})
data2 = orjson.dumps({"channel_id" : None, "payment_source_id" : None}).decode("utf-8")
print(json.loads(data2))
print (orjson.loads(data1))
{'channel_id': None, 'payment_source_id': None}
{'channel_id': None, 'payment_source_id': None}
The space difference is for just string. If you load to json, the result will be same. You can get success if you convert string to json while your api calling.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With