ohler55/ojg

not support token contain '-' ?

iyaozhen opened this issue · 11 comments

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 ?

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.

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.

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"]

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.

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.

Dead on, thanks for the clarification.

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

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

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.

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.

Okay to close?