From 8f2af35eaaf94493b4e459e14a1eda7ef00b7f64 Mon Sep 17 00:00:00 2001 From: Josh Wolfe Date: Tue, 25 Jul 2023 07:05:54 -0400 Subject: [PATCH] std.json: WriteStream.print instead of writePreformatted --- lib/std/json/dynamic.zig | 2 +- lib/std/json/stringify.zig | 12 +++++++----- lib/std/json/stringify_test.zig | 10 +++++----- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/std/json/dynamic.zig b/lib/std/json/dynamic.zig index 18f7e385bf..5e99b52d44 100644 --- a/lib/std/json/dynamic.zig +++ b/lib/std/json/dynamic.zig @@ -65,7 +65,7 @@ pub const Value = union(enum) { .bool => |inner| try jws.write(inner), .integer => |inner| try jws.write(inner), .float => |inner| try jws.write(inner), - .number_string => |inner| try jws.writePreformatted(inner), + .number_string => |inner| try jws.print("{s}", .{inner}), .string => |inner| try jws.write(inner), .array => |inner| try jws.write(inner.items), .object => |inner| { diff --git a/lib/std/json/stringify.zig b/lib/std/json/stringify.zig index 726dc571de..3e00531eaa 100644 --- a/lib/std/json/stringify.zig +++ b/lib/std/json/stringify.zig @@ -152,7 +152,7 @@ pub fn writeStreamArbitraryDepth( /// | /// | /// | write -/// | writePreformatted +/// | print /// = beginObject ( objectField )* endObject /// = beginArray ( )* endArray /// ``` @@ -378,13 +378,14 @@ pub fn WriteStream( return self.indent_level == 0 and self.next_punctuation == .comma; } - /// An alternative to calling `write` that outputs the given bytes verbatim. + /// An alternative to calling `write` that formats a value with `std.fmt`. /// This function does the usual punctuation and indentation formatting - /// assuming the given slice represents a single complete value; + /// assuming the resulting formatted string represents a single complete value; /// e.g. `"1"`, `"[]"`, `"[1,2]"`, not `"1,2"`. - pub fn writePreformatted(self: *Self, value_slice: []const u8) Error!void { + /// This function may be useful for doing your own number formatting. + pub fn print(self: *Self, comptime fmt: []const u8, args: anytype) Error!void { try self.valueStart(); - try self.stream.writeAll(value_slice); + try self.stream.print(fmt, args); self.valueDone(); } @@ -584,6 +585,7 @@ pub fn WriteStream( pub const emitNumber = @compileError("Deprecated; Use .write() instead."); pub const emitString = @compileError("Deprecated; Use .write() instead."); pub const emitJson = @compileError("Deprecated; Use .write() instead."); + pub const writePreformatted = @compileError("Deprecated; Use .print(\"{s}\", .{s}) instead."); }; } diff --git a/lib/std/json/stringify_test.zig b/lib/std/json/stringify_test.zig index a9f74e6bce..4eec97e667 100644 --- a/lib/std/json/stringify_test.zig +++ b/lib/std/json/stringify_test.zig @@ -402,7 +402,7 @@ test "comptime stringify" { }, .{}, 8) catch unreachable; } -test "writePreformatted" { +test "print" { var out_buf: [1024]u8 = undefined; var slice_stream = std.io.fixedBufferStream(&out_buf); const out = slice_stream.writer(); @@ -412,11 +412,11 @@ test "writePreformatted" { try w.beginObject(); try w.objectField("a"); - try w.writePreformatted("[ ]"); + try w.print("[ ]", .{}); try w.objectField("b"); try w.beginArray(); - try w.writePreformatted("[[]] "); - try w.writePreformatted(" {}"); + try w.print("[{s}] ", .{"[]"}); + try w.print(" {}", .{12345}); try w.endArray(); try w.endObject(); @@ -426,7 +426,7 @@ test "writePreformatted" { \\ "a": [ ], \\ "b": [ \\ [[]] , - \\ {} + \\ 12345 \\ ] \\} ;