zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit d34201c8491007c5a24b4175a5170a5b6cc3d55e (tree)
parent 36c57c3ba17c45a4c9b902041db9bb993dbc67b9
Author: Philipp Lühmann <philipp@luehmann.dev>
Date:   Wed,  9 Aug 2023 01:26:46 +0200

std.json: stringify enum literals (#16742)


Diffstat:
Mlib/std/json/stringify.zig | 3++-
Mlib/std/json/stringify_test.zig | 5+++++
2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/std/json/stringify.zig b/lib/std/json/stringify.zig @@ -181,6 +181,7 @@ pub fn writeStreamArbitraryDepth( /// * If the union declares a method `pub fn jsonStringify(self: *@This(), jw: anytype) !void`, it is called to do the serialization instead of the default behavior. The given `jw` is a pointer to this `WriteStream`. /// * Zig `enum` -> JSON string naming the active tag. /// * If the enum declares a method `pub fn jsonStringify(self: *@This(), jw: anytype) !void`, it is called to do the serialization instead of the default behavior. The given `jw` is a pointer to this `WriteStream`. +/// * Zig untyped enum literal -> JSON string naming the active tag. /// * Zig error -> JSON string naming the error. /// * Zig `*T` -> the rendering of `T`. Note there is no guard against circular-reference infinite recursion. /// @@ -449,7 +450,7 @@ pub fn WriteStream( return try self.write(null); } }, - .Enum => { + .Enum, .EnumLiteral => { if (comptime std.meta.trait.hasFn("jsonStringify")(T)) { return value.jsonStringify(self); } diff --git a/lib/std/json/stringify_test.zig b/lib/std/json/stringify_test.zig @@ -172,6 +172,11 @@ test "stringify enums" { try testStringify("\"bar\"", E.bar, .{}); } +test "stringify enum literals" { + try testStringify("\"foo\"", .foo, .{}); + try testStringify("\"bar\"", .bar, .{}); +} + test "stringify tagged unions" { const T = union(enum) { nothing,