authorgravatar for 19653795+mateusz834@users.noreply.github.commateusz <19653795+mateusz834@users.noreply.github.com> 2023-04-07 14:01:09+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-04-07 15:01:09+03:00
log086639630800fc52bd727163e85d174ec1ac1103
tree34a1f0687d5fd201b41bf0292e83f187219bd5d1
parentdac62424f9dc1b775b7b47f954bea9f0326d81a1
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.json: allow returning custom errors from custom stringify


2 files changed, 19 insertions(+), 2 deletions(-)

lib/std/json.zig+18-1
...@@ -2268,7 +2268,7 @@ pub fn stringify(...@@ -2268,7 +2268,7 @@ pub fn stringify(
2268 value: anytype,2268 value: anytype,
2269 options: StringifyOptions,2269 options: StringifyOptions,
2270 out_stream: anytype,2270 out_stream: anytype,
2271) @TypeOf(out_stream).Error!void {2271) !void {
2272 const T = @TypeOf(value);2272 const T = @TypeOf(value);
2273 switch (@typeInfo(T)) {2273 switch (@typeInfo(T)) {
2274 .Float, .ComptimeFloat => {2274 .Float, .ComptimeFloat => {
...@@ -2767,3 +2767,20 @@ test "deserializing string with escape sequence into sentinel slice" {...@@ -2767,3 +2767,20 @@ test "deserializing string with escape sequence into sentinel slice" {
2767 // Double-check that we're getting the right result2767 // Double-check that we're getting the right result
2768 try testing.expect(mem.eql(u8, result, "\n"));2768 try testing.expect(mem.eql(u8, result, "\n"));
2769}2769}
2770
2771test "stringify struct with custom stringify that returns a custom error" {
2772 var ret = std.json.stringify(struct {
2773 field: Field = .{},
2774
2775 pub const Field = struct {
2776 field: ?[]*Field = null,
2777
2778 const Self = @This();
2779 pub fn jsonStringify(_: Self, _: StringifyOptions, _: anytype) error{CustomError}!void {
2780 return error.CustomError;
2781 }
2782 };
2783 }{}, StringifyOptions{}, std.io.null_writer);
2784
2785 try std.testing.expectError(error.CustomError, ret);
2786}
src/Autodoc.zig+1-1
...@@ -770,7 +770,7 @@ const DocData = struct {...@@ -770,7 +770,7 @@ const DocData = struct {
770 self: Expr,770 self: Expr,
771 opts: std.json.StringifyOptions,771 opts: std.json.StringifyOptions,
772 w: anytype,772 w: anytype,
773 ) !void {773 ) @TypeOf(w).Error!void {
774 const active_tag = std.meta.activeTag(self);774 const active_tag = std.meta.activeTag(self);
775 var jsw = std.json.writeStream(w, 15);775 var jsw = std.json.writeStream(w, 15);
776 if (opts.whitespace) |ws| jsw.whitespace = ws;776 if (opts.whitespace) |ws| jsw.whitespace = ws;