Skip to content

Commit 8f2af35

Browse files
thejoshwolfeandrewrk
authored andcommitted
std.json: WriteStream.print instead of writePreformatted
1 parent 49053cb commit 8f2af35

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

lib/std/json/dynamic.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ test "comptime stringify" {
402402
}, .{}, 8) catch unreachable;
403403
}
404404

405-
test "writePreformatted" {
405+
test "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
;

0 commit comments

Comments
 (0)