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

added get function to std.json.Value #22802

Closed
wants to merge 2 commits into from

Conversation

curuvar
Copy link
Contributor

@curuvar curuvar commented Feb 7, 2025

Added a get function to easily get a specific item from a Value ObjectMap.

For example, given the following JSON:

{
  "a": [1, {"x": 2, "y": 3}, 4],
  "b": "something else"
}

Calling get(i64, &.{"a", "1", "y"}) will return 3, and get([]const u8, &.{"b"}) will return "something else".

Valid types are i64, f64, []const u8, bool, std.json.Value, std.json.Array, and std.json.ObjectMap.

Returns null if the key path cannot be found and error.WrongType if the data in the value
is incompatible with the requested type.

Copy link
Collaborator

@linusg linusg left a comment

Choose a reason for hiding this comment

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

This API looks extremely awkward, if anything something more widely useful like JSONPath (RFC 9535) should be implemented.

@curuvar
Copy link
Contributor Author

curuvar commented Feb 7, 2025

I personally don't think just passing a array of selectors is too awkward, but I can see how passing in a simple string might be easier.

I'll look into allowing something like get( u64, "some.path.4" )

@linusg
Copy link
Collaborator

linusg commented Feb 7, 2025

I think you should present a motivating use case before putting more work into this.

@curuvar
Copy link
Contributor Author

curuvar commented Feb 7, 2025

Let me think more on that. I may resubmit later.

@curuvar curuvar closed this Feb 7, 2025
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