From e16b6cb148d9fe6599ec8ce4b2803f0aed3a1d6b Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Tue, 30 Sep 2025 14:00:09 +0200 Subject: [fix] JSON format: serialization of the result-types The ``JSONEncoder`` (``format="json"``) must perform a conversion to the built-in types for the ``msgspec.Struct``:: if isinstance(o, msgspec.Struct): return msgspec.to_builtins(o) The result types are already of type ``msgspec.Struct``, so they can be converted into built-in types. The field types (in the result type) that were not yet of type ``msgspec.Struct`` have been converted to:: searx.weather.GeoLocation@dataclass -> msgspec.Struct searx.weather.DateTime -> msgspec.Struct searx.weather.Temperature -> msgspec.Struct searx.weather.PressureUnits -> msgspec.Struct searx.weather.WindSpeed -> msgspec.Struct searx.weather.RelativeHumidity -> msgspec.Struct searx.weather.Compass -> msgspec.Struct BTW: Wherever it seemed sensible, the typing was also modernized in the modified files. Closes: https://github.com/searxng/searxng/issues/5250 Signed-off-by: Markus Heiser --- searx/webutils.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'searx/webutils.py') diff --git a/searx/webutils.py b/searx/webutils.py index e025c6b47..65fcdc10e 100644 --- a/searx/webutils.py +++ b/searx/webutils.py @@ -16,6 +16,7 @@ from typing import Iterable, List, Tuple, TYPE_CHECKING from io import StringIO from codecs import getincrementalencoder +import msgspec from flask_babel import gettext, format_date # type: ignore from searx import logger, get_setting @@ -147,6 +148,8 @@ def write_csv_response(csv: CSVWriter, rc: "ResultContainer") -> None: # pylint class JSONEncoder(json.JSONEncoder): # pylint: disable=missing-class-docstring def default(self, o): + if isinstance(o, msgspec.Struct): + return msgspec.to_builtins(o) if isinstance(o, datetime): return o.isoformat() if isinstance(o, timedelta): -- cgit v1.2.3