diff options
| author | Josh Wolfe <thejoshwolfe@gmail.com> | 2024-08-22 08:26:14 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-22 08:26:14 -0400 |
| commit | febfcbd49d0a4713dee9db38816c163fa1926ed4 (patch) | |
| tree | 9a926671f830cfe6aeb7d3e94f5c9bc8066d56a2 /lib/std/json/stringify_test.zig | |
| parent | 31220b50b5e718b862a8ce8e993619ecd2959101 (diff) | |
| download | zig-febfcbd49d0a4713dee9db38816c163fa1926ed4.tar.gz zig-febfcbd49d0a4713dee9db38816c163fa1926ed4.zip | |
std.json.WriteStream supports streaming long values directly to the underlying stream (#21155)
Diffstat (limited to 'lib/std/json/stringify_test.zig')
| -rw-r--r-- | lib/std/json/stringify_test.zig | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/std/json/stringify_test.zig b/lib/std/json/stringify_test.zig index 1722868d2a..c0003b87dc 100644 --- a/lib/std/json/stringify_test.zig +++ b/lib/std/json/stringify_test.zig @@ -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); +} |
