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
------------------------------------------
- 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
- 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>
<artifactId>json-path-parent</artifactId>
<groupId>com.jayway.jsonpath</groupId>
<version>0.5.5-SNAPSHOT</version>
<version>0.5.6-SNAPSHOT</version>
</parent>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>0.5.5-SNAPSHOT</version>
<version>0.5.6-SNAPSHOT</version>
<name>json-path-assert</name>
<url>http://maven.apache.org</url>
<url>http://code.google.com/p/json-path/</url>
<dependencies>
<dependency>
<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 java.io.InputStream;
import java.io.InputStreamReader;
import static com.jayway.jsonassert.JsonAssert.*;
import static org.hamcrest.Matchers.*;
@ -161,6 +160,9 @@ public abstract class JsonAssertTest {
with(JSON).assertEquals("$.store.book[0].title", "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

200
json-path/pom.xml

@ -1,100 +1,100 @@
<?xml version="1.0"?>
<!--
~ Copyright 2010 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ 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">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>json-path-parent</artifactId>
<groupId>com.jayway.jsonpath</groupId>
<version>0.5.5-SNAPSHOT</version>
</parent>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<packaging>jar</packaging>
<version>0.5.5-SNAPSHOT</version>
<name>json-path</name>
<url>http://maven.apache.org</url>
<dependencies>
<!--
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
</dependency>
-->
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>1.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>1.0.6.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.11</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.11</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.11</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- test dependencies -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<?xml version="1.0"?>
<!--
~ Copyright 2010 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ 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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-parent</artifactId>
<version>0.5.6-SNAPSHOT</version>
</parent>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<packaging>jar</packaging>
<version>0.5.6-SNAPSHOT</version>
<name>json-path</name>
<url>http://code.google.com/p/json-path/</url>
<dependencies>
<!--
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
</dependency>
-->
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>1.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>1.0.6.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.11</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.11</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.11</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- test dependencies -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</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.List;
import java.util.Scanner;
/**
* 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
*
* <p/>
* 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
*
*
* <p/>
* <p/>
* absolute path examples:
*
* <p/>
* $store.book
* $store.book[1].value
*
* <p/>
* not absolute path examples
*
* <p/>
* $..book
* $.store.book[1,2]
* $.store.book[?(@.category = 'fiction')]
@ -38,8 +39,8 @@ public class PathUtil {
/**
* Splits a path into fragments
*
* the path <code>$.store.book[1].category</code> returns ["$", "store", "book", "[1]", "value"]
* <p/>
* the path <code>$.store.book[1].category</code> returns ["$", "store", "book", "[1]", "category"]
*
* @param jsonPath path to split
* @return fragments
@ -52,7 +53,10 @@ public class PathUtil {
jsonPath = "$." + jsonPath;
}
jsonPath = jsonPath.replace("..", ".~.")
jsonPath = jsonPath.replace("$['","$.['")
.replaceAll("\\['(\\w+)\\.(\\w+)']", "['$1~$2']")
.replace("']['","'].['" )
.replace("..", ".~~.")
.replace("[", ".[")
.replace("@.", "@")
.replace("['", "")
@ -64,8 +68,13 @@ public class PathUtil {
if (split[i].trim().isEmpty()) {
continue;
}
fragments.add(split[i].replace("@", "@.").replace("~", ".."));
fragments.add(split[i].replace("@", "@.").replace("~", "."));
}
//for (String fragment : fragments) {
// System.out.println(fragment);
//}
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.List;
import java.util.Map;
import java.util.Scanner;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems;
@ -58,7 +59,8 @@ public abstract class JsonPathTest {
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95,\n" +
" \"foo:bar\": \"fooBar\"\n" +
" \"foo:bar\": \"fooBar\",\n" +
" \"dot.notation\": \"new\"\n" +
" }\n" +
" }\n" +
"}";
@ -66,8 +68,16 @@ public abstract class JsonPathTest {
@Before
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
public void filter_an_array() throws Exception {
JsonElement matches = JsonPath.read(ARRAY, "$.[?(@.value = 1)]");
@ -79,8 +89,12 @@ public abstract class JsonPathTest {
@Test
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"), "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");
assertThat((List<String>)res, hasItems("Evelyn Waugh"));
assertEquals(res2.toObject(), "Evelyn Waugh");
assertThat((List<String>) res, hasItems("Evelyn Waugh"));
//assertNull(();
}
@ -285,4 +301,5 @@ public abstract class JsonPathTest {
public void invalid_new_path_throws_exception() throws Exception {
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
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("$.foo:bar.author"), hasItems("$", "foo:bar", "author"));
}

2
pom.xml

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

Loading…
Cancel
Save