diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-07-10 12:04:27 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-10 12:04:27 +0200 |
| commit | 1a998886c863a1829d649f196093f1058cd9cf13 (patch) | |
| tree | db1b3b0a043a113cfb6544e8103cb64f8e4e4d8f /lib/std/json/stringify.zig | |
| parent | 5b4b033236a7bed19c90faf7fefbc1990911cfef (diff) | |
| parent | 10d6db5d7d1fb62ee2915a7ad2c7feb771ea3bbb (diff) | |
| download | zig-1a998886c863a1829d649f196093f1058cd9cf13.tar.gz zig-1a998886c863a1829d649f196093f1058cd9cf13.zip | |
Merge pull request #24329 from ziglang/writergate
Deprecates all existing std.io readers and writers in favor of the newly
provided std.io.Reader and std.io.Writer which are non-generic and have the
buffer above the vtable - in other words the buffer is in the interface, not
the implementation. This means that although Reader and Writer are no longer
generic, they are still transparent to optimization; all of the interface
functions have a concrete hot path operating on the buffer, and only make
vtable calls when the buffer is full.
Diffstat (limited to 'lib/std/json/stringify.zig')
| -rw-r--r-- | lib/std/json/stringify.zig | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/std/json/stringify.zig b/lib/std/json/stringify.zig index db2ba85318..aa49573695 100644 --- a/lib/std/json/stringify.zig +++ b/lib/std/json/stringify.zig @@ -38,7 +38,7 @@ pub const StringifyOptions = struct { emit_nonportable_numbers_as_strings: bool = false, }; -/// Writes the given value to the `std.io.Writer` stream. +/// Writes the given value to the `std.io.GenericWriter` stream. /// See `WriteStream` for how the given value is serialized into JSON. /// The maximum nesting depth of the output JSON document is 256. /// See also `stringifyMaxDepth` and `stringifyArbitraryDepth`. @@ -81,7 +81,7 @@ pub fn stringifyArbitraryDepth( } /// Calls `stringifyArbitraryDepth` and stores the result in dynamically allocated memory -/// instead of taking a `std.io.Writer`. +/// instead of taking a `std.io.GenericWriter`. /// /// Caller owns returned memory. pub fn stringifyAlloc( @@ -469,7 +469,6 @@ pub fn WriteStream( /// * When option `emit_nonportable_numbers_as_strings` is true, if the value is outside the range `+-1<<53` (the precise integer range of f64), it is rendered as a JSON string in base 10. Otherwise, it is rendered as JSON number. /// * Zig floats -> JSON number or string. /// * If the value cannot be precisely represented by an f64, it is rendered as a JSON string. Otherwise, it is rendered as JSON number. - /// * TODO: Float rendering will likely change in the future, e.g. to remove the unnecessary "e+00". /// * Zig `[]const u8`, `[]u8`, `*[N]u8`, `@Vector(N, u8)`, and similar -> JSON string. /// * See `StringifyOptions.emit_strings_as_arrays`. /// * If the content is not valid UTF-8, rendered as an array of numbers instead. @@ -689,7 +688,8 @@ fn outputUnicodeEscape(codepoint: u21, out_stream: anytype) !void { // then it may be represented as a six-character sequence: a reverse solidus, followed // by the lowercase letter u, followed by four hexadecimal digits that encode the character's code point. try out_stream.writeAll("\\u"); - try std.fmt.formatIntValue(codepoint, "x", std.fmt.FormatOptions{ .width = 4, .fill = '0' }, out_stream); + //try w.printInt("x", .{ .width = 4, .fill = '0' }, codepoint); + try std.fmt.format(out_stream, "{x:0>4}", .{codepoint}); } else { assert(codepoint <= 0x10FFFF); // To escape an extended character that is not in the Basic Multilingual Plane, @@ -697,9 +697,11 @@ fn outputUnicodeEscape(codepoint: u21, out_stream: anytype) !void { const high = @as(u16, @intCast((codepoint - 0x10000) >> 10)) + 0xD800; const low = @as(u16, @intCast(codepoint & 0x3FF)) + 0xDC00; try out_stream.writeAll("\\u"); - try std.fmt.formatIntValue(high, "x", std.fmt.FormatOptions{ .width = 4, .fill = '0' }, out_stream); + //try w.printInt("x", .{ .width = 4, .fill = '0' }, high); + try std.fmt.format(out_stream, "{x:0>4}", .{high}); try out_stream.writeAll("\\u"); - try std.fmt.formatIntValue(low, "x", std.fmt.FormatOptions{ .width = 4, .fill = '0' }, out_stream); + //try w.printInt("x", .{ .width = 4, .fill = '0' }, low); + try std.fmt.format(out_stream, "{x:0>4}", .{low}); } } |
