Browse Source

Fixed issues.

pull/29/head
Kalle Stenflo 11 years ago
parent
commit
a875a730f3
  1. 7
      README
  2. 1
      json-path/pom.xml
  3. 48
      json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java
  4. 9
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java
  5. 74
      json-path/src/test/java/com/jayway/jsonpath/MultiAttributeTest.java

7
README

@ -1,11 +1,16 @@
Java DSL for reading and testing JSON documents.
------------------------------------------
0.8.2 2013-08-19
0.9 2013-08-19
------------------------------------------
- OSGI ready
- Bug fixes
- Performance improvements
- select multiple attributes
List<Map<String, Object>> matches = JsonPath.read(DOCUMENT, "$.store.book[*].['category', 'title']");
Map<String, Object> match = JsonPath.read(DOCUMENT, "$.store.bicycle['color', 'display-price']");
------------------------------------------

1
json-path/pom.xml

@ -44,6 +44,7 @@
<optional>true</optional>
</dependency>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- test dependencies -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

48
json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java

@ -1,12 +1,14 @@
package com.jayway.jsonpath;
import org.hamcrest.Matchers;
import org.junit.Test;
import java.util.List;
import java.util.regex.Matcher;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertThat;
/**
* Created by IntelliJ IDEA.
@ -28,6 +30,48 @@ public class IssuesTest {
assertTrue(result.isEmpty());
}
@Test
public void issue_15() throws Exception {
String json = "{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Herman Melville\",\n" +
" \"title\": \"Moby Dick\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"price\": 8.99,\n" +
" \"retailer\": null, \n" +
" \"children\": true,\n" +
" \"number\": -2.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"title\": \"The Lord of the Rings\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"price\": 22.99,\n" +
" \"number\":0,\n" +
" \"children\": false\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
List<String> titles = JsonPath.read(json, "$.store.book[?(@.children==true)].title");
assertThat(titles, Matchers.contains("Moby Dick"));
assertEquals(1, titles.size());
}
@Test
public void issue_22() throws Exception {
String json = "{\"a\":[{\"b\":1,\"c\":2},{\"b\":5,\"c\":2}]}";
System.out.println(JsonPath.read(json, "a[?(@.b==5)].d"));
}
@Test
public void issue_29_b() throws Exception {
@ -37,4 +81,6 @@ public class IssuesTest {
assertTrue(result.size() == 1);
}
}

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

@ -1,5 +1,6 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.spi.JsonProviderFactory;
import com.jayway.jsonpath.util.ScriptEngineJsonPath;
import org.junit.Test;
@ -18,6 +19,11 @@ import static org.junit.Assert.*;
*/
public class JsonPathTest {
static {
//JsonProviderFactory.setDefaultProvider(JacksonProvider.class);
}
public final static String ARRAY = "[{\"value\": 1},{\"value\": 2}, {\"value\": 3},{\"value\": 4}]";
public final static String DOCUMENT =
@ -72,6 +78,9 @@ public class JsonPathTest {
private final static String ARRAY_EXPAND = "[{\"parent\": \"ONE\", \"child\": {\"name\": \"NAME_ONE\"}}, [{\"parent\": \"TWO\", \"child\": {\"name\": \"NAME_TWO\"}}]]";
@Test
public void array_start_expands() throws Exception {
//assertThat(JsonPath.<List<String>>read(ARRAY_EXPAND, "$[?(@.parent = 'ONE')].child.name"), hasItems("NAME_ONE"));

74
json-path/src/test/java/com/jayway/jsonpath/MultiAttributeTest.java

@ -0,0 +1,74 @@
package com.jayway.jsonpath;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
/**
* User: kalle
* Date: 8/20/13
* Time: 8:03 AM
*/
public class MultiAttributeTest {
public final static String DOCUMENT =
"{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"display-price\": 8.95\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"display-price\": 12.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Herman Melville\",\n" +
" \"title\": \"Moby Dick\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"display-price\": 8.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"title\": \"The Lord of the Rings\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"display-price\": 22.99\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"display-price\": 19.95,\n" +
" \"foo:bar\": \"fooBar\",\n" +
" \"dot.notation\": \"new\",\n" +
" \"dash-notation\": \"dashes\"\n" +
" }\n" +
" }\n" +
"}";
@Test
public void multiple_attributes_from_array_content(){
List<Map<String, Object>> matches = JsonPath.read(DOCUMENT, "$.store.book[*].['category', 'title']");
assertEquals(4, matches.size());
assertTrue(matches.get(1).containsKey("category"));
assertTrue(matches.get(1).containsKey("title"));
assertEquals(2, matches.get(1).size());
}
@Test
public void multiple_attributes_from_single_object(){
Map<String, Object> match = JsonPath.read(DOCUMENT, "$.store.bicycle['color', 'display-price']");
assertTrue(match.containsKey("color"));
assertTrue(match.containsKey("display-price"));
}
}
Loading…
Cancel
Save