writeJsonAsync method
Writes the JSON representation of this object directly to the sink.
Use encode to safely serialize any nested child objects.
Implementation
@override
Future<void> writeJsonAsync(
StringSink sink,
JsonEncoderCallback encode,
) async {
sink.write('"');
await for (final chunk in stream) {
// jsonEncode escapes the chunk and adds surrounding quotes (e.g., "chunk\ntext").
final escaped = jsonEncode(chunk);
if (escaped.length >= 2) {
// Strip the first and last character to extract just the escaped inner content
sink.write(escaped.substring(1, escaped.length - 1));
}
}
sink.write('"');
}