ohler55 / ojg

Optimized JSON for Go
MIT License
839 stars 50 forks source link

not support token contain '-' ? #104

Closed iyaozhen closed 1 year ago

iyaozhen commented 1 year ago
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 ?

tigh-latte commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Dead on, thanks for the clarification.

ohler55 commented 1 year ago

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

ohler55 commented 1 year ago

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

tigh-latte commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Okay to close?