try:
import ujson as json
except ModuleNotFoundError:
- import json # type: ignore[no-redef]
+ import json # type: ignore[no-redef]
+
+T = TypeVar('T')
-T = TypeVar('T') # pylint: disable=invalid-name
class JsonWriter:
""" JSON encoder that renders the output directly into an output
self.data = io.StringIO()
self.pending = ''
-
def __call__(self) -> str:
""" Return the rendered JSON content as a string.
The writer remains usable after calling this function.
self.pending = ''
return self.data.getvalue()
-
def start_object(self) -> 'JsonWriter':
""" Write the open bracket of a JSON object.
"""
self.pending = '{'
return self
-
def end_object(self) -> 'JsonWriter':
""" Write the closing bracket of a JSON object.
"""
self.pending = '}'
return self
-
def start_array(self) -> 'JsonWriter':
""" Write the opening bracket of a JSON array.
"""
self.pending = '['
return self
-
def end_array(self) -> 'JsonWriter':
""" Write the closing bracket of a JSON array.
"""
self.pending = ']'
return self
-
def key(self, name: str) -> 'JsonWriter':
""" Write the key string of a JSON object.
"""
self.pending = ':'
return self
-
def value(self, value: Any) -> 'JsonWriter':
""" Write out a value as JSON. The function uses the json.dumps()
function for encoding the JSON. Thus any value that can be
"""
return self.raw(json.dumps(value, ensure_ascii=False))
-
def float(self, value: float, precision: int) -> 'JsonWriter':
""" Write out a float value with the given precision.
"""
self.pending = ','
return self
-
def raw(self, raw_json: str) -> 'JsonWriter':
""" Write out the given value as is. This function is useful if
a value is already available in JSON format.
self.data.write(raw_json)
return self
-
def keyval(self, key: str, value: Any) -> 'JsonWriter':
""" Write out an object element with the given key and value.
This is a shortcut for calling 'key()', 'value()' and 'next()'.
self.value(value)
return self.next()
-
def keyval_not_none(self, key: str, value: Optional[T],
transform: Optional[Callable[[T], Any]] = None) -> 'JsonWriter':
""" Write out an object element only if the value is not None.