diff options
| author | mateusz <19653795+mateusz834@users.noreply.github.com> | 2023-04-07 14:01:09 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-07 15:01:09 +0300 |
| commit | 086639630800fc52bd727163e85d174ec1ac1103 (patch) | |
| tree | 34a1f0687d5fd201b41bf0292e83f187219bd5d1 /lib/std/json.zig | |
| parent | dac62424f9dc1b775b7b47f954bea9f0326d81a1 (diff) | |
| download | zig-086639630800fc52bd727163e85d174ec1ac1103.tar.gz zig-086639630800fc52bd727163e85d174ec1ac1103.zip | |
std.json: allow returning custom errors from custom stringify
Diffstat (limited to 'lib/std/json.zig')
| -rw-r--r-- | lib/std/json.zig | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/lib/std/json.zig b/lib/std/json.zig index d2467fb2cd..096bed8d06 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -2268,7 +2268,7 @@ pub fn stringify( value: anytype, options: StringifyOptions, out_stream: anytype, -) @TypeOf(out_stream).Error!void { +) !void { const T = @TypeOf(value); switch (@typeInfo(T)) { .Float, .ComptimeFloat => { @@ -2767,3 +2767,20 @@ test "deserializing string with escape sequence into sentinel slice" { // Double-check that we're getting the right result try testing.expect(mem.eql(u8, result, "\n")); } + +test "stringify struct with custom stringify that returns a custom error" { + var ret = std.json.stringify(struct { + field: Field = .{}, + + pub const Field = struct { + field: ?[]*Field = null, + + const Self = @This(); + pub fn jsonStringify(_: Self, _: StringifyOptions, _: anytype) error{CustomError}!void { + return error.CustomError; + } + }; + }{}, StringifyOptions{}, std.io.null_writer); + + try std.testing.expectError(error.CustomError, ret); +} |
