Skip to content

allowing single quotes in 'in' filter with Jackson JSON provider #305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jayway.jsonpath.spi.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
Expand All @@ -20,7 +21,13 @@

public class JacksonJsonNodeJsonProvider extends AbstractJsonProvider {

private static final ObjectMapper defaultObjectMapper = new ObjectMapper();
private static final ObjectMapper defaultObjectMapper = createDefaultObjectMapper();

private static ObjectMapper createDefaultObjectMapper() {
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
return mapper;
}

protected ObjectMapper objectMapper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.jayway.jsonpath.spi.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.jayway.jsonpath.InvalidJsonException;
Expand All @@ -29,7 +30,14 @@

public class JacksonJsonProvider extends AbstractJsonProvider {

private static final ObjectMapper defaultObjectMapper = new ObjectMapper();
private static final ObjectMapper defaultObjectMapper = createDefaultObjectMapper();

private static ObjectMapper createDefaultObjectMapper() {
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
return mapper;
}

private static final ObjectReader defaultObjectReader = defaultObjectMapper.reader().withType(Object.class);

protected ObjectMapper objectMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void always_return_same_object() { // Test because of Bug #211
context.put("$", "child", child1);
ObjectNode node2 = context.read("$");
ObjectNode child2 = context.read("$.child");

assertThat(node1).isSameAs(node2);
assertThat(child1).isSameAs(child2);
}
Expand Down Expand Up @@ -110,6 +110,15 @@ public void test_type_ref_fail() throws IOException {
using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON).read("$", typeRef);
}

@Test
public void jackson_json_allows_single_quotes() {
final String jsonArray = "[{\"foo\": \"bar\"}, {\"foo\": \"baz\"}]";
final Object readFromSingleQuote = using(JACKSON_JSON_NODE_CONFIGURATION).parse(jsonArray).read("$.[?(@.foo in ['bar'])].foo");
final Object readFromDoubleQuote = using(JACKSON_JSON_NODE_CONFIGURATION).parse(jsonArray).read("$.[?(@.foo in [\"bar\"])].foo");
assertThat(readFromSingleQuote).isEqualTo(readFromDoubleQuote);

}

public static class FooBarBaz<T> {
public T gen;
public String foo;
Expand Down
8 changes: 8 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,12 @@ public void jackson_converts_dates() {
assertThat(date).isEqualTo(now);
}

@Test
public void jackson_allows_single_quotes() {
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);

}
}