I am writing a kind of 'universal' json decoder that would convert everything it can using a default json encoder plus sets, and everything else it will convert using str()
- which is a bit stupid, but it will allow to just work silently and let users know what kind of data there was initially.
So with the small amendment the following code is taken from here:
import json
class SetEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, set):
return list(obj)
try:
return json.JSONEncoder.default(self, obj)
except TypeError:
return str(obj)
Then if I try to convert a function using this encoder it will return something like:
'<function a at 0x10ddd8f28>'
Which is, again a bit stupid but totally fine for my purposes.
The question is: what if str()
fails to convert anything provided as an input argument? What kind of error will be triggered? I looked through str
source and didn't get when exactly (if at all?) it captures the possible conversion errors. Or it is assumed to convert any kind of input provided?