Skip to content

Commit e43617e

Browse files
authored
Merge pull request #24505 from ziglang/json
update std.json and std.zon to new I/O API
2 parents c58cce7 + 0fb7a0a commit e43617e

27 files changed

+4302
-4662
lines changed

lib/compiler/resinator/main.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,14 @@ pub fn main() !void {
290290
};
291291
defer depfile.close();
292292

293-
const depfile_writer = depfile.deprecatedWriter();
294-
var depfile_buffered_writer = std.io.bufferedWriter(depfile_writer);
293+
var depfile_buffer: [1024]u8 = undefined;
294+
var depfile_writer = depfile.writer(&depfile_buffer);
295295
switch (options.depfile_fmt) {
296296
.json => {
297-
var write_stream = std.json.writeStream(depfile_buffered_writer.writer(), .{ .whitespace = .indent_2 });
298-
defer write_stream.deinit();
297+
var write_stream: std.json.Stringify = .{
298+
.writer = &depfile_writer.interface,
299+
.options = .{ .whitespace = .indent_2 },
300+
};
299301

300302
try write_stream.beginArray();
301303
for (dependencies_list.items) |dep_path| {
@@ -304,7 +306,7 @@ pub fn main() !void {
304306
try write_stream.endArray();
305307
},
306308
}
307-
try depfile_buffered_writer.flush();
309+
try depfile_writer.interface.flush();
308310
}
309311
}
310312

lib/std/Build/Cache/Path.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,19 @@ pub fn formatEscapeString(path: Path, writer: *std.io.Writer) std.io.Writer.Erro
161161
}
162162
}
163163

164+
/// Deprecated, use double quoted escape to print paths.
164165
pub fn fmtEscapeChar(path: Path) std.fmt.Formatter(Path, formatEscapeChar) {
165166
return .{ .data = path };
166167
}
167168

169+
/// Deprecated, use double quoted escape to print paths.
168170
pub fn formatEscapeChar(path: Path, writer: *std.io.Writer) std.io.Writer.Error!void {
169171
if (path.root_dir.path) |p| {
170-
try std.zig.charEscape(p, writer);
171-
if (path.sub_path.len > 0) try std.zig.charEscape(fs.path.sep_str, writer);
172+
for (p) |byte| try std.zig.charEscape(byte, writer);
173+
if (path.sub_path.len > 0) try writer.writeByte(fs.path.sep);
172174
}
173175
if (path.sub_path.len > 0) {
174-
try std.zig.charEscape(path.sub_path, writer);
176+
for (path.sub_path) |byte| try std.zig.charEscape(byte, writer);
175177
}
176178
}
177179

