| author | |
| committer | |
| log | 8f2af35eaaf94493b4e459e14a1eda7ef00b7f64 |
| tree | 0b69d2f345d3ecaf7bfc6d9fc55459bafdb907f3 |
| parent | 49053cb1b4af7ee2973cf606def398c7eef6578b |
3 files changed, 13 insertions(+), 11 deletions(-)
lib/std/json/dynamic.zig+1-1| ... | ... | @@ -65,7 +65,7 @@ pub const Value = union(enum) { |
| 65 | 65 | .bool => |inner| try jws.write(inner), |
| 66 | 66 | .integer => |inner| try jws.write(inner), |
| 67 | 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 | 69 | .string => |inner| try jws.write(inner), |
| 70 | 70 | .array => |inner| try jws.write(inner.items), |
| 71 | 71 | .object => |inner| { |
lib/std/json/stringify.zig+7-5| ... | ... | @@ -152,7 +152,7 @@ pub fn writeStreamArbitraryDepth( |
| 152 | 152 | /// | <object> |
| 153 | 153 | /// | <array> |
| 154 | 154 | /// | write |
| 155 | /// | writePreformatted | |
| 155 | ||
| 156 | 156 | /// <object> = beginObject ( objectField <value> )* endObject |
| 157 | 157 | /// <array> = beginArray ( <value> )* endArray |
| 158 | 158 | /// ``` |
| ... | ... | @@ -378,13 +378,14 @@ pub fn WriteStream( |
| 378 | 378 | return self.indent_level == 0 and self.next_punctuation == .comma; |
| 379 | 379 | } |
| 380 | 380 | |
| 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 | 382 | /// 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 | 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 | 387 | try self.valueStart(); |
| 387 | try self.stream.writeAll(value_slice); | |
| 388 | try self.stream.print(fmt, args); | |
| 388 | 389 | self.valueDone(); |
| 389 | 390 | } |
| 390 | 391 | |
| ... | ... | @@ -584,6 +585,7 @@ pub fn WriteStream( |
| 584 | 585 | pub const emitNumber = @compileError("Deprecated; Use .write() instead."); |
| 585 | 586 | pub const emitString = @compileError("Deprecated; Use .write() instead."); |
| 586 | 587 | pub const emitJson = @compileError("Deprecated; Use .write() instead."); |
| 588 | pub const writePreformatted = @compileError("Deprecated; Use .print(\"{s}\", .{s}) instead."); | |
| 587 | 589 | }; |
| 588 | 590 | } |
| 589 | 591 |
lib/std/json/stringify_test.zig+5-5| ... | ... | @@ -402,7 +402,7 @@ test "comptime stringify" { |
| 402 | 402 | }, .{}, 8) catch unreachable; |
| 403 | 403 | } |
| 404 | 404 | |
| 405 | test "writePreformatted" { | |
| 405 | test "print" { | |
| 406 | 406 | var out_buf: [1024]u8 = undefined; |
| 407 | 407 | var slice_stream = std.io.fixedBufferStream(&out_buf); |
| 408 | 408 | const out = slice_stream.writer(); |
| ... | ... | @@ -412,11 +412,11 @@ test "writePreformatted" { |
| 412 | 412 | |
| 413 | 413 | try w.beginObject(); |
| 414 | 414 | try w.objectField("a"); |
| 415 | try w.writePreformatted("[ ]"); | |
| 415 | try w.print("[ ]", .{}); | |
| 416 | 416 | try w.objectField("b"); |
| 417 | 417 | try w.beginArray(); |
| 418 | try w.writePreformatted("[[]] "); | |
| 419 | try w.writePreformatted(" {}"); | |
| 418 | try w.print("[{s}] ", .{"[]"}); | |
| 419 | try w.print(" {}", .{12345}); | |
| 420 | 420 | try w.endArray(); |
| 421 | 421 | try w.endObject(); |
| 422 | 422 | |
| ... | ... | @@ -426,7 +426,7 @@ test "writePreformatted" { |
| 426 | 426 | \\ "a": [ ], |
| 427 | 427 | \\ "b": [ |
| 428 | 428 | \\ [[]] , |
| 429 | \\ {} | |
| 429 | \\ 12345 | |
| 430 | 430 | \\ ] |
| 431 | 431 | \\} |
| 432 | 432 | ; |