authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2023-03-08 19:26:03+01:00
committergravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2023-03-08 19:26:03+01:00
logc0789b4814989f2d91d3d2145d227dcdec3e2770
tree9df748fd5e898355d9d4b06ebc1dfe8e7f1f81eb
parent7e3591bedd41bde8bcca892885376e2f4a8163a3

std.json.stringify: support [*:0]const u8


1 files changed, 16 insertions(+), 7 deletions(-)

lib/std/json.zig+16-7
......@@ -2350,10 +2350,13 @@ pub fn stringify(
23502350 return stringify(value.*, options, out_stream);
23512351 },
23522352 },
2353 // TODO: .Many when there is a sentinel (waiting for https://github.com/ziglang/zig/pull/3972)
2354 .Slice => {
2355 if (ptr_info.child == u8 and options.string == .String and std.unicode.utf8ValidateSlice(value)) {
2356 try encodeJsonString(value, options, out_stream);
2353 .Many, .Slice => {
2354 if (ptr_info.size == .Many and ptr_info.sentinel == null)
2355 @compileError("unable to stringify type '" ++ @typeName(T) ++ "' without sentinel");
2356 const slice = if (ptr_info.size == .Many) mem.span(value) else value;
2357
2358 if (ptr_info.child == u8 and options.string == .String and std.unicode.utf8ValidateSlice(slice)) {
2359 try encodeJsonString(slice, options, out_stream);
23572360 return;
23582361 }
23592362
......@@ -2362,7 +2365,7 @@ pub fn stringify(
23622365 if (child_options.whitespace) |*whitespace| {
23632366 whitespace.indent_level += 1;
23642367 }
2365 for (value, 0..) |x, i| {
2368 for (slice, 0..) |x, i| {
23662369 if (i != 0) {
23672370 try out_stream.writeByte(',');
23682371 }
......@@ -2371,7 +2374,7 @@ pub fn stringify(
23712374 }
23722375 try stringify(x, child_options, out_stream);
23732376 }
2374 if (value.len != 0) {
2377 if (slice.len != 0) {
23752378 if (options.whitespace) |whitespace| {
23762379 try whitespace.outputIndent(out_stream);
23772380 }
......@@ -2526,6 +2529,12 @@ test "stringify string" {
25262529 try teststringify("\"\\/\"", "/", StringifyOptions{ .string = .{ .String = .{ .escape_solidus = true } } });
25272530}
25282531
2532test "stringify many-item sentinel-terminated string" {
2533 try teststringify("\"hello\"", @as([*:0]const u8, "hello"), StringifyOptions{});
2534 try teststringify("\"with\\nescapes\\r\"", @as([*:0]const u8, "with\nescapes\r"), StringifyOptions{ .string = .{ .String = .{ .escape_unicode = true } } });
2535 try teststringify("\"with unicode\\u0001\"", @as([*:0]const u8, "with unicode\u{1}"), StringifyOptions{ .string = .{ .String = .{ .escape_unicode = true } } });
2536}
2537
25292538test "stringify tagged unions" {
25302539 try teststringify("42", union(enum) {
25312540 Foo: u32,
......@@ -2712,7 +2721,7 @@ test "encodesTo" {
27122721 try testing.expectEqual(true, encodesTo("withąunicode😂", "with\\u0105unicode\\ud83d\\ude02"));
27132722}
27142723
2715test "issue 14600" {
2724test "deserializing string with escape sequence into sentinel slice" {
27162725 const json = "\"\\n\"";
27172726 var token_stream = std.json.TokenStream.init(json);
27182727 const options = ParseOptions{ .allocator = std.testing.allocator };