JsonSurfer icon indicating copy to clipboard operation
JsonSurfer copied to clipboard

Results do not match other implementations

Open cburgmer opened this issue 5 years ago • 5 comments

The following queries provide results that do not match those of other implementations of JSONPath (compare https://cburgmer.github.io/json-path-comparison/):

  • [x] $.2 Input:

    {"a": "first", "2": "second", "b": "third"}
    

    Expected output:

    ["second"]
    

    Error:

    Exception in thread "main" org.antlr.v4.runtime.misc.ParseCancellationException
    	at org.antlr.v4.runtime.BailErrorStrategy.recoverInline(BailErrorStrategy.java:66)
    	at org.antlr.v4.runtime.Parser.match(Parser.java:206)
    	at org.jsfr.json.compiler.JsonPathParser.childNode(JsonPathParser.java:672)
    	at org.jsfr.json.compiler.JsonPathParser.relativePath(JsonPathParser.java:272)
    	at org.jsfr.json.compiler.JsonPathParser.path(JsonPathParser.java:159)
    	at org.jsfr.json.compiler.JsonPathCompiler.compile(JsonPathCompiler.java:283)
    	at org.jsfr.json.compiler.JsonPathCompiler.compile(JsonPathCompiler.java:273)
    	at org.jsfr.json.JsonSurfer.collectAll(JsonSurfer.java:262)
    	at query.App.main(App.java:27)
    Caused by: org.antlr.v4.runtime.InputMismatchException
    	at org.antlr.v4.runtime.BailErrorStrategy.recoverInline(BailErrorStrategy.java:61)
    	... 8 more
    
  • [ ] $[-1] Input:

    ["first", "second", "third"]
    

    Expected output:

    ["third"]
    

    Actual output:

    []
    
  • [ ] $[-1:] Input:

    ["first", "second", "third"]
    

    Expected output:

    ["third"]
    

    Actual output:

    ["first", "second", "third"]
    
  • [x] $[?(@.key=="some.value")] Input:

    [{"key": "some"}, {"key": "value"}, {"key": "some.value"}]
    

    Expected output:

    [{"key": "some.value"}]
    

    Error:

    line 1:11 token recognition error at: '"'
    line 1:22 token recognition error at: '"'
    line 1:12 no viable alternative at input '@.key==some'
    Exception in thread "main" org.antlr.v4.runtime.misc.ParseCancellationException
    	at org.antlr.v4.runtime.BailErrorStrategy.recover(BailErrorStrategy.java:51)
    	at org.jsfr.json.compiler.JsonPathParser.filterExpr(JsonPathParser.java:947)
    	at org.jsfr.json.compiler.JsonPathParser.filter(JsonPathParser.java:768)
    	at org.jsfr.json.compiler.JsonPathParser.relativePath(JsonPathParser.java:307)
    	at org.jsfr.json.compiler.JsonPathParser.path(JsonPathParser.java:159)
    	at org.jsfr.json.compiler.JsonPathCompiler.compile(JsonPathCompiler.java:283)
    	at org.jsfr.json.compiler.JsonPathCompiler.compile(JsonPathCompiler.java:273)
    	at org.jsfr.json.JsonSurfer.collectAll(JsonSurfer.java:262)
    	at query.App.main(App.java:27)
    Caused by: org.antlr.v4.runtime.NoViableAltException
    	at org.antlr.v4.runtime.atn.ParserATNSimulator.noViableAlt(ParserATNSimulator.java:2026)
    	at org.antlr.v4.runtime.atn.ParserATNSimulator.execATN(ParserATNSimulator.java:467)
    	at org.antlr.v4.runtime.atn.ParserATNSimulator.adaptivePredict(ParserATNSimulator.java:393)
    	at org.jsfr.json.compiler.JsonPathParser.filterExpr(JsonPathParser.java:843)
    	... 7 more
    

For reference, the output was generated by the program in https://github.com/cburgmer/json-path-comparison/tree/master/implementations/Java_com.github.jsurfer.

cburgmer avatar Feb 03 '20 21:02 cburgmer

I can try to fix the last case first

wanglingsong avatar Feb 10 '20 07:02 wanglingsong

Due to the streaming nature of JsonSurfer, Case 2 and 3 with negative step are very hard to implement

wanglingsong avatar Feb 17 '20 08:02 wanglingsong

Just out of curiosity, for a query $[-n] you could keep a window with n entries. When the stream stops you pick the first entry from that window and have your candidate. The upper memory limit for the window would depend on the size of the entries in the list you are processing. This is very likely no different to what you are currently doing, just n times (= constant) more.

cburgmer avatar Feb 17 '20 09:02 cburgmer

Cool. It does make sense. I will try.

wanglingsong avatar Feb 19 '20 06:02 wanglingsong

I've just reviewed source code and couldn't figure out a quick fix. So Case 2 & 3 won't be supported in near release

wanglingsong avatar Feb 27 '20 11:02 wanglingsong