Browse Source

Merge

pull/1/head
U-SWNA\dbaldwin100 14 years ago
parent
commit
ae9faa0600
  1. 5
      changelog.txt
  2. 6
      json-path-assert/pom.xml
  3. 4
      json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java
  4. 200
      json-path/pom.xml
  5. 29
      json-path/src/main/java/com/jayway/jsonpath/PathUtil.java
  6. 19
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java
  7. 13
      json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java
  8. 2
      pom.xml

5
changelog.txt

@ -1,6 +1,11 @@
------------------------------------------ ------------------------------------------
0.5.5 0.5.5
------------------------------------------ ------------------------------------------
- Improved JSON bracket notation. $['store']['book'][*]['author name']
properties can also contain '.' eg $['store']['book'][*]['author.name']
------------------------------------------
0.5.4
------------------------------------------
- Replaced com.googlecode.json-simple with net.minidev.json-smart - Replaced com.googlecode.json-simple with net.minidev.json-smart
- Introduced different parse modes, JsonPath.SLACK_MODE and JsonPath.STRICT_MODE (the slack mode lets you use single quotes or even no quotes at all) - Introduced different parse modes, JsonPath.SLACK_MODE and JsonPath.STRICT_MODE (the slack mode lets you use single quotes or even no quotes at all)

6
json-path-assert/pom.xml

@ -19,13 +19,13 @@
<parent> <parent>
<artifactId>json-path-parent</artifactId> <artifactId>json-path-parent</artifactId>
<groupId>com.jayway.jsonpath</groupId> <groupId>com.jayway.jsonpath</groupId>
<version>0.5.5-SNAPSHOT</version> <version>0.5.6-SNAPSHOT</version>
</parent> </parent>
<groupId>com.jayway.jsonpath</groupId> <groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId> <artifactId>json-path-assert</artifactId>
<version>0.5.5-SNAPSHOT</version> <version>0.5.6-SNAPSHOT</version>
<name>json-path-assert</name> <name>json-path-assert</name>
<url>http://maven.apache.org</url> <url>http://code.google.com/p/json-path/</url>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.jayway.jsonpath</groupId> <groupId>com.jayway.jsonpath</groupId>

4
json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java

@ -11,7 +11,6 @@ import com.jayway.jsonpath.json.JsonException;
import com.jayway.jsonpath.json.JsonFactory; import com.jayway.jsonpath.json.JsonFactory;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import static com.jayway.jsonassert.JsonAssert.*; import static com.jayway.jsonassert.JsonAssert.*;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
@ -161,6 +160,9 @@ public abstract class JsonAssertTest {
with(JSON).assertEquals("$.store.book[0].title", "Sayings of the Century") with(JSON).assertEquals("$.store.book[0].title", "Sayings of the Century")
.assertThat("$.store.book[0].title", equalTo("Sayings of the Century")); .assertThat("$.store.book[0].title", equalTo("Sayings of the Century"));
with(JSON).assertEquals("$['store']['book'][0].['title']", "Sayings of the Century")
.assertThat("$['store'].book[0].title", equalTo("Sayings of the Century"));
} }
@Test @Test

200
json-path/pom.xml

@ -1,100 +1,100 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<!-- <!--
~ Copyright 2010 the original author or authors. ~ Copyright 2010 the original author or authors.
~ ~
~ Licensed under the Apache License, Version 2.0 (the "License"); ~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License. ~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at ~ You may obtain a copy of the License at
~ ~
~ http://www.apache.org/licenses/LICENSE-2.0 ~ http://www.apache.org/licenses/LICENSE-2.0
~ ~
~ Unless required by applicable law or agreed to in writing, software ~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS, ~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and ~ See the License for the specific language governing permissions and
~ limitations under the License. ~ limitations under the License.
--> -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>json-path-parent</artifactId> <groupId>com.jayway.jsonpath</groupId>
<groupId>com.jayway.jsonpath</groupId> <artifactId>json-path-parent</artifactId>
<version>0.5.5-SNAPSHOT</version> <version>0.5.6-SNAPSHOT</version>
</parent> </parent>
<groupId>com.jayway.jsonpath</groupId> <groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId> <artifactId>json-path</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>0.5.5-SNAPSHOT</version> <version>0.5.6-SNAPSHOT</version>
<name>json-path</name> <name>json-path</name>
<url>http://maven.apache.org</url> <url>http://code.google.com/p/json-path/</url>
<dependencies> <dependencies>
<!-- <!--
<dependency> <dependency>
<groupId>com.googlecode.json-simple</groupId> <groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId> <artifactId>json-simple</artifactId>
</dependency> </dependency>
--> -->
<dependency> <dependency>
<groupId>net.minidev</groupId> <groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId> <artifactId>json-smart</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId> <artifactId>commons-lang</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>1.6</version> <version>1.6</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.minidev</groupId> <groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId> <artifactId>json-smart</artifactId>
<version>1.0.6.3</version> <version>1.0.6.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<version>1.5.11</version> <version>1.5.11</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId> <artifactId>slf4j-log4j12</artifactId>
<version>1.5.11</version> <version>1.5.11</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<version>1.5.11</version> <version>1.5.11</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>log4j</groupId>
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<version>1.2.15</version> <version>1.2.15</version>
</dependency> </dependency>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- test dependencies --> <!-- test dependencies -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hamcrest</groupId> <groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId> <artifactId>hamcrest-library</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

29
json-path/src/main/java/com/jayway/jsonpath/PathUtil.java

@ -2,6 +2,7 @@ package com.jayway.jsonpath;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Scanner;
/** /**
* User: kalle stenflo * User: kalle stenflo
@ -12,18 +13,18 @@ public class PathUtil {
/** /**
* Checks if a path points to a single item or if it potentially returns multiple items * Checks if a path points to a single item or if it potentially returns multiple items
* * <p/>
* a path is considered <strong>not</strong> definite if it contains a scan fragment ".." * a path is considered <strong>not</strong> definite if it contains a scan fragment ".."
* or an array position fragment that is not based on a single index * or an array position fragment that is not based on a single index
* * <p/>
* * <p/>
* absolute path examples: * absolute path examples:
* * <p/>
* $store.book * $store.book
* $store.book[1].value * $store.book[1].value
* * <p/>
* not absolute path examples * not absolute path examples
* * <p/>
* $..book * $..book
* $.store.book[1,2] * $.store.book[1,2]
* $.store.book[?(@.category = 'fiction')] * $.store.book[?(@.category = 'fiction')]
@ -38,8 +39,8 @@ public class PathUtil {
/** /**
* Splits a path into fragments * Splits a path into fragments
* * <p/>
* the path <code>$.store.book[1].category</code> returns ["$", "store", "book", "[1]", "value"] * the path <code>$.store.book[1].category</code> returns ["$", "store", "book", "[1]", "category"]
* *
* @param jsonPath path to split * @param jsonPath path to split
* @return fragments * @return fragments
@ -52,7 +53,10 @@ public class PathUtil {
jsonPath = "$." + jsonPath; jsonPath = "$." + jsonPath;
} }
jsonPath = jsonPath.replace("..", ".~.") jsonPath = jsonPath.replace("$['","$.['")
.replaceAll("\\['(\\w+)\\.(\\w+)']", "['$1~$2']")
.replace("']['","'].['" )
.replace("..", ".~~.")
.replace("[", ".[") .replace("[", ".[")
.replace("@.", "@") .replace("@.", "@")
.replace("['", "") .replace("['", "")
@ -64,8 +68,13 @@ public class PathUtil {
if (split[i].trim().isEmpty()) { if (split[i].trim().isEmpty()) {
continue; continue;
} }
fragments.add(split[i].replace("@", "@.").replace("~", "..")); fragments.add(split[i].replace("@", "@.").replace("~", "."));
} }
//for (String fragment : fragments) {
// System.out.println(fragment);
//}
return fragments; return fragments;
} }
} }

19
json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

@ -14,6 +14,7 @@ import com.jayway.jsonpath.json.JsonObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Scanner;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.hasItems;
@ -58,7 +59,8 @@ public abstract class JsonPathTest {
" \"bicycle\": {\n" + " \"bicycle\": {\n" +
" \"color\": \"red\",\n" + " \"color\": \"red\",\n" +
" \"price\": 19.95,\n" + " \"price\": 19.95,\n" +
" \"foo:bar\": \"fooBar\"\n" + " \"foo:bar\": \"fooBar\",\n" +
" \"dot.notation\": \"new\"\n" +
" }\n" + " }\n" +
" }\n" + " }\n" +
"}"; "}";
@ -66,8 +68,16 @@ public abstract class JsonPathTest {
@Before @Before
public void init_factory(){ public void init_factory(){
@Test
public void bracket_notation_can_be_used_in_path() throws Exception {
} }
assertEquals("new", JsonPath.read(DOCUMENT, "$.['store'].bicycle.['dot.notation']"));
assertEquals("new", JsonPath.read(DOCUMENT, "$['store']['bicycle']['dot.notation']"));
assertEquals("new", JsonPath.read(DOCUMENT, "$.['store']['bicycle']['dot.notation']"));
assertEquals("new", JsonPath.read(DOCUMENT, "$.['store'].['bicycle'].['dot.notation']"));
}
@Test @Test
public void filter_an_array() throws Exception { public void filter_an_array() throws Exception {
JsonElement matches = JsonPath.read(ARRAY, "$.[?(@.value = 1)]"); JsonElement matches = JsonPath.read(ARRAY, "$.[?(@.value = 1)]");
@ -79,8 +89,12 @@ public abstract class JsonPathTest {
@Test @Test
public void read_path_with_colon() throws Exception { public void read_path_with_colon() throws Exception {
assertEquals(JsonPath.read(DOCUMENT, "$.store.bicycle.foo:bar"), "fooBar");
assertEquals(JsonPath.read(DOCUMENT, "$.['store'].['bicycle'].['foo:bar']"), "fooBar");
assertEquals(JsonPath.read(DOCUMENT, "$.store.bicycle.foo:bar").toObject(), "fooBar"); assertEquals(JsonPath.read(DOCUMENT, "$.store.bicycle.foo:bar").toObject(), "fooBar");
assertEquals(JsonPath.read(DOCUMENT, "$.['store'].['bicycle'].['foo:bar']").toObject(), "fooBar"); assertEquals(JsonPath.read(DOCUMENT, "$.['store'].['bicycle'].['foo:bar']").toObject(), "fooBar");
assertEquals(JsonPath.read(DOCUMENT, "$.store.bicycle.foo:bar"), "fooBar");
assertEquals(JsonPath.read(DOCUMENT, "$['store']['bicycle']['foo:bar']"), "fooBar");
} }
@ -270,7 +284,9 @@ public abstract class JsonPathTest {
JsonElement res2 = JsonPath.read(DOCUMENT, "$.store.book[1, 200].author"); JsonElement res2 = JsonPath.read(DOCUMENT, "$.store.book[1, 200].author");
assertThat((List<String>)res, hasItems("Evelyn Waugh"));
assertEquals(res2.toObject(), "Evelyn Waugh"); assertEquals(res2.toObject(), "Evelyn Waugh");
assertThat((List<String>) res, hasItems("Evelyn Waugh"));
//assertNull((); //assertNull(();
} }
@ -285,4 +301,5 @@ public abstract class JsonPathTest {
public void invalid_new_path_throws_exception() throws Exception { public void invalid_new_path_throws_exception() throws Exception {
JsonPath.read(DOCUMENT, "new "); JsonPath.read(DOCUMENT, "new ");
} }
} }

13
json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java

@ -30,6 +30,18 @@ public class SplitPathFragmentsTest {
*/ */
@Test
public void bracket_notation_can_be_split() throws Exception {
assertThat(PathUtil.splitPath("$.['store'].['price']"), hasItems("$", "store", "price"));
assertThat(PathUtil.splitPath("$['store']['price']"), hasItems("$", "store", "price"));
assertThat(PathUtil.splitPath("['store']['price']"), hasItems("$", "store", "price"));
assertThat(PathUtil.splitPath("['store'].price"), hasItems("$", "store", "price"));
assertThat(PathUtil.splitPath("$.['store book'].['price list']"), hasItems("$", "store book", "price list"));
assertThat(PathUtil.splitPath("$.['store.book'].['price.list']"), hasItems("$", "store.book", "price.list"));
}
@Test @Test
public void fragments_are_split_correctly() throws Exception { public void fragments_are_split_correctly() throws Exception {
@ -62,6 +74,7 @@ public class SplitPathFragmentsTest {
assertThat(PathUtil.splitPath("$.[0].[1].author"), hasItems("$", "[0]", "[1]", "author")); assertThat(PathUtil.splitPath("$.[0].[1].author"), hasItems("$", "[0]", "[1]", "author"));
assertThat(PathUtil.splitPath("$.foo:bar.author"), hasItems("$", "foo:bar", "author")); assertThat(PathUtil.splitPath("$.foo:bar.author"), hasItems("$", "foo:bar", "author"));
} }

2
pom.xml

@ -24,7 +24,7 @@
<groupId>com.jayway.jsonpath</groupId> <groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-parent</artifactId> <artifactId>json-path-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>0.5.5-SNAPSHOT</version> <version>0.5.6-SNAPSHOT</version>
<url>http://code.google.com/p/json-path</url> <url>http://code.google.com/p/json-path</url>
<name>json-path-parent-pom</name> <name>json-path-parent-pom</name>
<description>Java JsonPath implementation</description> <description>Java JsonPath implementation</description>

Loading…
Cancel
Save