json: give enums a default stringify implementation

This commit is contained in:
Evin Yulo 2023-06-27 14:28:17 -04:00 committed by Andrew Kelley
parent b26fa4ec4b
commit ba6e5e65a0
2 changed files with 10 additions and 1 deletions

View File

@ -167,7 +167,7 @@ pub fn stringify(
return value.jsonStringify(options, out_stream);
}
@compileError("Unable to stringify enum '" ++ @typeName(T) ++ "'");
return try encodeJsonString(@tagName(value), options, out_stream);
},
.Union => {
if (comptime std.meta.trait.hasFn("jsonStringify")(T)) {

View File

@ -73,6 +73,15 @@ test "stringify many-item sentinel-terminated string" {
try teststringify("\"with unicode\\u0001\"", @as([*:0]const u8, "with unicode\u{1}"), StringifyOptions{ .string = .{ .String = .{ .escape_unicode = true } } });
}
test "stringify enums" {
const E = enum {
foo,
bar,
};
try teststringify("\"foo\"", E.foo, .{});
try teststringify("\"bar\"", E.bar, .{});
}
test "stringify tagged unions" {
const T = union(enum) {
nothing,