From a875a730f3ce216cb4da11b482e57272ce7a9d4f Mon Sep 17 00:00:00 2001 From: Kalle Stenflo Date: Tue, 20 Aug 2013 08:52:10 +0200 Subject: [PATCH] Fixed issues. --- README | 7 +- json-path/pom.xml | 1 + .../java/com/jayway/jsonpath/IssuesTest.java | 48 +++++++++++- .../com/jayway/jsonpath/JsonPathTest.java | 9 +++ .../jayway/jsonpath/MultiAttributeTest.java | 74 +++++++++++++++++++ 5 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 json-path/src/test/java/com/jayway/jsonpath/MultiAttributeTest.java diff --git a/README b/README index 16de2abc..9c9f00c3 100644 --- a/README +++ b/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> matches = JsonPath.read(DOCUMENT, "$.store.book[*].['category', 'title']"); + Map match = JsonPath.read(DOCUMENT, "$.store.bicycle['color', 'display-price']"); + + ------------------------------------------ diff --git a/json-path/pom.xml b/json-path/pom.xml index 6e1edb9e..ec3f7fda 100644 --- a/json-path/pom.xml +++ b/json-path/pom.xml @@ -44,6 +44,7 @@ true + diff --git a/json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java b/json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java index 8465c348..fbf5b925 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java +++ b/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 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); } + + } diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java b/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java index e5e47a11..9f1fbb3c 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java +++ b/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.>read(ARRAY_EXPAND, "$[?(@.parent = 'ONE')].child.name"), hasItems("NAME_ONE")); diff --git a/json-path/src/test/java/com/jayway/jsonpath/MultiAttributeTest.java b/json-path/src/test/java/com/jayway/jsonpath/MultiAttributeTest.java new file mode 100644 index 00000000..f54558f4 --- /dev/null +++ b/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> 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 match = JsonPath.read(DOCUMENT, "$.store.bicycle['color', 'display-price']"); + + assertTrue(match.containsKey("color")); + assertTrue(match.containsKey("display-price")); + } + +}