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(
22682268 value: anytype,
22692269 options: StringifyOptions,
22702270 out_stream: anytype,
2271) @TypeOf(out_stream).Error!void {
2271) !void {
22722272 const T = @TypeOf(value);
22732273 switch (@typeInfo(T)) {
22742274 .Float, .ComptimeFloat => {
......@@ -2767,3 +2767,20 @@ test "deserializing string with escape sequence into sentinel slice" {
27672767 // Double-check that we're getting the right result
27682768 try testing.expect(mem.eql(u8, result, "\n"));
27692769}
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 {
770770 self: Expr,
771771 opts: std.json.StringifyOptions,
772772 w: anytype,
773 ) !void {
773 ) @TypeOf(w).Error!void {
774774 const active_tag = std.meta.activeTag(self);
775775 var jsw = std.json.writeStream(w, 15);
776776 if (opts.whitespace) |ws| jsw.whitespace = ws;