I'm looking to output a Python dictionary to a file using the json library with formatting such that lists are represented on the same line.
I have tried making a custom encoder and using the ones I have found online as well such as the suggestion here: https://stackoverflow.com/a/26512016/1411362
such that the final code line is:
import json
data = {'a':1, 'b':[1,2,3,4]}
with open("data.json", 'w') as f:
json.dump(data, f, indent=4, cls=CustomEncoderClass)
however, this fails to work if I try to use json.dump to export as a file instead of json.dumps (like in the link above) to a string. Is there a way to use the custom encoder such that it works when I export the data to a file?