X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/7d911f9ffbdf63b2b2a45c3a3ee7063d006a5779..e7ccd46a61f6fd4a2be17a983f02d3a13ce460e8:/nominatim/api/v1/format.py diff --git a/nominatim/api/v1/format.py b/nominatim/api/v1/format.py index ad635e39..1e37b4c7 100644 --- a/nominatim/api/v1/format.py +++ b/nominatim/api/v1/format.py @@ -7,8 +7,9 @@ """ Output formatters for API version v1. """ -from typing import Mapping, Any +from typing import List, Dict, Mapping, Any import collections +import datetime as dt import nominatim.api as napi from nominatim.api.result_formatting import FormatDispatcher @@ -16,6 +17,10 @@ from nominatim.api.v1.classtypes import ICONS from nominatim.api.v1 import format_json, format_xml from nominatim.utils.json_writer import JsonWriter +class RawDataList(List[Dict[str, Any]]): + """ Data type for formatting raw data lists 'as is' in json. + """ + dispatch = FormatDispatcher() @dispatch.format_func(napi.StatusResult, 'text') @@ -232,3 +237,20 @@ def _format_search_jsonv2(results: napi.SearchResults, options: Mapping[str, Any]) -> str: return format_json.format_base_json(results, options, False, class_label='category') + +@dispatch.format_func(RawDataList, 'json') +def _format_raw_data_json(results: RawDataList, _: Mapping[str, Any]) -> str: + out = JsonWriter() + out.start_array() + for res in results: + out.start_object() + for k, v in res.items(): + if isinstance(v, dt.datetime): + out.keyval(k, v.isoformat(sep= ' ', timespec='seconds')) + else: + out.keyval(k, v) + out.end_object().next() + + out.end_array() + + return out()