Merge pull request #789 from alexta69/copilot/fix-32a91bc7-90af-4cfd-a3e1-d04a36659489
Fix AttributeError when serializing objects without __dict__ attribute
This commit is contained in:
commit
c4d7dd9948
14
app/main.py
14
app/main.py
|
|
@ -115,10 +115,18 @@ config = Config()
|
||||||
|
|
||||||
class ObjectSerializer(json.JSONEncoder):
|
class ObjectSerializer(json.JSONEncoder):
|
||||||
def default(self, obj):
|
def default(self, obj):
|
||||||
if isinstance(obj, object):
|
# First try to use __dict__ for custom objects
|
||||||
|
if hasattr(obj, '__dict__'):
|
||||||
return obj.__dict__
|
return obj.__dict__
|
||||||
else:
|
# Convert iterables (generators, dict_items, etc.) to lists
|
||||||
return json.JSONEncoder.default(self, obj)
|
# Exclude strings and bytes which are also iterable
|
||||||
|
elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes)):
|
||||||
|
try:
|
||||||
|
return list(obj)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
# Fall back to default behavior
|
||||||
|
return json.JSONEncoder.default(self, obj)
|
||||||
|
|
||||||
serializer = ObjectSerializer()
|
serializer = ObjectSerializer()
|
||||||
app = web.Application()
|
app = web.Application()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue