diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java index c9e425fd..eb700a26 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java +++ b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java @@ -8,8 +8,8 @@ package com.jayway.jsonpath.reader.filter; */ public class FilterFactory { - private final static Filter DOCUMENT_FILTER = new PassThrewFilter("$", false); - private final static Filter ALL_ARRAY_ITEMS_FILTER = new PassThrewFilter("[*]", true); + private final static Filter DOCUMENT_FILTER = new PassThroughFilter("$", false); + private final static Filter ALL_ARRAY_ITEMS_FILTER = new PassThroughFilter("[*]", true); private final static Filter WILDCARD_FILTER = new WildcardFilter("*"); private final static Filter SCAN_FILTER = new ScanFilter(".."); @@ -23,7 +23,7 @@ public class FilterFactory { return ALL_ARRAY_ITEMS_FILTER; - } else if ("*".equals(pathFragment)) { + } else if ("*".equals(pathFragment) || "['*']".equals(pathFragment)) { return WILDCARD_FILTER; diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassThrewFilter.java b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassthroughFilter.java similarity index 80% rename from json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassThrewFilter.java rename to json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassthroughFilter.java index 9dc5782d..500f5e9f 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassThrewFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassthroughFilter.java @@ -8,11 +8,11 @@ import com.jayway.jsonpath.spi.JsonProvider; * Date: 11/4/11 * Time: 10:15 PM */ -public class PassThrewFilter extends Filter { +public class PassThroughFilter extends Filter { private boolean isArrayFilter; - public PassThrewFilter(String condition, boolean isArrayFilter) { + public PassThroughFilter(String condition, boolean isArrayFilter) { super(condition); this.isArrayFilter = isArrayFilter; } diff --git a/json-path/src/test/java/com/jayway/jsonpath/ComplianceTests.java b/json-path/src/test/java/com/jayway/jsonpath/ComplianceTests.java new file mode 100644 index 00000000..65520c5b --- /dev/null +++ b/json-path/src/test/java/com/jayway/jsonpath/ComplianceTests.java @@ -0,0 +1,211 @@ +package com.jayway.jsonpath; + +import org.hamcrest.Matchers; +import org.junit.Test; + +import java.util.List; +import java.util.Map; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertThat; + +/** + * test defined in http://jsonpath.googlecode.com/svn/trunk/tests/jsonpath-test-js.html + */ +public class ComplianceTests { + + + + @Test + public void test_one() throws Exception { + + String json = "{ a: \"a\",\n" + + " b: \"b\",\n" + + " \"c d\": \"e\" \n" + + " }"; + + assertThat(JsonPath.read(json, "$.a"), is(equalTo("a"))); + assertThat(JsonPath.>read(json, "$.*"), hasItems("a", "b", "e")); + assertThat(JsonPath.>read(json, "$['*']"), hasItems("a", "b", "e")); + + //assertThat(JsonPath.read(json, "$['a']"), is(equalTo("a"))); //high + //assertThat(JsonPath.read(json, "$.'c d'"), is(equalTo("e"))); //low + //assertThat(JsonPath.>read(json, "$[*]"), hasItems("a", "b", "e")); //low + + } + + @Test + public void test_two() throws Exception { + String json = "[ 1, \"2\", 3.14, true, null ]"; + + assertThat(JsonPath.read(json, "$[0]"), is(equalTo(1))); + assertThat(JsonPath.read(json, "$[4]"), is(equalTo(null))); + assertThat(JsonPath.>read(json, "$[*]"), hasItems((Comparable)new Integer(1), (Comparable)new String("2"), (Comparable)new Double(3.14), (Comparable)new Boolean(true), null)); + assertThat(JsonPath.read(json, "$[-1:]"), is(equalTo(null))); + } + + @Test + public void test_three() throws Exception { + String json = "{ points: [\n" + + " { id: \"i1\", x: 4, y: -5 },\n" + + " { id: \"i2\", x: -2, y: 2, z: 1 },\n" + + " { id: \"i3\", x: 8, y: 3 },\n" + + " { id: \"i4\", x: -6, y: -1 },\n" + + " { id: \"i5\", x: 0, y: 2, z: 1 },\n" + + " { id: \"i6\", x: 1, y: 4 }\n" + + " ]\n" + + " }"; + + assertThat(JsonPath.>read(json, "$.points[1]"), allOf( + Matchers.hasEntry("id", "i2"), + Matchers.hasEntry("x", -2), + Matchers.hasEntry("y", 2), + Matchers.hasEntry("z", 1) + )); + + assertThat(JsonPath.read(json, "$.points[4].x"), equalTo(0)); + + assertThat(JsonPath.>read(json, "$.points[?(@.id = 'i4')].x"), hasItem(-6)); + + assertThat(JsonPath.>read(json, "$.points[*].x"), hasItems(4, -2, 8, -6, 0, 1)); + + assertThat(JsonPath.>read(json, "$.points[?(@.z)].id"), hasItems("i2", "i5")); + + assertThat(JsonPath.read(json, "$.points[(@.length-1)].id"), equalTo("i6")); + + //assertThat(JsonPath.>read(json, "$['points'][?(@.x * @.x + @.y * @.y > 50)].id"), hasItems(?)); //low + } + + @Test + public void test_four() throws Exception { + + } + + + + /* + --one + { "o": { a: "a", + b: "b", + "c d": "e" + }, + "p": [ "$.a", + "$['a']", + "$.'c d'", + "$.*", + "$['*']" , + "$[*]" + ] + }, + --two + { "o": [ 1, "2", 3.14, true, null ], + "p": [ "$[0]", + "$[4]", + "$[*]", + "$[-1:]" + ] + }, + --three + { "o": { points: [ + { id: "i1", x: 4, y: -5 }, + { id: "i2", x: -2, y: 2, z: 1 }, + { id: "i3", x: 8, y: 3 }, + { id: "i4", x: -6, y: -1 }, + { id: "i5", x: 0, y: 2, z: 1 }, + { id: "i6", x: 1, y: 4 } + ] + }, + "p": [ "$.points[1]", + "$.points[4].x", + "$.points[?(@.id=='i4')].x", + "$.points[*].x", + "$['points'][?(@.x*@.x+@.y*@.y > 50)].id", + "$.points[?(@.z)].id", + "$.points[(@.length-1)].id" + ] + }, + --four + { "o": { "menu": { + "header": "SVG Viewer", + "items": [ + {"id": "Open"}, + {"id": "OpenNew", "label": "Open New"}, + null, + {"id": "ZoomIn", "label": "Zoom In"}, + {"id": "ZoomOut", "label": "Zoom Out"}, + {"id": "OriginalView", "label": "Original View"}, + null, + {"id": "Quality"}, + {"id": "Pause"}, + {"id": "Mute"}, + null, + {"id": "Find", "label": "Find..."}, + {"id": "FindAgain", "label": "Find Again"}, + {"id": "Copy"}, + {"id": "CopyAgain", "label": "Copy Again"}, + {"id": "CopySVG", "label": "Copy SVG"}, + {"id": "ViewSVG", "label": "View SVG"}, + {"id": "ViewSource", "label": "View Source"}, + {"id": "SaveAs", "label": "Save As"}, + null, + {"id": "Help"}, + {"id": "About", "label": "About Adobe CVG Viewer..."} + ] + } + }, + "p": [ "$.menu.items[?(@ && @.id && !@.label)].id", + "$.menu.items[?(@ && @.label && /SVG/.test(@.label))].id", + "$.menu.items[?(!@)]", + "$..[0]" + ] + }, + --five + { "o": { a: [1,2,3,4], + b: [5,6,7,8] + }, + "p": [ "$..[0]", + "$..[-1:]", + "$..[?(@%2==0)]" + ] + }, + { "o": { lin: {color:"red", x:2, y:3}, + cir: {color:"blue", x:5, y:2, r:1 }, + arc: {color:"green", x:2, y:4, r:2, phi0:30, dphi:120 }, + pnt: {x:0, y:7 } + }, + "p": [ "$.'?(@.color)'.x", + "$['lin','cir'].color" + ] + }, + { "o": { lin: {color:"red", x:2, y:3}, + cir: {color:"blue", x:5, y:2, r:1 }, + arc: {color:"green", x:2, y:4, r:2, phi0:30, dphi:120 }, + pnt: {x:0, y:7 } + }, + "p": [ "$.'?(@.color)'.x", + "$['lin','arc'].color" + ] + }, + { "o": { text: [ "hello", "world2.0"] }, + "p": [ "$.text[?(@.length > 5)]", + "$.text[?(@.charAt(0) == 'h')]" + ] + }, + { "o": { a: { a:2, b:3 }, + b: { a:4, b:5 }, + c: { a: { a:6, b:7}, c:8} + }, + "p": [ "$..a" + ] + }, + { "o": { a: [ { a:5, '@':2, '$':3 }, // issue 7: resolved by escaping the '@' character + { a:6, '@':3, '$':4 }, // in a JSONPath expression. + { a:7, '@':4, '$':5 } + ] + }, + "p": [ "$.a[?(@['\\@']==3)]", + "$.a[?(@['$']==5)]" + ] + } + */ +} diff --git a/json-path/src/test/java/com/jayway/jsonpath/Issues.java b/json-path/src/test/java/com/jayway/jsonpath/IssueTests.java similarity index 95% rename from json-path/src/test/java/com/jayway/jsonpath/Issues.java rename to json-path/src/test/java/com/jayway/jsonpath/IssueTests.java index f2415b2b..ed1f43c6 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/Issues.java +++ b/json-path/src/test/java/com/jayway/jsonpath/IssueTests.java @@ -10,7 +10,7 @@ import static junit.framework.Assert.assertNull; * Date: 2/29/12 * Time: 8:42 AM */ -public class Issues { +public class IssueTests { @Test public void issue_7() throws Exception {