Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion json-path/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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', 'org.codehaus.jettison.*;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', 'org.codehaus.jettison.*;resolution:=optional', '*'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
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;

Expand Down Expand Up @@ -153,7 +157,7 @@ private static boolean isJson(Object o) {
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 true;
} catch(Exception e){
return false;
Expand Down Expand Up @@ -314,11 +318,11 @@ public JsonNode(Object parsedJson) {

@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;
}

Expand All @@ -334,16 +338,16 @@ public ValueNode asValueListNode(Predicate.PredicateContext ctx){
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)));
}
}

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() {
Expand All @@ -355,19 +359,20 @@ public Object getJson() {
}

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;
}
Expand Down
10 changes: 10 additions & 0 deletions json-path/src/test/java/com/jayway/jsonpath/JacksonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,14 @@ public void jackson_converts_dates() {
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);

}

}
13 changes: 4 additions & 9 deletions json-path/src/test/java/com/jayway/jsonpath/ProviderInTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
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;

Expand All @@ -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();
Expand All @@ -49,8 +44,8 @@ public void testJsonPathQuotesJackson() throws Exception {
final List<String> doubleQuoteInResult = ctx.read(DOUBLE_QUOTES_IN_FILTER);
assertEquals(doubleQuoteInResult, doubleQuoteEqualsResult);

exception.expect(InvalidJsonException.class);
ctx.read(SINGLE_QUOTES_IN_FILTER);
final List<String> singleQuoteInResult = ctx.read(SINGLE_QUOTES_IN_FILTER);
assertEquals(doubleQuoteInResult, singleQuoteInResult);
}


Expand All @@ -68,8 +63,8 @@ public void testJsonPathQuotesJacksonJsonNode() throws Exception {
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
Expand Down