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) {...@@ -65,7 +65,7 @@ pub const Value = union(enum) {
65 .bool => |inner| try jws.write(inner),65 .bool => |inner| try jws.write(inner),
66 .integer => |inner| try jws.write(inner),66 .integer => |inner| try jws.write(inner),
67 .float => |inner| try jws.write(inner),67 .float => |inner| try jws.write(inner),
68 .number_string => |inner| try jws.writePreformatted(inner),68 .number_string => |inner| try jws.print("{s}", .{inner}),
69 .string => |inner| try jws.write(inner),69 .string => |inner| try jws.write(inner),
70 .array => |inner| try jws.write(inner.items),70 .array => |inner| try jws.write(inner.items),
71 .object => |inner| {71 .object => |inner| {
lib/std/json/stringify.zig+7-5
...@@ -152,7 +152,7 @@ pub fn writeStreamArbitraryDepth(...@@ -152,7 +152,7 @@ pub fn writeStreamArbitraryDepth(
152/// | <object>152/// | <object>
153/// | <array>153/// | <array>
154/// | write154/// | write
155/// | writePreformatted155/// | print
156/// <object> = beginObject ( objectField <value> )* endObject156/// <object> = beginObject ( objectField <value> )* endObject
157/// <array> = beginArray ( <value> )* endArray157/// <array> = beginArray ( <value> )* endArray
158/// ```158/// ```
...@@ -378,13 +378,14 @@ pub fn WriteStream(...@@ -378,13 +378,14 @@ pub fn WriteStream(
378 return self.indent_level == 0 and self.next_punctuation == .comma;378 return self.indent_level == 0 and self.next_punctuation == .comma;
379 }379 }
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`.
382 /// This function does the usual punctuation and indentation formatting382 /// 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;
384 /// e.g. `"1"`, `"[]"`, `"[1,2]"`, not `"1,2"`.384 /// 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 {
386 try self.valueStart();387 try self.valueStart();
387 try self.stream.writeAll(value_slice);388 try self.stream.print(fmt, args);
388 self.valueDone();389 self.valueDone();
389 }390 }
390391
...@@ -584,6 +585,7 @@ pub fn WriteStream(...@@ -584,6 +585,7 @@ pub fn WriteStream(
584 pub const emitNumber = @compileError("Deprecated; Use .write() instead.");585 pub const emitNumber = @compileError("Deprecated; Use .write() instead.");
585 pub const emitString = @compileError("Deprecated; Use .write() instead.");586 pub const emitString = @compileError("Deprecated; Use .write() instead.");
586 pub const emitJson = @compileError("Deprecated; Use .write() instead.");587 pub const emitJson = @compileError("Deprecated; Use .write() instead.");
588 pub const writePreformatted = @compileError("Deprecated; Use .print(\"{s}\", .{s}) instead.");
587 };589 };
588}590}
589591
lib/std/json/stringify_test.zig+5-5
...@@ -402,7 +402,7 @@ test "comptime stringify" {...@@ -402,7 +402,7 @@ test "comptime stringify" {
402 }, .{}, 8) catch unreachable;402 }, .{}, 8) catch unreachable;
403}403}
404404
405test "writePreformatted" {405test "print" {
406 var out_buf: [1024]u8 = undefined;406 var out_buf: [1024]u8 = undefined;
407 var slice_stream = std.io.fixedBufferStream(&out_buf);407 var slice_stream = std.io.fixedBufferStream(&out_buf);
408 const out = slice_stream.writer();408 const out = slice_stream.writer();
...@@ -412,11 +412,11 @@ test "writePreformatted" {...@@ -412,11 +412,11 @@ test "writePreformatted" {
412412
413 try w.beginObject();413 try w.beginObject();
414 try w.objectField("a");414 try w.objectField("a");
415 try w.writePreformatted("[ ]");415 try w.print("[ ]", .{});
416 try w.objectField("b");416 try w.objectField("b");
417 try w.beginArray();417 try w.beginArray();
418 try w.writePreformatted("[[]] ");418 try w.print("[{s}] ", .{"[]"});
419 try w.writePreformatted(" {}");419 try w.print(" {}", .{12345});
420 try w.endArray();420 try w.endArray();
421 try w.endObject();421 try w.endObject();
422422
...@@ -426,7 +426,7 @@ test "writePreformatted" {...@@ -426,7 +426,7 @@ test "writePreformatted" {
426 \\ "a": [ ],426 \\ "a": [ ],
427 \\ "b": [427 \\ "b": [
428 \\ [[]] ,428 \\ [[]] ,
429 \\ {}429 \\ 12345
430 \\ ]430 \\ ]
431 \\}431 \\}
432 ;432 ;