Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support stringifying non-exhaustive enum to json #21228

Merged
merged 3 commits into from
Sep 7, 2024

Conversation

pfgithub
Copy link
Contributor

test "stringify non-exhaustive enum" {
    const E = enum(u8) {
        foo = 0,
        _,
    };
    try testStringify("\"foo\"", E.foo, .{});
    try testStringify("1", @as(E, @enumFromInt(1)), .{});
}

It stringifies the named ones to strings and unnamed ones to a number, which parses just fine into an enum already.

I'm not sure if there's a better way to detect if the active field is unnamed - if doesn't support both _ and else, and tagName doesn't return an optional for non-exhaustive enums.

Copy link
Sponsor Contributor

@thejoshwolfe thejoshwolfe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great idea! and thanks for the test! just missing documentation. The other suggestion is optional.

lib/std/json/stringify.zig Outdated Show resolved Hide resolved
lib/std/json/stringify.zig Outdated Show resolved Hide resolved
@pfgithub
Copy link
Contributor Author

pfgithub commented Sep 7, 2024

Added to the documentation comment, switched to recurse instead of writing raw text, and updated for the lowercase std.builtin.Type change

Copy link
Sponsor Contributor

@thejoshwolfe thejoshwolfe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@thejoshwolfe thejoshwolfe merged commit c97db8e into ziglang:master Sep 7, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants