authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2023-07-25 07:05:54-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-27 10:23:58-07:00
log8f2af35eaaf94493b4e459e14a1eda7ef00b7f64
tree0b69d2f345d3ecaf7bfc6d9fc55459bafdb907f3
parent49053cb1b4af7ee2973cf606def398c7eef6578b

std.json: WriteStream.print instead of writePreformatted


3 files changed, 13 insertions(+), 11 deletions(-)

lib/std/json/dynamic.zig+1-1
......@@ -65,7 +65,7 @@ pub const Value = union(enum) {
6565 .bool => |inner| try jws.write(inner),
6666 .integer => |inner| try jws.write(inner),
6767 .float => |inner| try jws.write(inner),
68 .number_string => |inner| try jws.writePreformatted(inner),
68 .number_string => |inner| try jws.print("{s}", .{inner}),
6969 .string => |inner| try jws.write(inner),
7070 .array => |inner| try jws.write(inner.items),
7171 .object => |inner| {
lib/std/json/stringify.zig+7-5
......@@ -152,7 +152,7 @@ pub fn writeStreamArbitraryDepth(
152152/// | <object>
153153/// | <array>
154154/// | write
155/// | writePreformatted
155/// | print
156156/// <object> = beginObject ( objectField <value> )* endObject
157157/// <array> = beginArray ( <value> )* endArray
158158/// ```
......@@ -378,13 +378,14 @@ pub fn WriteStream(
378378 return self.indent_level == 0 and self.next_punctuation == .comma;
379379 }
380380
381 /// An alternative to calling `write` that outputs the given bytes verbatim.
381 /// An alternative to calling `write` that formats a value with `std.fmt`.
382382 /// This function does the usual punctuation and indentation formatting
383 /// assuming the given slice represents a single complete value;
383 /// assuming the resulting formatted string represents a single complete value;
384384 /// e.g. `"1"`, `"[]"`, `"[1,2]"`, not `"1,2"`.
385 pub fn writePreformatted(self: *Self, value_slice: []const u8) Error!void {
385 /// This function may be useful for doing your own number formatting.
386 pub fn print(self: *Self, comptime fmt: []const u8, args: anytype) Error!void {
386387 try self.valueStart();
387 try self.stream.writeAll(value_slice);
388 try self.stream.print(fmt, args);
388389 self.valueDone();
389390 }
390391
......@@ -584,6 +585,7 @@ pub fn WriteStream(
584585 pub const emitNumber = @compileError("Deprecated; Use .write() instead.");
585586 pub const emitString = @compileError("Deprecated; Use .write() instead.");
586587 pub const emitJson = @compileError("Deprecated; Use .write() instead.");
588 pub const writePreformatted = @compileError("Deprecated; Use .print(\"{s}\", .{s}) instead.");
587589 };
588590}
589591
lib/std/json/stringify_test.zig+5-5
......@@ -402,7 +402,7 @@ test "comptime stringify" {
402402 }, .{}, 8) catch unreachable;
403403}
404404
405test "writePreformatted" {
405test "print" {
406406 var out_buf: [1024]u8 = undefined;
407407 var slice_stream = std.io.fixedBufferStream(&out_buf);
408408 const out = slice_stream.writer();
......@@ -412,11 +412,11 @@ test "writePreformatted" {
412412
413413 try w.beginObject();
414414 try w.objectField("a");
415 try w.writePreformatted("[ ]");
415 try w.print("[ ]", .{});
416416 try w.objectField("b");
417417 try w.beginArray();
418 try w.writePreformatted("[[]] ");
419 try w.writePreformatted(" {}");
418 try w.print("[{s}] ", .{"[]"});
419 try w.print(" {}", .{12345});
420420 try w.endArray();
421421 try w.endObject();
422422
......@@ -426,7 +426,7 @@ test "writePreformatted" {
426426 \\ "a": [ ],
427427 \\ "b": [
428428 \\ [[]] ,
429 \\ {}
429 \\ 12345
430430 \\ ]
431431 \\}
432432 ;