Mar 18, 2024
I recently realized that it seemed that rapidjson had issues with serializing a python dict with numbers in its keys:
>>> import rapidjson as rj
>>> import json
>>> a = {1:"cat"}
>>> json.dumps(a)
'{"1": "cat"}'
>>> rj.dumps(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: {1: 'cat'} is not JSON serializable
The python std json could handle it fine. I asked on the rapidjson github page if this was expected, and it was. There is a workaround for this though:
>>> rj.dumps(a, mapping_mode=rj.MM_COERCE_KEYS_TO_STRINGS)
'{"1":"one"}'
rapidjson is my go to replacement library for json stuff in python since it’s (usually) almost totally drop-in and ready. Just have to put include rapidjson as json
and you’re good to know. Looks like there can be some edge cases such as this. Hope this helps, God bless!