1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Streaming JSON encoder.
10 from typing import Any, TypeVar, Optional, Callable
14 except ModuleNotFoundError:
15 import json # type: ignore[no-redef]
21 """ JSON encoder that renders the output directly into an output
22 stream. This is a very simple writer which produces JSON in a
23 compact as possible form.
25 The writer does not check for syntactic correctness. It is the
26 responsibility of the caller to call the write functions in an
27 order that produces correct JSON.
29 All functions return the writer object itself so that function
33 def __init__(self) -> None:
34 self.data = io.StringIO()
37 def __call__(self) -> str:
38 """ Return the rendered JSON content as a string.
39 The writer remains usable after calling this function.
42 assert self.pending in (']', '}')
43 self.data.write(self.pending)
45 return self.data.getvalue()
47 def start_object(self) -> 'JsonWriter':
48 """ Write the open bracket of a JSON object.
51 self.data.write(self.pending)
55 def end_object(self) -> 'JsonWriter':
56 """ Write the closing bracket of a JSON object.
58 assert self.pending in (',', '{', '')
59 if self.pending == '{':
60 self.data.write(self.pending)
64 def start_array(self) -> 'JsonWriter':
65 """ Write the opening bracket of a JSON array.
68 self.data.write(self.pending)
72 def end_array(self) -> 'JsonWriter':
73 """ Write the closing bracket of a JSON array.
75 assert self.pending in (',', '[', ']', ')', '')
76 if self.pending not in (',', ''):
77 self.data.write(self.pending)
81 def key(self, name: str) -> 'JsonWriter':
82 """ Write the key string of a JSON object.
85 self.data.write(self.pending)
86 self.data.write(json.dumps(name, ensure_ascii=False))
90 def value(self, value: Any) -> 'JsonWriter':
91 """ Write out a value as JSON. The function uses the json.dumps()
92 function for encoding the JSON. Thus any value that can be
93 encoded by that function is permissible here.
95 return self.raw(json.dumps(value, ensure_ascii=False))
97 def float(self, value: float, precision: int) -> 'JsonWriter':
98 """ Write out a float value with the given precision.
100 return self.raw(f"{value:0.{precision}f}")
102 def next(self) -> 'JsonWriter':
103 """ Write out a delimiter comma between JSON object or array elements.
106 self.data.write(self.pending)
110 def raw(self, raw_json: str) -> 'JsonWriter':
111 """ Write out the given value as is. This function is useful if
112 a value is already available in JSON format.
115 self.data.write(self.pending)
117 self.data.write(raw_json)
120 def keyval(self, key: str, value: Any) -> 'JsonWriter':
121 """ Write out an object element with the given key and value.
122 This is a shortcut for calling 'key()', 'value()' and 'next()'.
128 def keyval_not_none(self, key: str, value: Optional[T],
129 transform: Optional[Callable[[T], Any]] = None) -> 'JsonWriter':
130 """ Write out an object element only if the value is not None.
131 If 'transform' is given, it must be a function that takes the
132 value type and returns a JSON encodable type. The transform
133 function will be called before the value is written out.
135 if value is not None:
137 self.value(transform(value) if transform else value)