mirror of
https://github.com/alexta69/metube.git
synced 2026-06-17 08:16:01 +00:00
Make ObjectSerializer handle all iterables including generators
Co-authored-by: alexta69 <7450369+alexta69@users.noreply.github.com>
This commit is contained in:
+9
-1
@@ -115,9 +115,17 @@ config = Config()
|
|||||||
|
|
||||||
class ObjectSerializer(json.JSONEncoder):
|
class ObjectSerializer(json.JSONEncoder):
|
||||||
def default(self, obj):
|
def default(self, obj):
|
||||||
|
# First try to use __dict__ for custom objects
|
||||||
if hasattr(obj, '__dict__'):
|
if hasattr(obj, '__dict__'):
|
||||||
return obj.__dict__
|
return obj.__dict__
|
||||||
else:
|
# Convert iterables (generators, dict_items, etc.) to lists
|
||||||
|
# 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)
|
return json.JSONEncoder.default(self, obj)
|
||||||
|
|
||||||
serializer = ObjectSerializer()
|
serializer = ObjectSerializer()
|
||||||
|
|||||||
Reference in New Issue
Block a user