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

not support token contain '-' ? #104

Closed
iyaozhen opened this issue Jan 5, 2023 · 11 comments
Closed

not support token contain '-' ? #104

iyaozhen opened this issue Jan 5, 2023 · 11 comments

Comments

@iyaozhen
Copy link

iyaozhen commented Jan 5, 2023

func Test_parse(t *testing.T) {
	//jsonStr := `{"foo-bar": "test"}`

	_, err := jp.ParseString(`foo-bar`)
	if err != nil {
		t.Error(err)
	}
}

parse error at 4 in foo-bar

foo\-bar "foo-bar" and "foo\-bar" not work too.

I found - not in tokenMap, can supported it ?

@iyaozhen iyaozhen changed the title not support token with - ? not support token contain '-' ? Jan 5, 2023
@tigh-latte
Copy link

tigh-latte commented Jan 10, 2023

I just hit this today, it looks like it - isn't in the token map so it treats foo as one token and bar as another. Adding it to the map, however, causes script eval's to break.

@tigh-latte
Copy link

I have quickly thrown an incomplete fix together here which passes all current tests.

It's incomplete because it doesn't cover the fact that equations/scripts may error if they have a hyphen in the key, so we'd probably need more state.

@ohler55
Copy link
Owner

ohler55 commented Jan 10, 2023

So - is also an operation for scripts so it can not be treated as a token character. You can use the bracket notation to define a path though. ["foo-bar"]

@tigh-latte
Copy link

tigh-latte commented Jan 10, 2023

Aw class that'll work for me for the time being!

Can I ask if this is a bug? Any other jsonpath implementation (including the one on jsonpath.com) I've used hasn't required the usage of square brackets to access keys with hyphens.

If you agree I don't mind trying to delve into this at some point to try and sort it, I think the parser may need some sort of state machine.

@ohler55
Copy link
Owner

ohler55 commented Jan 11, 2023

Can speak for other parsers. I know some support it but don't deal well with scripts with a minus sign or require spaces. I elected to avoid having to guess what the user intended and went with an approach with no ambiguity. So not a bug for OjG.

@tigh-latte
Copy link

Dead on, thanks for the clarification.

@ohler55
Copy link
Owner

ohler55 commented Jan 11, 2023

I checked out the site and jsonpath.com does not support subtraction. OjG attempts to follow https://goessner.net/articles/JsonPath when possible.

@ohler55
Copy link
Owner

ohler55 commented Jan 11, 2023

Correction on jsonpath.com. It does support subtraction but fails if there is a - in the name.

@tigh-latte
Copy link

Cool, being honest I would expect a library to mirror jsonpath.com, but this library is awesome regardless so I don't mind working around this.

@ohler55
Copy link
Owner

ohler55 commented Jan 11, 2023

There really is no standard. Goessner was first out of the gate but others have followed and filling in the gaps in different ways. Check out https://cburgmer.github.io/json-path-comparison for a comparison of the various implementations.

@ohler55
Copy link
Owner

ohler55 commented Jan 13, 2023

Okay to close?

@ohler55 ohler55 closed this as completed Jan 15, 2023
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

No branches or pull requests

3 participants