ohler55 / ojg

Optimized JSON for Go
MIT License
857 stars 49 forks source link

Can I get a Normalized Path in JP? (ietf) #152

Closed xujianjun2000 closed 11 months ago

xujianjun2000 commented 11 months ago

Great Json tools, really!

As IETF says in https://datatracker.ietf.org/doc/draft-ietf-jsonpath-base/ (2.7. Normalized Paths) Is there Normalized Paths implementation in jp? How to get them in API?

thanks!!

ohler55 commented 11 months ago

The only call in jp that provides normalized paths is the jp.Walk() function.

xujianjun2000 commented 11 months ago

The only call in jp that provides normalized paths is the jp.Walk() function.

Yes,I did see that path in Walk(),but printed path.String(), seems they are not the exact definition in ITEF. Sometimes path.String() gives "dot notation", not always "bracket notation" when represents a Child node.

Below I quote the definition of Normalized Paths in 2.7 section:

2.7. Normalized Paths

A Normalized Path is a unique representation of the location of a node in a value which uniquely identifies the node in the value. Specifically, a Normalized Path is a JSONPath query with restricted syntax (defined below), e.g., $['book'][3], which when applied to the value results in a nodelist consisting of just the node identified by the Normalized Path. Note: a Normalized Path represents the identity of a node in a specific value. There is precisely one Normalized Path identifying any particular node in a value.

A nodelist may be represented compactly in JSON as an array of strings, where the strings are Normalized Paths.

Normalized Paths provide a predictable format that simplifies testing and post-processing of nodelists, e.g., to remove duplicate nodes. Normalized Paths are used in this document as result paths in examples.

Normalized Paths use the canonical bracket notation, rather than dot notation.

ohler55 commented 11 months ago

I'll have to add an itef string version.

xujianjun2000 commented 11 months ago

I'll have to add an itef string version.

Ohler! Great to hear the news, hope itef version coming soon. And, can the callback function in Expr.Modify(...) add a "path Expr" parameter to indicate the "normalized path" of the current modifying Node when the ballback func is invoked. I think this will be more useful to me in some case. Or is there already an approach to achieve it?

ohler55 commented 11 months ago

It will not be possible to add the path to the callback as the path is not carried around with the traversal of the data. It would have to be a completely different function that sacrifices performance for building the path.

xujianjun2000 commented 11 months ago

It will not be possible to add the path to the callback as the path is not carried around with the traversal of the data. It would have to be a completely different function that sacrifices performance for building the path.

Oh I see, thank you so much and your Great oj&jp !!!

ohler55 commented 11 months ago

What could be done is to add a jp.PathsTo(expr jp.Expr) function or something like that that would return a slice of paths that match the expr. It will be significantly slow but would be able to return paths to all the matches.

ohler55 commented 11 months ago

Something else you can try for now is to prepend a jp.Bracket to the path and it will or should print in the IETF bracketed format.

xujianjun2000 commented 11 months ago

What could be done is to add a jp.PathsTo(expr jp.Expr) function or something like that that would return a slice of paths that match the expr. It will be significantly slow but would be able to return paths to all the matches.

yes, that would be very slow. If offer the Path in callback in Modify(...) I think would be enough, since I want to aware which Node (normalized path is unique ID here) I actually modifying when matching multiple Nodes by one Expr.

xujianjun2000 commented 11 months ago

Something else you can try for now is to prepend a jp.Bracket to the path and it will or should print in the IETF bracketed format.

That is the one I want for now, Thank you for you reply!!!~~~~

ohler55 commented 11 months ago

I created issue #153 for a jp.PathsTo() function. It will not be possible to change jp.Modify().

ohler55 commented 11 months ago

I've started a branch named jp-locate. It adds a Expr.BracketString() function and the start of Expr.Locate(). You are welcome to experiment with it but realize it is a work in progress.

xujianjun2000 commented 10 months ago

I've started a branch named jp-locate. It adds a Expr.BracketString() function and the start of Expr.Locate(). You are welcome to experiment with it but realize it is a work in progress.

ohler,that is nice~