Skip to content

Commit c30df07

Browse files
committed
std.json: update to new I/O API
also do a little bit of namespace cleanup
1 parent a288266 commit c30df07

14 files changed

+2921
-3245
lines changed

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)