X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/dfcb24061eb6d3d415cd86d2f4edd95c4debaf36..e1fc1566f3759700fa82d4875057192d0853b2f1:/nominatim/api/v1/format.py diff --git a/nominatim/api/v1/format.py b/nominatim/api/v1/format.py index cb2b15a7..116e2ae6 100644 --- a/nominatim/api/v1/format.py +++ b/nominatim/api/v1/format.py @@ -7,12 +7,9 @@ """ Output formatters for API version v1. """ -from typing import Dict, Any -from collections import OrderedDict -import json - from nominatim.api.result_formatting import FormatDispatcher from nominatim.api import StatusResult +from nominatim.utils.json_writer import JsonWriter dispatch = FormatDispatcher() @@ -26,13 +23,15 @@ def _format_status_text(result: StatusResult) -> str: @dispatch.format_func(StatusResult, 'json') def _format_status_json(result: StatusResult) -> str: - out: Dict[str, Any] = OrderedDict() - out['status'] = result.status - out['message'] = result.message - if result.data_updated is not None: - out['data_updated'] = result.data_updated.isoformat() - out['software_version'] = str(result.software_version) - if result.database_version is not None: - out['database_version'] = str(result.database_version) + out = JsonWriter() + + out.start_object()\ + .keyval('status', result.status)\ + .keyval('message', result.message)\ + .keyval_not_none('data_updated', result.data_updated, + lambda v: v.isoformat())\ + .keyval('software_version', str(result.software_version))\ + .keyval_not_none('database_version', result.database_version, str)\ + .end_object() - return json.dumps(out) + return out()