Skip to content

std.json: support field aliases #8982

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

Closed
wants to merge 5 commits into from
Closed

std.json: support field aliases #8982

wants to merge 5 commits into from

Conversation

travisstaloch
Copy link
Contributor

@travisstaloch travisstaloch commented Jun 3, 2021

  • if a struct T passed to parse(T) contains a pub const __tags of the form
    shown below, these aliases will be used as the json keys instead of the field
    name.
  • adds std.mem.{indexOfSliceScalar, indexOfSliceScalarPos} which support
    finding aliases. adding these was my workaround to a compiler bug
    encountered when using a for loop for the same purpose.
    const S = struct {
        value: u8,
        pub const __tags = .{
            .value = .{ .alias = "__value" },
        };
    };

The names __tags and alias were chosen by me ad-hoc and may be easily changed.
Inspired by #1099 #1099 (comment)

- if a struct T passed to parse(T) contains a `pub const __tags` decl
  with an `alias` field, this alias will be used as the json key.
- adds std.mem.{indexOfSliceScalar, indexOfSliceScalarPos} which support
  finding aliases.  adding these was my workaround to a compiler bug
  encountered when using a for loop for the same purpose.
@travisstaloch travisstaloch changed the title json: support field aliases std.json: support field aliases Jun 3, 2021
@travisstaloch
Copy link
Contributor Author

@SpexGuy suspects that this is failing due to significant comptime usage and may have to wait for stage 2.

- if a struct T passed to parse(T) contains a `pub const __tags` decl
  with an `alias` field, this alias will be used as the json key.
- adds std.mem.{indexOfSliceScalar, indexOfSliceScalarPos} which support
  finding aliases.  adding these was my workaround to a compiler bug
  encountered when using a for loop for the same purpose.
@travisstaloch
Copy link
Contributor Author

moved to #8987 after bad rebase.

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.

1 participant