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

proposal: encoding/json: add EvalJSONPointer function to support RFC 6901 #42009

Closed
baijum opened this issue Oct 16, 2020 · 8 comments
Closed

Comments

@baijum
Copy link

baijum commented Oct 16, 2020

RFC 6901 describes the JSON Pointer standard.

JSON Pointer defines a string syntax for identifying a specific value within a JSON document.

More advanced query languages like JSONPath and JMESPath is not required in many cases. Also including it as part of the standard library would be an overkill.

Since JSON Pointer is a small standard, the implementation would be easy.

The API could be something like this:

func EvalJSONPointer(v interface{}, format string) ([]byte, error) {...}
@Merovius
Copy link
Contributor

I think the API needs a bit of work. AIUI it takes an arbitrary Go value and returns a []byte - so is the intent that it marshals the referenced value into JSON? ISTM that it would be more useful to return the value as a Go value. But that mainly depends on how people would want to use it - there might even be a need for different function signatures, to do different things. In particular, the RFC poses two questions that need answers: 1. How is - handled and 2. how are errors handled? Again - we might need different answers for different use-cases.

Given that, as far as I can see, nothing in the stdlib benefits from this and the implementation also doesn't benefit from coupling with internal stdlib code, I think the best course of action here is to implement this outside the stdlib to experiment with the API and re-consider the benefits of including it once kinks in the API has been shaken out.

@odeke-em odeke-em changed the title enconding/json: add EvalJSONPointer function to support RFC 6901 proposal: encoding/json: add EvalJSONPointer function to support RFC 6901 Oct 16, 2020
@gopherbot gopherbot added this to the Proposal milestone Oct 16, 2020
@mvdan
Copy link
Member

mvdan commented Oct 16, 2020

I agree with Axel; implement this outside of the standard library first. https://golang.org/doc/faq#x_in_std

@baijum
Copy link
Author

baijum commented Oct 16, 2020

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Oct 28, 2020
@rsc
Copy link
Contributor

rsc commented Oct 28, 2020

encoding/json does support a generic JSON representation - map[string]interface{} - but it's not really the most efficient or preferred implementation most of the time. Would jsonpointer have to work on every possible struct that can be marshaled or unmarshaled with the json package?

Also, how commonly used is this? Why not keep using one of the choices outside the standard library?

@rsc rsc moved this from Incoming to Active in Proposals (old) Oct 29, 2020
@dolmen
Copy link
Contributor

dolmen commented Oct 30, 2020

As the author of package github.com/dolmen-go/jsonptr I don't think there is a need for a JSON Pointer implementation inside the stdlib. An implementation inside the stdlib would not be easier than any external one. The only valid reason for an stdlib implementation would be to reduce fragmentation by directing most users of JSON Pointer towards the use of that implementation.

@rsc wrote:

encoding/json does support a generic JSON representation - map[string]interface{} - but it's not really the most efficient or preferred implementation most of the time. Would jsonpointer have to work on every possible struct that can be marshaled or unmarshaled with the json package?

An under-documented feature of my own implementation is that it supports not just trees of []interface{} and map[string]interface{} as input document, but also json.RawMessage and any value implementing interface JSONDecoder (a subset of encoding/json.Decoder API). This allows much flexibility for input. However the same flexibility isn't offered for jsonptr.Set because of writability constraints.

@dolmen
Copy link
Contributor

dolmen commented Oct 30, 2020

I've also published a framework for benchmarking Go implementations of JSON Pointer here:
https://github.com/dolmen-go/jsonptr-benchmark

@rsc
Copy link
Contributor

rsc commented Nov 11, 2020

Based on the discussion above, this seems like a likely decline.

@rsc rsc moved this from Active to Likely Decline in Proposals (old) Nov 11, 2020
@rsc
Copy link
Contributor

rsc commented Nov 18, 2020

No change in consensus, so declined.

@rsc rsc closed this as completed Nov 18, 2020
@rsc rsc moved this from Likely Decline to Declined in Proposals (old) Nov 19, 2020
@golang golang locked and limited conversation to collaborators Nov 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

6 participants