From db59d0ba216bc246657e52930ad8ecb5c67eda4b Mon Sep 17 00:00:00 2001 From: Jochen Berger Date: Fri, 17 Mar 2017 09:35:02 +0100 Subject: [PATCH 1/6] use json-smart to parse the query --- .../com/jayway/jsonpath/internal/filter/ValueNode.java | 10 +++++++++- .../src/test/java/com/jayway/jsonpath/JacksonTest.java | 10 ++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java index 7506358a..2eb45939 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java @@ -11,6 +11,10 @@ import com.jayway.jsonpath.internal.Utils; import com.jayway.jsonpath.internal.path.PathCompiler; import com.jayway.jsonpath.internal.path.PredicateContextImpl; import com.jayway.jsonpath.spi.json.JsonProvider; + +import net.minidev.json.parser.JSONParser; +import net.minidev.json.parser.ParseException; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -343,7 +347,11 @@ public abstract class ValueNode { } public Object parse(Predicate.PredicateContext ctx){ - return parsed ? json : ctx.configuration().jsonProvider().parse(json.toString()); + try { + return parsed ? json : new JSONParser(JSONParser.MODE_PERMISSIVE).parse(json.toString()); + } catch (ParseException e) { + throw new IllegalArgumentException(e); + } } public boolean isParsed() { diff --git a/json-path/src/test/java/com/jayway/jsonpath/JacksonTest.java b/json-path/src/test/java/com/jayway/jsonpath/JacksonTest.java index 7799c9d7..ef79e22a 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JacksonTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JacksonTest.java @@ -45,4 +45,14 @@ public class JacksonTest extends BaseTest { assertThat(date).isEqualTo(now); } + @Test + // https://github.com/jayway/JsonPath/issues/275 + public void single_quotes_work_with_in_filter() { + final String jsonArray = "[{\"foo\": \"bar\"}, {\"foo\": \"baz\"}]"; + final Object readFromSingleQuote = JsonPath.using(JACKSON_CONFIGURATION).parse(jsonArray).read("$.[?(@.foo in ['bar'])].foo"); + final Object readFromDoubleQuote = JsonPath.using(JACKSON_CONFIGURATION).parse(jsonArray).read("$.[?(@.foo in [\"bar\"])].foo"); + assertThat(readFromSingleQuote).isEqualTo(readFromDoubleQuote); + + } + } From 6cf30fd1a0be42aa0e8385b1ff8ca99e7fbd74be Mon Sep 17 00:00:00 2001 From: jochenberger Date: Fri, 17 Mar 2017 11:54:48 +0100 Subject: [PATCH 2/6] Update ValueNode.java --- .../jsonpath/internal/filter/ValueNode.java | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java index 2eb45939..f1cda9eb 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java @@ -318,11 +318,11 @@ public abstract class ValueNode { @Override public Class type(Predicate.PredicateContext ctx) { - if(ctx.configuration().jsonProvider().isArray(parse(ctx))) return List.class; - else if(ctx.configuration().jsonProvider().isMap(parse(ctx))) return Map.class; - else if(ctx.configuration().jsonProvider().unwrap(parse(ctx)) instanceof Number) return Number.class; - else if(ctx.configuration().jsonProvider().unwrap(parse(ctx)) instanceof String) return String.class; - else if(ctx.configuration().jsonProvider().unwrap(parse(ctx)) instanceof Boolean) return Boolean.class; + if(isArray(ctx)) return List.class; + else if(isMap(ctx)) return Map.class; + else if(parse(ctx) instanceof Number) return Number.class; + else if(parse(ctx) instanceof String) return String.class; + else if(parse(ctx) instanceof Boolean) return Boolean.class; else return Void.class; } @@ -338,11 +338,7 @@ public abstract class ValueNode { if(!isArray(ctx)){ return UNDEFINED; } else { - Collection nodes = new ArrayList(); - for (Object value : ctx.configuration().jsonProvider().toIterable(parse(ctx))) { - nodes.add(value); - } - return new ValueListNode(nodes); + return new ValueListNode(Collections.unmodifiableList((List) parse(ctx))); } } @@ -363,19 +359,20 @@ public abstract class ValueNode { } public boolean isArray(Predicate.PredicateContext ctx) { - return ctx.configuration().jsonProvider().isArray(parse(ctx)); + return parse(ctx) instanceof List; } public boolean isMap(Predicate.PredicateContext ctx) { - return ctx.configuration().jsonProvider().isArray(parse(ctx)); + return parse(ctx) instanceof Map; } public int length(Predicate.PredicateContext ctx) { - return isArray(ctx) ? ctx.configuration().jsonProvider().length(parse(ctx)) : -1; + return isArray(ctx) ? ((List) parse(ctx)).size() : -1; } public boolean isEmpty(Predicate.PredicateContext ctx) { - if(isArray(ctx) || isMap(ctx)) return ctx.configuration().jsonProvider().length(parse(ctx)) == 0; + if (isArray(ctx)) return ((List) parse(ctx)).size() == 0; + else if (isMap(ctx)) return ((Map) parse(ctx)).size() == 0; else if((parse(ctx) instanceof String)) return ((String)parse(ctx)).length() == 0; return true; } @@ -850,4 +847,4 @@ public abstract class ValueNode { } -} \ No newline at end of file +} From aba863021c1ba59234ac0b239e1fce707b3de9a3 Mon Sep 17 00:00:00 2001 From: jochenberger Date: Wed, 22 Mar 2017 08:50:23 +0100 Subject: [PATCH 3/6] Don't mark json-smart as optional --- json-path/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json-path/build.gradle b/json-path/build.gradle index fa79910b..18f875ac 100644 --- a/json-path/build.gradle +++ b/json-path/build.gradle @@ -9,7 +9,7 @@ jar { baseName 'json-path' manifest { attributes 'Implementation-Title': 'json-path', 'Implementation-Version': version - instruction 'Import-Package', 'org.json.*;resolution:=optional', 'com.google.gson.*;resolution:=optional', 'com.fasterxml.jackson.*;resolution:=optional', 'net.minidev.json.*;resolution:=optional', 'org.apache.tapestry5.json.*;resolution:=optional', '*' + instruction 'Import-Package', 'org.json.*;resolution:=optional', 'com.google.gson.*;resolution:=optional', 'com.fasterxml.jackson.*;resolution:=optional', 'org.apache.tapestry5.json.*;resolution:=optional', '*' } } From 81d64ed2ae47cb88b5b81cce8f30ea40ae551390 Mon Sep 17 00:00:00 2001 From: jochenberger Date: Thu, 23 Mar 2017 15:22:13 +0100 Subject: [PATCH 4/6] Use json-smart to detect whether the String is JSON --- .../java/com/jayway/jsonpath/internal/filter/ValueNode.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java index 21c3fe24..f91b3890 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java @@ -30,7 +30,6 @@ import java.util.regex.Pattern; public abstract class ValueNode { public static final NullNode NULL_NODE = new NullNode(); - public static final BooleanNode TRUE = new BooleanNode("true"); public static final BooleanNode FALSE = new BooleanNode("false"); public static final UndefinedNode UNDEFINED = new UndefinedNode(); @@ -157,7 +156,7 @@ public abstract class ValueNode { char c1 = str.charAt(str.length() - 1); if ((c0 == '[' && c1 == ']') || (c0 == '{' && c1 == '}')){ try { - Configuration.defaultConfiguration().jsonProvider().parse(str); + new JSONParser(JSONParser.MODE_PERMISSIVE).parse(str); return false; } catch(Exception e){ return false; From 7ac3039965b27dc5c7dee500be8d38909786f793 Mon Sep 17 00:00:00 2001 From: jochenberger Date: Thu, 23 Mar 2017 15:37:11 +0100 Subject: [PATCH 5/6] Restore accidentally removed line --- .../main/java/com/jayway/jsonpath/internal/filter/ValueNode.java | 1 + 1 file changed, 1 insertion(+) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java index f91b3890..6a8a4a8e 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java @@ -30,6 +30,7 @@ import java.util.regex.Pattern; public abstract class ValueNode { public static final NullNode NULL_NODE = new NullNode(); + public static final BooleanNode TRUE = new BooleanNode("true"); public static final BooleanNode FALSE = new BooleanNode("false"); public static final UndefinedNode UNDEFINED = new UndefinedNode(); From 98debbb67bec02884a94ccd72cb18b3d2c07615e Mon Sep 17 00:00:00 2001 From: Jochen Berger Date: Tue, 28 Mar 2017 10:01:42 +0200 Subject: [PATCH 6/6] adapt test --- .../java/com/jayway/jsonpath/ProviderInTest.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/json-path/src/test/java/com/jayway/jsonpath/ProviderInTest.java b/json-path/src/test/java/com/jayway/jsonpath/ProviderInTest.java index 0508539c..ca9bd716 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/ProviderInTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/ProviderInTest.java @@ -12,9 +12,7 @@ import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider; import com.jayway.jsonpath.spi.mapper.JsonOrgMappingProvider; import com.jayway.jsonpath.spi.mapper.JsonSmartMappingProvider; import org.assertj.core.util.Lists; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import java.util.List; @@ -32,9 +30,6 @@ public class ProviderInTest { private final String SINGLE_QUOTES_IN_FILTER = String.format(IN_FILTER, SINGLE_QUOTES); - @Rule - public final ExpectedException exception = ExpectedException.none(); - @Test public void testJsonPathQuotesJackson() throws Exception { final Configuration jackson = Configuration.builder().jsonProvider(new JacksonJsonProvider()).mappingProvider(new JacksonMappingProvider()).build(); @@ -49,8 +44,8 @@ public class ProviderInTest { final List doubleQuoteInResult = ctx.read(DOUBLE_QUOTES_IN_FILTER); assertEquals(doubleQuoteInResult, doubleQuoteEqualsResult); - exception.expect(InvalidJsonException.class); - ctx.read(SINGLE_QUOTES_IN_FILTER); + final List singleQuoteInResult = ctx.read(SINGLE_QUOTES_IN_FILTER); + assertEquals(doubleQuoteInResult, singleQuoteInResult); } @@ -68,8 +63,8 @@ public class ProviderInTest { final ArrayNode doubleQuoteInResult = ctx.read(DOUBLE_QUOTES_IN_FILTER); assertEquals(doubleQuoteInResult, doubleQuoteEqualsResult); - exception.expect(InvalidJsonException.class); - ctx.read(SINGLE_QUOTES_IN_FILTER); + final ArrayNode singleQuoteInResult = ctx.read(SINGLE_QUOTES_IN_FILTER); + assertEquals(doubleQuoteInResult, singleQuoteInResult); } @Test