wanglingsong / JsonSurfer

A streaming JsonPath processor in Java
MIT License
292 stars 55 forks source link

Filter does not support object fields starting with @ #65

Closed wolfgangcolsman closed 3 years ago

wolfgangcolsman commented 3 years ago

Steps to reproduce

Example JSON:

{
    "store": {
        "book": [
            {
                "@id": 1,
                "title": "Sayings of the Century"     
            }
        ]
    }
}

Query:

$..[?(@.@id==1)]

Code:

String json = "...";  // see JSON file above
String path = "$..[?(@.@id==1)]";

JsonSurfer surfer = JsonSurferJackson.INSTANCE;
surfer.configBuilder().bind(path, new JsonPathListener() {
    @Override
    public void onValue(Object value, ParsingContext context) {
    System.out.println(value);
    }
}).buildAndSurf(json);

Expected

{"@id":1,"title":"Sayings of the Century"}

Actual

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.SurfingConfiguration$Builder.bind(SurfingConfiguration.java:201)
    at io.zontal.commands.extract.examples.AstraZeneca.testAt(AstraZeneca.java:83)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
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)
    ... 31 more

Note

The issue is the @id, which is valid format in e.g. json-ld

wanglingsong commented 3 years ago

Try this: $..[?(@['@id']==1)

wolfgangcolsman commented 3 years ago

This changes the exception to

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.filter(JsonPathParser.java:770)
    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.SurfingConfiguration$Builder.bind(SurfingConfiguration.java:201)
    ...
Caused by: org.antlr.v4.runtime.InputMismatchException
    at org.antlr.v4.runtime.BailErrorStrategy.recoverInline(BailErrorStrategy.java:61)
    ... 32 more
wanglingsong commented 3 years ago

Sorry, I missed a brackets: $..[?(@['@id']==1)]

wolfgangcolsman commented 3 years ago

Awesome. Thank you for your help