json-path / JsonPath

Java JsonPath implementation
Apache License 2.0
8.94k stars 1.65k forks source link

ClassCastException in ValueNode$JsonNode.asValueListNode #441

Open aabeling opened 6 years ago

aabeling commented 6 years ago

Hi,

I am using json-path-2.4.0.

My document is the following

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95,
                "a": {
                    "data": [
                        {
                            "foo": "bar"
                        }
                    ]
                }
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

And the code is

Configuration configuration = Configuration
            .builder()
            .mappingProvider(new JacksonMappingProvider())
            .jsonProvider(new JacksonJsonNodeJsonProvider())
            .build()
            .addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
final DocumentContext documentContext = JsonPath
            .parse(json, configuration);

String jsonPath = "$.store.book[?('bar' in @.a.data[*].foo)].title";
Object result = documentContext.read(jsonPath);

It fails with

java.lang.ClassCastException: com.fasterxml.jackson.databind.node.ArrayNode cannot be cast to java.util.List
    at com.jayway.jsonpath.internal.filter.ValueNode$JsonNode.asValueListNode(ValueNode.java:341)
    at com.jayway.jsonpath.internal.filter.EvaluatorFactory$InEvaluator.evaluate(EvaluatorFactory.java:173)
    at com.jayway.jsonpath.internal.filter.RelationalExpressionNode.apply(RelationalExpressionNode.java:44)
    at com.jayway.jsonpath.internal.filter.FilterCompiler$CompiledFilter.apply(FilterCompiler.java:415)
    at com.jayway.jsonpath.internal.path.PredicatePathToken.accept(PredicatePathToken.java:76)
    at com.jayway.jsonpath.internal.path.PredicatePathToken.evaluate(PredicatePathToken.java:59)
    at com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:81)
    at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:79)
    at com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:81)
    at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:79)
    at com.jayway.jsonpath.internal.path.RootPathToken.evaluate(RootPathToken.java:62)
    at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:53)
    at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:61)
    at com.jayway.jsonpath.JsonPath.read(JsonPath.java:187)
    at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:102)
    at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:89)
    at de.mobilcom.fitworkflow.util.JsonPathTest.test_jsonBook(JsonPathTest.java:60)
...

Any help is appreciated.

Best regards Achim

dmwmishere commented 5 years ago

Got same problem:


JsonPath.using(Configuration.defaultConfiguration().jsonProvider(new JacksonJsonNodeJsonProvider())).parse("[{\"f1\": [1,2,3]}, {\"f1\": [4,5,6,3,2]}]").read("$[1].f1[?(@ in $[0].f1)]");
Causes error:
java.lang.ClassCastException: com.fasterxml.jackson.databind.node.ArrayNode cannot be cast to java.util.List

    at com.jayway.jsonpath.internal.filter.ValueNode$JsonNode.asValueListNode(ValueNode.java:341)
    at com.jayway.jsonpath.internal.filter.EvaluatorFactory$InEvaluator.evaluate(EvaluatorFactory.java:173)
    at com.jayway.jsonpath.internal.filter.RelationalExpressionNode.apply(RelationalExpressionNode.java:44)
    at com.jayway.jsonpath.internal.filter.FilterCompiler$CompiledFilter.apply(FilterCompiler.java:415)
    at com.jayway.jsonpath.internal.path.PredicatePathToken.accept(PredicatePathToken.java:76)
    at com.jayway.jsonpath.internal.path.PredicatePathToken.evaluate(PredicatePathToken.java:59)
    at com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:81)
    at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:79)
    at com.jayway.jsonpath.internal.path.PathToken.handleArrayIndex(PathToken.java:134)
    at com.jayway.jsonpath.internal.path.ArrayPathToken.evaluateIndexOperation(ArrayPathToken.java:63)
    at com.jayway.jsonpath.internal.path.ArrayPathToken.evaluate(ArrayPathToken.java:52)
    at com.jayway.jsonpath.internal.path.RootPathToken.evaluate(RootPathToken.java:62)
    at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:53)
    at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:61)
    at com.jayway.jsonpath.JsonPath.read(JsonPath.java:187)
    at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:102)
    at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:89)
    at org.dmwm.test.jxpath.JsonPathTest.test_06_0_jackson_bug(JsonPathTest.java:111)
    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.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.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
riyasvaliya commented 5 years ago

I have the same problem. Configurations with either JacksonJsonProvider or JacksonJsonNodeJsonProvider will throw this exception if the 'in' operator is used.

v-vmikheev commented 5 years ago

Hello! Any progress on this?

DinoChiesa commented 4 years ago

Same problem. I'm using JacksonJsonNodeJsonProvider.