Skip to content

Commit

Permalink
std.json: WriteStream.print instead of writePreformatted
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoshwolfe authored and andrewrk committed Jul 27, 2023
1 parent 49053cb commit 8f2af35
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/std/json/dynamic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
12 changes: 7 additions & 5 deletions lib/std/json/stringify.zig
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub fn writeStreamArbitraryDepth(
/// | <object>
/// | <array>
/// | write
/// | writePreformatted
/// | print
/// <object> = beginObject ( objectField <value> )* endObject
/// <array> = beginArray ( <value> )* endArray
/// ```
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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.");
};
}

Expand Down
10 changes: 5 additions & 5 deletions lib/std/json/stringify_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();

Expand All @@ -426,7 +426,7 @@ test "writePreformatted" {
\\ "a": [ ],
\\ "b": [
\\ [[]] ,
\\ {}
\\ 12345
\\ ]
\\}
;
Expand Down

0 comments on commit 8f2af35

Please sign in to comment.