std.json.WriteStream supports streaming long values directly to the underlying stream (#21155)
This commit is contained in:
@@ -443,3 +443,53 @@ test "nonportable numbers" {
|
||||
try testStringify("9999999999999999", 9999999999999999, .{});
|
||||
try testStringify("\"9999999999999999\"", 9999999999999999, .{ .emit_nonportable_numbers_as_strings = true });
|
||||
}
|
||||
|
||||
test "stringify raw streaming" {
|
||||
var out_buf: [1024]u8 = undefined;
|
||||
var slice_stream = std.io.fixedBufferStream(&out_buf);
|
||||
const out = slice_stream.writer();
|
||||
|
||||
{
|
||||
var w = writeStream(out, .{ .whitespace = .indent_2 });
|
||||
try testRawStreaming(&w, &slice_stream);
|
||||
}
|
||||
|
||||
{
|
||||
var w = writeStreamMaxDepth(out, .{ .whitespace = .indent_2 }, 8);
|
||||
try testRawStreaming(&w, &slice_stream);
|
||||
}
|
||||
|
||||
{
|
||||
var w = writeStreamMaxDepth(out, .{ .whitespace = .indent_2 }, null);
|
||||
try testRawStreaming(&w, &slice_stream);
|
||||
}
|
||||
|
||||
{
|
||||
var w = writeStreamArbitraryDepth(testing.allocator, out, .{ .whitespace = .indent_2 });
|
||||
defer w.deinit();
|
||||
try testRawStreaming(&w, &slice_stream);
|
||||
}
|
||||
}
|
||||
|
||||
fn testRawStreaming(w: anytype, slice_stream: anytype) !void {
|
||||
slice_stream.reset();
|
||||
|
||||
try w.beginObject();
|
||||
try w.beginObjectFieldRaw();
|
||||
try w.stream.writeAll("\"long");
|
||||
try w.stream.writeAll(" key\"");
|
||||
w.endObjectFieldRaw();
|
||||
try w.beginWriteRaw();
|
||||
try w.stream.writeAll("\"long");
|
||||
try w.stream.writeAll(" value\"");
|
||||
w.endWriteRaw();
|
||||
try w.endObject();
|
||||
|
||||
const result = slice_stream.getWritten();
|
||||
const expected =
|
||||
\\{
|
||||
\\ "long key": "long value"
|
||||
\\}
|
||||
;
|
||||
try std.testing.expectEqualStrings(expected, result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user