mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 04:48:20 +00:00
json: give enums a default stringify implementation
This commit is contained in:
parent
b26fa4ec4b
commit
ba6e5e65a0
@ -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)) {
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user