mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 22:35:24 +00:00
std.json: WriteStream.print instead of writePreformatted
This commit is contained in:
parent
49053cb1b4
commit
8f2af35eaa
@ -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| {
|
||||
|
||||
@ -152,7 +152,7 @@ pub fn writeStreamArbitraryDepth(
|
||||
/// | <object>
|
||||
/// | <array>
|
||||
/// | write
|
||||
/// | writePreformatted
|
||||
/// | print
|
||||
/// <object> = beginObject ( objectField <value> )* endObject
|
||||
/// <array> = beginArray ( <value> )* 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.");
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
\\ ]
|
||||
\\}
|
||||
;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user