lib/std/Io/Reader.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,9 +990,9 @@ pub fn discardDelimiterLimit(r: *Reader, delimiter: u8, limit: Limit) DiscardDel
990990
/// Returns `error.EndOfStream` if and only if there are fewer than `n` bytes
991991
/// remaining.
992992
///
993-
/// Asserts buffer capacity is at least `n`.
993+
/// If the end of stream is not encountered, asserts buffer capacity is at
994+
/// least `n`.
994995
pub fn fill(r: *Reader, n: usize) Error!void {
995-
assert(n <= r.buffer.len);
996996
if (r.seek + n <= r.end) {
997997
@branchHint(.likely);
998998
return;

lib/std/json.zig

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//! The high-level `stringify` serializes a Zig or `Value` type into JSON.
1111

1212
const builtin = @import("builtin");
13-
const testing = @import("std").testing;
14-
const ArrayList = @import("std").ArrayList;
13+
const std = @import("std");
14+
const testing = std.testing;
1515

1616
test Scanner {
1717
var scanner = Scanner.initCompleteInput(testing.allocator, "{\"foo\": 123}\n");
@@ -41,11 +41,13 @@ test Value {
4141
try testing.expectEqualSlices(u8, "goes", parsed.value.object.get("anything").?.string);
4242
}
4343

44-
test writeStream {
45-
var out = ArrayList(u8).init(testing.allocator);
44+
test Stringify {
45+
var out: std.io.Writer.Allocating = .init(testing.allocator);
46+
var write_stream: Stringify = .{
47+
.writer = &out.writer,
48+
.options = .{ .whitespace = .indent_2 },
49+
};
4650
defer out.deinit();
47-
var write_stream = writeStream(out.writer(), .{ .whitespace = .indent_2 });
48-
defer write_stream.deinit();
4951
try write_stream.beginObject();
5052
try write_stream.objectField("foo");
5153
try write_stream.write(123);
@@ -55,16 +57,7 @@ test writeStream {
5557
\\ "foo": 123
5658
\\}
5759
;
58-
try testing.expectEqualSlices(u8, expected, out.items);
59-
}
60-
61-
test stringify {
62-
var out = ArrayList(u8).init(testing.allocator);
63-
defer out.deinit();
64-
65-
const T = struct { a: i32, b: []const u8 };
66-
try stringify(T{ .a = 123, .b = "xy" }, .{}, out.writer());
67-
try testing.expectEqualSlices(u8, "{\"a\":123,\"b\":\"xy\"}", out.items);
60+
try testing.expectEqualSlices(u8, expected, out.getWritten());
6861
}
6962

7063
pub const ObjectMap = @import("json/dynamic.zig").ObjectMap;
@@ -73,18 +66,18 @@ pub const Value = @import("json/dynamic.zig").Value;
7366

7467
pub const ArrayHashMap = @import("json/hashmap.zig").ArrayHashMap;
7568

76-
pub const validate = @import("json/scanner.zig").validate;
77-
pub const Error = @import("json/scanner.zig").Error;
78-
pub const reader = @import("json/scanner.zig").reader;
79-
pub const default_buffer_size = @import("json/scanner.zig").default_buffer_size;
80-
pub const Token = @import("json/scanner.zig").Token;
81-
pub const TokenType = @import("json/scanner.zig").TokenType;
82-
pub const Diagnostics = @import("json/scanner.zig").Diagnostics;
83-
pub const AllocWhen = @import("json/scanner.zig").AllocWhen;
84-
pub const default_max_value_len = @import("json/scanner.zig").default_max_value_len;
85-
pub const Reader = @import("json/scanner.zig").Reader;
86-
pub const Scanner = @import("json/scanner.zig").Scanner;
87-
pub const isNumberFormattedLikeAnInteger = @import("json/scanner.zig").isNumberFormattedLikeAnInteger;
69+
pub const Scanner = @import("json/Scanner.zig");
70+
pub const validate = Scanner.validate;
71+
pub const Error = Scanner.Error;
72+
pub const reader = Scanner.reader;
73+
pub const default_buffer_size = Scanner.default_buffer_size;
74+
pub const Token = Scanner.Token;
75+
pub const TokenType = Scanner.TokenType;
76+
pub const Diagnostics = Scanner.Diagnostics;
77+
pub const AllocWhen = Scanner.AllocWhen;
78+
pub const default_max_value_len = Scanner.default_max_value_len;
79+
pub const Reader = Scanner.Reader;
80+
pub const isNumberFormattedLikeAnInteger = Scanner.isNumberFormattedLikeAnInteger;
8881

8982
pub const ParseOptions = @import("json/static.zig").ParseOptions;
9083
pub const Parsed = @import("json/static.zig").Parsed;
@@ -99,27 +92,49 @@ pub const innerParseFromValue = @import("json/static.zig").innerParseFromValue;
9992
pub const ParseError = @import("json/static.zig").ParseError;
10093
pub const ParseFromValueError = @import("json/static.zig").ParseFromValueError;
10194

102-
pub const StringifyOptions = @import("json/stringify.zig").StringifyOptions;
103-
pub const stringify = @import("json/stringify.zig").stringify;
104-
pub const stringifyMaxDepth = @import("json/stringify.zig").stringifyMaxDepth;
105-
pub const stringifyArbitraryDepth = @import("json/stringify.zig").stringifyArbitraryDepth;
106-
pub const stringifyAlloc = @import("json/stringify.zig").stringifyAlloc;
107-
pub const writeStream = @import("json/stringify.zig").writeStream;
108-
pub const writeStreamMaxDepth = @import("json/stringify.zig").writeStreamMaxDepth;
109-
pub const writeStreamArbitraryDepth = @import("json/stringify.zig").writeStreamArbitraryDepth;
110-
pub const WriteStream = @import("json/stringify.zig").WriteStream;
111-
pub const encodeJsonString = @import("json/stringify.zig").encodeJsonString;
112-
pub const encodeJsonStringChars = @import("json/stringify.zig").encodeJsonStringChars;
113-
114-
pub const Formatter = @import("json/fmt.zig").Formatter;
115-
pub const fmt = @import("json/fmt.zig").fmt;
95+
pub const Stringify = @import("json/Stringify.zig");
96+
97+
/// Returns a formatter that formats the given value using stringify.
98+
pub fn fmt(value: anytype, options: Stringify.Options) Formatter(@TypeOf(value)) {
99+
return Formatter(@TypeOf(value)){ .value = value, .options = options };
100+
}
101+
102+
test fmt {
103+
const expectFmt = std.testing.expectFmt;
104+
try expectFmt("123", "{f}", .{fmt(@as(u32, 123), .{})});
105+
try expectFmt(
106+
\\{"num":927,"msg":"hello","sub":{"mybool":true}}
107+
, "{f}", .{fmt(struct {
108+
num: u32,
109+
msg: []const u8,
110+
sub: struct {
111+
mybool: bool,
112+
},
113+
}{
114+
.num = 927,
115+
.msg = "hello",
116+
.sub = .{ .mybool = true },
117+
}, .{})});
118+
}
119+
120+
/// Formats the given value using stringify.
121+
pub fn Formatter(comptime T: type) type {
122+
return struct {
123+
value: T,
124+
options: Stringify.Options,
125+
126+
pub fn format(self: @This(), writer: *std.Io.Writer) std.Io.Writer.Error!void {
127+
try Stringify.value(self.value, self.options, writer);
128+
}
129+
};
130+
}
116131

117132
test {
118133
_ = @import("json/test.zig");
119-
_ = @import("json/scanner.zig");
134+
_ = Scanner;
120135
_ = @import("json/dynamic.zig");
121136
_ = @import("json/hashmap.zig");
122137
_ = @import("json/static.zig");
123-
_ = @import("json/stringify.zig");
138+
_ = Stringify;
124139
_ = @import("json/JSONTestSuite_test.zig");
125140
}

0 commit comments

Comments
 (0)