Kalle Stenflo
10 years ago
11 changed files with 54 additions and 563 deletions
@ -1,78 +0,0 @@
|
||||
package com.jayway.jsonpath.old; |
||||
|
||||
import com.jayway.jsonpath.Option; |
||||
import com.jayway.jsonpath.spi.json.JsonProvider; |
||||
import com.jayway.jsonpath.spi.json.JsonProviderFactory; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.Set; |
||||
|
||||
public class DeepScanTest { |
||||
|
||||
private static final String DOCUMENT = "{\n" + |
||||
" \"store\":{\n" + |
||||
" \"book\":[\n" + |
||||
" {\n" + |
||||
" \"category\":\"reference\",\n" + |
||||
" \"author\":\"Nigel Rees\",\n" + |
||||
" \"title\":\"Sayings of the Century\",\n" + |
||||
" \"price\":8.95\n" + |
||||
" },\n" + |
||||
" {\n" + |
||||
" \"category\":\"fiction\",\n" + |
||||
" \"author\":\"Evelyn Waugh\",\n" + |
||||
" \"title\":\"Sword of Honour\",\n" + |
||||
" \"price\":12.99\n" + |
||||
" },\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" + |
||||
" }\n" + |
||||
" ],\n" + |
||||
" \"bicycle\":{\n" + |
||||
" \"color\":\"red\",\n" + |
||||
" \"price\":19.95\n" + |
||||
" }\n" + |
||||
" }\n" + |
||||
"}"; |
||||
|
||||
private static final JsonProvider prov = JsonProviderFactory.createProvider(); |
||||
private static final Set<Option> opts = Collections.emptySet(); |
||||
/* |
||||
@Test |
||||
public void correct_path() { |
||||
|
||||
System.out.println(DOCUMENT); |
||||
|
||||
System.out.println(PathEvaluator.evaluate("$.store..", DOCUMENT, prov, opts)); |
||||
} |
||||
|
||||
@Test |
||||
public void a_string_property_can_be_scanned_for() { |
||||
|
||||
PathEvaluationResult result = PathEvaluator.evaluate("$.store..category", DOCUMENT, prov, opts); |
||||
|
||||
assertThat(result.getPathList(), hasItems("$['store']['book'][0]['category']", "$['store']['book'][1]['category']", "$['store']['book'][2]['category']")); |
||||
|
||||
assertThat(result.getResultList(), hasItems( |
||||
Matchers.<Object>is("reference"), |
||||
Matchers.<Object>is("fiction"), |
||||
Matchers.<Object>is("fiction"))); |
||||
|
||||
System.out.println(result.toString()); |
||||
} |
||||
|
||||
@Test |
||||
public void a_path_can_end_with_deep_scan() { |
||||
|
||||
String json = "{\"items\":[1, 3, 5, 7, 8, 13, 20]}"; |
||||
|
||||
//System.out.println( JsonPath.read(json, "$.."));
|
||||
System.out.println(PathEvaluator.evaluate("$..", json, prov, opts).toString()); |
||||
|
||||
} |
||||
*/ |
||||
} |
@ -1,84 +0,0 @@
|
||||
package com.jayway.jsonpath.old; |
||||
|
||||
import com.jayway.jsonpath.JsonPath; |
||||
import org.junit.Test; |
||||
|
||||
public class DocumentationPageTests { |
||||
|
||||
|
||||
public static final String JSON = "{ \n" + |
||||
" \"store\": {\n" + |
||||
" \"book\": [ \n" + |
||||
" { \n" + |
||||
" \"category\": \"reference\",\n" + |
||||
" \"author\": \"Nigel Rees\",\n" + |
||||
" \"title\": \"Sayings of the Century\",\n" + |
||||
" \"price\": 8.95\n" + |
||||
" },\n" + |
||||
" { \n" + |
||||
" \"category\": \"fiction\",\n" + |
||||
" \"author\": \"Evelyn Waugh\",\n" + |
||||
" \"title\": \"Sword of Honour\",\n" + |
||||
" \"price\": 12.99\n" + |
||||
" },\n" + |
||||
" { \n" + |
||||
" \"category\": \"fiction\",\n" + |
||||
" \"author\": \"Herman Melville\",\n" + |
||||
" \"title\": \"Moby Dick\",\n" + |
||||
" \"isbn\": \"0-553-21311-3\",\n" + |
||||
" \"price\": 8.99\n" + |
||||
" },\n" + |
||||
" { \n" + |
||||
" \"category\": null,\n" + |
||||
" \"author\": \"J. R. R. Tolkien\",\n" + |
||||
" \"title\": \"The Lord of the Rings\",\n" + |
||||
" \"isbn\": \"0-395-19395-8\",\n" + |
||||
" \"price\": 22.99,\n" + |
||||
" \"edition\": {\n" + |
||||
" \"release-date\": \"2013-01-22\"\n" + |
||||
" }\n" + |
||||
" }\n" + |
||||
" ],\n" + |
||||
" \"bicycle\": {\n" + |
||||
" \"color\": \"red\",\n" + |
||||
" \"price\": 19.95\n" + |
||||
" }\n" + |
||||
" }\n" + |
||||
"}"; |
||||
|
||||
@Test |
||||
public void test_1() { |
||||
System.out.println(JsonPath.read(JSON, "$.store.bicycle")); |
||||
} |
||||
|
||||
@Test |
||||
public void test_2() { |
||||
System.out.println(JsonPath.read(JSON, "$.store.book[0]")); |
||||
} |
||||
|
||||
@Test |
||||
public void test_3() { |
||||
System.out.println(JsonPath.read(JSON, "$.store.book[*].author")); |
||||
} |
||||
|
||||
@Test |
||||
public void test_4() { |
||||
System.out.println(JsonPath.read(JSON, "$.store.book[-2:].author")); |
||||
} |
||||
|
||||
@Test |
||||
public void test_5() { |
||||
System.out.println(JsonPath.read(JSON, "$.store.book[1:3].author")); |
||||
} |
||||
|
||||
@Test |
||||
public void test_6() { |
||||
System.out.println(JsonPath.read(JSON, "$.store.book[0]['author', 'category']")); |
||||
} |
||||
|
||||
@Test |
||||
public void test_7() { |
||||
System.out.println(JsonPath.read(JSON, "$..price")); |
||||
} |
||||
|
||||
} |
@ -1,123 +0,0 @@
|
||||
package com.jayway.jsonpath.old; |
||||
|
||||
import com.jayway.jsonpath.JsonPath; |
||||
import org.junit.Test; |
||||
|
||||
import java.util.List; |
||||
|
||||
import static org.hamcrest.Matchers.hasItems; |
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertThat; |
||||
|
||||
public class HelpTest { |
||||
|
||||
public static final String JSON = "{\n" + |
||||
" \"destination_addresses\" : [\n" + |
||||
" \"Rua do Dom António José Cordeiro 48, 3800-012 Aveiro, Portugal\",\n" + |
||||
" \"Rua do Dom António José Cordeiro 48, 3800-012 Aveiro, Portugal\"\n" + |
||||
" ],\n" + |
||||
" \"origin_addresses\" : [ \"N109, 3800, Portugal\" ],\n" + |
||||
" \"rows\" : [\n" + |
||||
" {\n" + |
||||
" \"elements\" : [\n" + |
||||
" {\n" + |
||||
" \"distance\" : {\n" + |
||||
" \"text\" : \"0.4 km\",\n" + |
||||
" \"value\" : 427\n" + |
||||
" },\n" + |
||||
" \"duration\" : {\n" + |
||||
" \"text\" : \"1 min\",\n" + |
||||
" \"value\" : 58\n" + |
||||
" },\n" + |
||||
" \"status\" : \"OK\"\n" + |
||||
" },\n" + |
||||
" {\n" + |
||||
" \"distance\" : {\n" + |
||||
" \"text\" : \"5.0 km\",\n" + |
||||
" \"value\" : 427\n" + |
||||
" },\n" + |
||||
" \"duration\" : {\n" + |
||||
" \"text\" : \"1 min\",\n" + |
||||
" \"value\" : 58\n" + |
||||
" },\n" + |
||||
" \"status\" : \"OK\"\n" + |
||||
" }\n" + |
||||
" ]\n" + |
||||
" }\n" + |
||||
" ],\n" + |
||||
" \"status\" : \"OK\"\n" + |
||||
"}"; |
||||
|
||||
private static final String JSON2 = "{\n" + |
||||
" \"error\": null,\n" + |
||||
" \"contents\": [\n" + |
||||
" {\n" + |
||||
" \"groupType\": \"series\",\n" + |
||||
" \"instanceId\": \"grp://15\",\n" + |
||||
" \"id\": \"prg://16\",\n" + |
||||
" \"type\": \"group\",\n" + |
||||
" \"media\": [\n" + |
||||
" {\n" + |
||||
" \"classification\": \"urn:1.2.3\",\n" + |
||||
" \"uri\": \"http://yahoo.com/1.png\",\n" + |
||||
" \"mimeType\": \"application/octet-stream\"\n" + |
||||
" }\n" + |
||||
" ]\n" + |
||||
" },\n" + |
||||
" {\n" + |
||||
" \"groupType\": \"series\",\n" + |
||||
" \"instanceId\": \"grp://15\",\n" + |
||||
" \"extra\": 1,\n" + |
||||
" \"id\": \"prg://16\",\n" + |
||||
" \"type\": \"group\",\n" + |
||||
" \"media\": [\n" + |
||||
" {\n" + |
||||
" \"classification\": \"urn:1.2.3\",\n" + |
||||
" \"uri\": \"http://yahoo.com/1.png\",\n" + |
||||
" \"mimeType\": \"application/octet-stream\"\n" + |
||||
" }\n" + |
||||
" ]\n" + |
||||
" }\n" + |
||||
" ],\n" + |
||||
" \"header\": {\n" + |
||||
" \"total\": 1\n" + |
||||
" }\n" + |
||||
"}"; |
||||
|
||||
|
||||
@Test |
||||
public void sample_one_a(){ |
||||
System.out.println(JsonPath.read(JSON2, "$.['error', 'header']")); |
||||
System.out.println(JsonPath.read(JSON2, "$.contents[*].['groupType', 'type', 'extra']")); |
||||
} |
||||
|
||||
@Test |
||||
public void sample_one_b(){ |
||||
System.out.println(JsonPath.read(JSON2, "$.['error', 'header']")); |
||||
System.out.println(JsonPath.read(JSON2, "$.contents[*].['groupType', 'type']")); |
||||
} |
||||
|
||||
@Test |
||||
public void sample_one(){ |
||||
List<String> addresses = JsonPath.read(JSON, "$.destination_addresses[*]"); |
||||
assertThat(addresses, hasItems("Rua do Dom António José Cordeiro 48, 3800-012 Aveiro, Portugal")); |
||||
} |
||||
|
||||
@Test |
||||
public void sample_two(){ |
||||
String text = JsonPath.read(JSON, "$.rows[0].elements[1].distance.text"); |
||||
assertEquals("5.0 km", text); |
||||
} |
||||
|
||||
@Test |
||||
public void sample_two_b(){ |
||||
String text = JsonPath.read(JSON, "$.rows[0].elements[1].distance.text"); |
||||
assertEquals("5.0 km", text); |
||||
} |
||||
|
||||
@Test |
||||
public void sample_three(){ |
||||
List<String> allDistanceTexts = JsonPath.read(JSON, "$.rows[0].elements[*].distance.text"); |
||||
assertThat(allDistanceTexts, hasItems("0.4 km", "5.0 km")); |
||||
} |
||||
} |
@ -1,116 +0,0 @@
|
||||
package com.jayway.jsonpath.old; |
||||
|
||||
public class JsonPathFilterTest { |
||||
|
||||
public final static String DOCUMENT = |
||||
"{ \"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\": \"Evelyn Waugh\",\n" + |
||||
" \"title\": \"Sword of Honour\",\n" + |
||||
" \"price\": 12.99\n" + |
||||
" },\n" + |
||||
" { \"category\": \"fiction\",\n" + |
||||
" \"author\": \"Herman Melville\",\n" + |
||||
" \"title\": \"Moby Dick\",\n" + |
||||
" \"isbn\": \"0-553-21311-3\",\n" + |
||||
" \"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" + |
||||
" \"price\": 22.99\n" + |
||||
" }\n" + |
||||
" ],\n" + |
||||
" \"bicycle\": {\n" + |
||||
" \"color\": \"red\",\n" + |
||||
" \"price\": 19.95,\n" + |
||||
" \"foo:bar\": \"fooBar\",\n" + |
||||
" \"dot.notation\": \"new\"\n" + |
||||
" }\n" + |
||||
" }\n" + |
||||
"}"; |
||||
|
||||
/* |
||||
|
||||
@Test |
||||
public void arrays_of_maps_can_be_filtered() throws Exception { |
||||
|
||||
|
||||
Map<String, Object> rootGrandChild_A = new HashMap<String, Object>(); |
||||
rootGrandChild_A.put("name", "rootGrandChild_A"); |
||||
|
||||
Map<String, Object> rootGrandChild_B = new HashMap<String, Object>(); |
||||
rootGrandChild_B.put("name", "rootGrandChild_B"); |
||||
|
||||
Map<String, Object> rootGrandChild_C = new HashMap<String, Object>(); |
||||
rootGrandChild_C.put("name", "rootGrandChild_C"); |
||||
|
||||
|
||||
Map<String, Object> rootChild_A = new HashMap<String, Object>(); |
||||
rootChild_A.put("name", "rootChild_A"); |
||||
rootChild_A.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C)); |
||||
|
||||
Map<String, Object> rootChild_B = new HashMap<String, Object>(); |
||||
rootChild_B.put("name", "rootChild_B"); |
||||
rootChild_B.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C)); |
||||
|
||||
Map<String, Object> rootChild_C = new HashMap<String, Object>(); |
||||
rootChild_C.put("name", "rootChild_C"); |
||||
rootChild_C.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C)); |
||||
|
||||
Map<String, Object> root = new HashMap<String, Object>(); |
||||
root.put("children", asList(rootChild_A, rootChild_B, rootChild_C)); |
||||
|
||||
|
||||
|
||||
Filter customFilter = new Filter.FilterAdapter<Map<String, Object>>() { |
||||
@Override |
||||
public boolean accept(Map<String, Object> map) { |
||||
if(map.getValue("name").equals("rootGrandChild_A")){ |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
}; |
||||
|
||||
Filter rootChildFilter = filter(where("name").regex(Pattern.compile("rootChild_[A|B]"))); |
||||
Filter rootGrandChildFilter = filter(where("name").regex(Pattern.compile("rootGrandChild_[A|B]"))); |
||||
|
||||
|
||||
|
||||
|
||||
//TODO: breaking v2 solved by [?,?]
|
||||
//List read = JsonPath.read(root, "children[?].children[?][?]", rootChildFilter, rootGrandChildFilter, customFilter);
|
||||
List read = JsonPath.read(root, "children[?].children[?, ?]", rootChildFilter, rootGrandChildFilter, customFilter); |
||||
|
||||
|
||||
System.out.println(read.size()); |
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void arrays_of_objects_can_be_filtered() throws Exception { |
||||
Map<String, Object> doc = new HashMap<String, Object>(); |
||||
doc.put("items", asList(1, 2, 3)); |
||||
|
||||
Filter customFilter = new Filter.FilterAdapter<Integer>(){ |
||||
@Override |
||||
public boolean accept(Integer o) { |
||||
return 1 == o; |
||||
} |
||||
}; |
||||
|
||||
List<Integer> res = JsonPath.read(doc, "$.items[?]", customFilter); |
||||
|
||||
assertEquals(1, res.getValue(0).intValue()); |
||||
} |
||||
|
||||
*/ |
||||
} |
@ -1,41 +0,0 @@
|
||||
package com.jayway.jsonpath.old.reader; |
||||
|
||||
import com.jayway.jsonpath.Configuration; |
||||
import com.jayway.jsonpath.JsonPath; |
||||
import com.jayway.jsonpath.spi.json.JsonProvider; |
||||
import com.jayway.jsonpath.spi.json.JsonProviderFactory; |
||||
import org.junit.Test; |
||||
|
||||
public class ReadConfigurationTest { |
||||
|
||||
private static JsonProvider provider = JsonProviderFactory.createProvider(); |
||||
|
||||
|
||||
@Test |
||||
public void fluent() { |
||||
|
||||
Configuration configuration = Configuration.defaultConfiguration(); |
||||
|
||||
Configuration configuration2 = Configuration.builder() |
||||
.jsonProvider(JsonProviderFactory.createProvider()) |
||||
.build(); |
||||
//.options(Option.THROW_ON_MISSING_PROPERTY).build();
|
||||
|
||||
JsonProvider jsonProvider = JsonProviderFactory.createProvider(); |
||||
|
||||
JsonPath.using(configuration).parse("{}").read("$"); |
||||
JsonPath.using(jsonProvider).parse("{}").read("$"); |
||||
|
||||
JsonPath.parse("{}").read("$"); |
||||
JsonPath.parse("{}", configuration).read("$"); |
||||
|
||||
JsonPath.using(configuration).parse("{}").read("$"); |
||||
/* |
||||
Object updatedJsonModel = JsonPath.parse("{...}") |
||||
.write("$['store'][1]['name']", "new name") |
||||
.write("$.store[1].age", 43) |
||||
.getValue(); |
||||
*/ |
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue