ytptube/app/library/encoder.py
copilot-swe-agent[bot] 1d5137f74f Reduce API response data by excluding unused fields from ItemDTO serialization
- Add serialize_for_list() to ItemDTO that excludes fields never used by the UI:
  options, template_chapter, temp_dir, total_bytes, total_bytes_estimate,
  tmpfilename, archive_id
- Update Encoder to use serialize_for_list() for ItemDTO objects
- Full serialize() still available for detail endpoint (GET /api/history/{id})
- Update TypeScript types to mark excluded fields as optional
- Add tests for serialize_for_list()

Co-authored-by: jbatesy <51190172+jbatesy@users.noreply.github.com>
2026-03-09 05:59:42 +00:00

38 lines
1.1 KiB
Python

import json
from datetime import date
from pathlib import Path
from yt_dlp.networking.impersonate import ImpersonateTarget
from yt_dlp.utils import DateRange
class Encoder(json.JSONEncoder):
"""
This class is used to serialize objects to JSON.
The only difference between this and the default JSONEncoder is that this one
will call the __dict__ method of an object if it exists.
"""
def default(self, o):
from .ItemDTO import ItemDTO
if isinstance(o, DateRange):
return {"start": str(o.start).replace("-", ""), "end": str(o.end).replace("-", "")}
if isinstance(o, (Path, date, ImpersonateTarget, ValueError)):
return str(o)
if isinstance(o, ItemDTO):
return o.serialize_for_list()
if isinstance(o, object):
if hasattr(o, "serialize"):
return o.serialize()
if hasattr(o, "model_dump"):
return o.model_dump()
if hasattr(o, "__dict__"):
return o.__dict__
return json.JSONEncoder.default(self, o)