JsonPath仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

608 lines
21 KiB

package com.jayway.jsonpath.old;
import com.jayway.jsonpath.BaseTest;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Criteria;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
12 years ago
import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;
import net.minidev.json.JSONAware;
import net.minidev.json.parser.JSONParser;
11 years ago
import org.assertj.core.api.Assertions;
12 years ago
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
11 years ago
import java.util.Collections;
import java.util.EnumSet;
13 years ago
import java.util.List;
import java.util.Map;
import java.util.Set;
13 years ago
import static com.jayway.jsonpath.JsonPath.read;
11 years ago
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
11 years ago
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
import static org.assertj.core.api.Assertions.filter;
12 years ago
import static org.hamcrest.Matchers.is;
11 years ago
import static org.junit.Assert.assertEquals;
12 years ago
import static org.junit.Assert.assertThat;
public class IssuesTest extends BaseTest {
private static final JsonProvider jp = Configuration.defaultConfiguration().jsonProvider();
11 years ago
@Test
public void full_ones_can_be_filtered() {
String json = "[\n" +
" {\"kind\" : \"full\"},\n" +
" {\"kind\" : \"empty\"}\n" +
"]";
List<Map<String, String>> fullOnes = read(json, "$[?(@.kind == full)]");
11 years ago
assertEquals(1, fullOnes.size());
assertEquals("full", fullOnes.get(0).get("kind"));
11 years ago
}
@Test
public void issue_36() {
String json = "{\n" +
"\n" +
" \"arrayOfObjectsAndArrays\" : [ { \"k\" : [\"json\"] }, { \"k\":[\"path\"] }, { \"k\" : [\"is\"] }, { \"k\" : [\"cool\"] } ],\n" +
"\n" +
" \"arrayOfObjects\" : [{\"k\" : \"json\"}, {\"k\":\"path\"}, {\"k\" : \"is\"}, {\"k\" : \"cool\"}]\n" +
"\n" +
" }";
Object o1 = read(json, "$.arrayOfObjectsAndArrays..k ");
Object o2 = read(json, "$.arrayOfObjects..k ");
12 years ago
assertEquals("[[\"json\"],[\"path\"],[\"is\"],[\"cool\"]]", jp.toJson(o1));
assertEquals("[\"json\",\"path\",\"is\",\"cool\"]", jp.toJson(o2));
}
12 years ago
@Test
public void issue_11() throws Exception {
13 years ago
String json = "{ \"foo\" : [] }";
List<String> result = read(json, "$.foo[?(@.rel == 'item')][0].uri");
13 years ago
assertTrue(result.isEmpty());
}
12 years ago
@Test(expected = PathNotFoundException.class)
public void issue_11b() throws Exception {
String json = "{ \"foo\" : [] }";
read(json, "$.foo[0].uri");
12 years ago
}
12 years ago
@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 = read(json, "$.store.book[?(@.children==true)].title");
12 years ago
assertThat(titles, Matchers.contains("Moby Dick"));
assertEquals(1, titles.size());
}
@Test
public void issue_24() {
InputStream is = null;
try {
is = this.getClass().getResourceAsStream("/issue_24.json");
//Object o = JsonPath.read(is, "$.project[?(@.template.@key == 'foo')].field[*].@key");
Object o = read(is, "$.project.field[*].@key");
//Object o = JsonPath.read(is, "$.project.template[?(@.@key == 'foo')].field[*].@key");
is.close();
} catch (Exception e) {
//e.printStackTrace();
Utils.closeQuietly(is);
}
}
@Test
public void issue_28_string() {
String json = "{\"contents\": [\"one\",\"two\",\"three\"]}";
List<String> result = read(json, "$.contents[?(@ == 'two')]");
assertThat(result, Matchers.contains("two"));
assertEquals(1, result.size());
}
12 years ago
@Test
public void issue_37() {
String json = "[\n" +
" {\n" +
" \"id\": \"9\",\n" +
" \"sku\": \"SKU-001\",\n" +
" \"compatible\": false\n" +
" },\n" +
" {\n" +
" \"id\": \"13\",\n" +
" \"sku\": \"SKU-005\",\n" +
" \"compatible\": true\n" +
" },\n" +
" {\n" +
" \"id\": \"11\",\n" +
" \"sku\": \"SKU-003\",\n" +
" \"compatible\": true\n" +
" }\n" +
"]";
List<String> result = read(json, "$.[?(@.compatible == true)].sku");
12 years ago
Assertions.assertThat(result).containsExactly("SKU-005", "SKU-003");
12 years ago
}
@Test
public void issue_38() {
String json = "{\n" +
" \"datapoints\":[\n" +
" [\n" +
" 10.1,\n" +
" 13.0\n" +
" ],\n" +
" [\n" +
" 21.0,\n" +
" 22.0\n" +
" ]\n" +
" ]\n" +
"}";
List<Double> result = read(json, "$.datapoints.[*].[0]");
12 years ago
assertThat(result.get(0), is(new Double(10.1)));
assertThat(result.get(1), is(new Double(21.0)));
}
@Test
public void issue_39() {
String json = "{\n" +
" \"obj1\": {\n" +
" \"arr\": [\"1\", \"2\"]\n" +
" },\n" +
" \"obj2\": {\n" +
" \"arr\": [\"3\", \"4\"]\n" +
" }\n" +
"}\n";
List<String> result = read(json, "$..arr");
12 years ago
assertThat(result.size(), is(2));
}
@Test
public void issue_28_int() {
String json = "{\"contents\": [1,2,3]}";
List<Integer> result = read(json, "$.contents[?(@ == 2)]");
assertThat(result, Matchers.contains(2));
assertEquals(1, result.size());
}
@Test
public void issue_28_boolean() {
String json = "{\"contents\": [true, true, false]}";
List<Boolean> result = read(json, "$.contents[?(@ == true)]");
assertThat(result, Matchers.contains(true, true));
assertEquals(2, result.size());
}
@Test(expected = PathNotFoundException.class)
12 years ago
public void issue_22() throws Exception {
11 years ago
Configuration configuration = Configuration.defaultConfiguration();
String json = "{\"a\":{\"b\":1,\"c\":2}}";
11 years ago
JsonPath.parse(json, configuration).read("a.d");
}
@Test
public void issue_22c() throws Exception {
11 years ago
//Configuration configuration = Configuration.builder().build();
Configuration configuration = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build();
String json = "{\"a\":{\"b\":1,\"c\":2}}";
assertNull(JsonPath.parse(json, configuration).read("a.d"));
}
@Test
public void issue_22b() throws Exception {
12 years ago
String json = "{\"a\":[{\"b\":1,\"c\":2},{\"b\":5,\"c\":2}]}";
List<Object> res = JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)).parse(json).read("a[?(@.b==5)].d");
11 years ago
Assertions.assertThat(res).hasSize(1).containsNull();
}
@Test(expected = PathNotFoundException.class)
public void issue_26() throws Exception {
String json = "[{\"a\":[{\"b\":1,\"c\":2}]}]";
Object o = read(json, "$.a");
12 years ago
}
@Test
public void issue_29_a() throws Exception {
String json = "{\"list\": [ { \"a\":\"atext\", \"b.b-a\":\"batext2\", \"b\":{ \"b-a\":\"batext\", \"b-b\":\"bbtext\" } }, { \"a\":\"atext2\", \"b\":{ \"b-a\":\"batext2\", \"b-b\":\"bbtext2\" } } ] }";
List<Map<String, Object>> result = read(json, "$.list[?(@['b.b-a']=='batext2')]");
assertEquals(1, result.size());
12 years ago
Object a = result.get(0).get("a");
assertEquals("atext", a);
result = read(json, "$.list[?(@.b.b-a=='batext2')]");
assertEquals(1, result.size());
assertEquals("atext2", result.get(0).get("a"));
}
@Test
public void issue_29_b() throws Exception {
String json = "{\"list\": [ { \"a\":\"atext\", \"b\":{ \"b-a\":\"batext\", \"b-b\":\"bbtext\" } }, { \"a\":\"atext2\", \"b\":{ \"b-a\":\"batext2\", \"b-b\":\"bbtext2\" } } ] }";
List<String> result = read(json, "$.list[?]", Filter.filter(Criteria.where("b.b-a").eq("batext2")));
assertTrue(result.size() == 1);
}
@Test
public void issue_30() throws Exception {
String json = "{\"foo\" : {\"@id\" : \"123\", \"$\" : \"hello\"}}";
assertEquals("123", read(json, "foo.@id"));
assertEquals("hello", read(json, "foo.$"));
}
@Test
public void issue_32() {
String json = "{\"text\" : \"skill: \\\"Heuristic Evaluation\\\"\", \"country\" : \"\"}";
assertEquals("skill: \"Heuristic Evaluation\"", read(json, "$.text"));
}
@Test
public void issue_33() {
String json = "{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": {\n" +
" \"name\": \"Author Name\",\n" +
" \"age\": 36\n" +
" },\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" +
" \"isbn\": \"0-553-21311-3\"\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95\n" +
" }\n" +
" }\n" +
"}";
List<Map<String, Object>> result = read(json, "$.store.book[?(@.author.age == 36)]");
Assertions.assertThat(result).hasSize(1);
Assertions.assertThat(result.get(0)).containsEntry("title", "Sayings of the Century");
}
12 years ago
12 years ago
@Test
public void array_root() {
String json = "[\n" +
" {\n" +
" \"a\": 1,\n" +
" \"b\": 2,\n" +
" \"c\": 3\n" +
" }\n" +
"]";
assertEquals(1, read(json, "$[0].a"));
12 years ago
}
@Test(expected = PathNotFoundException.class)
public void a_test() {
String json = "{\n" +
" \"success\": true,\n" +
" \"data\": {\n" +
" \"user\": 3,\n" +
" \"own\": null,\n" +
" \"passes\": null,\n" +
" \"completed\": null\n" +
" },\n" +
" \"version\": 1371160528774\n" +
"}";
Object read = read(json, "$.data.passes[0].id");
}
11 years ago
@Test
public void issue_42() {
String json = "{" +
" \"list\": [{" +
" \"name\": \"My (String)\" " +
" }] " +
" }";
List<Map<String, String>> result = read(json, "$.list[?(@.name == 'My (String)')]");
11 years ago
Assertions.assertThat(result).containsExactly(Collections.singletonMap("name", "My (String)"));
}
@Test
public void issue_43() {
String json = "{\"test\":null}";
Assertions.assertThat(read(json, "test")).isNull();
11 years ago
Assertions.assertThat(JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.SUPPRESS_EXCEPTIONS)).parse(json).read("nonExistingProperty")).isNull();
11 years ago
try {
read(json, "nonExistingProperty");
11 years ago
failBecauseExceptionWasNotThrown(PathNotFoundException.class);
} catch (PathNotFoundException e) {
11 years ago
11 years ago
}
try {
read(json, "nonExisting.property");
11 years ago
failBecauseExceptionWasNotThrown(PathNotFoundException.class);
} catch (PathNotFoundException e) {
11 years ago
}
}
@Test
public void issue_45() {
String json = "{\"rootkey\":{\"sub.key\":\"value\"}}";
Assertions.assertThat(read(json, "rootkey['sub.key']")).isEqualTo("value");
11 years ago
}
@Test
public void issue_46() {
String json = "{\"a\": {}}";
Configuration configuration = Configuration.defaultConfiguration().setOptions(Option.SUPPRESS_EXCEPTIONS);
11 years ago
Assertions.assertThat(JsonPath.using(configuration).parse(json).read("a.x")).isNull();
try {
read(json, "a.x");
11 years ago
failBecauseExceptionWasNotThrown(PathNotFoundException.class);
} catch (PathNotFoundException e) {
11 years ago
Assertions.assertThat(e).hasMessage("No results for path: $['a']['x']");
}
}
@Test
public void issue_x() {
String json = "{\n" +
" \"a\" : [\n" +
" {},\n" +
" { \"b\" : [ { \"c\" : \"foo\"} ] }\n" +
" ]\n" +
"}\n";
List<String> result = JsonPath.read(json, "$.a.*.b.*.c");
Assertions.assertThat(result).containsExactly("foo");
11 years ago
}
@Test
public void issue_60() {
String json = "[\n" +
"{\n" +
" \"mpTransactionId\": \"542986eae4b001fd500fdc5b-coreDisc_50-title\",\n" +
" \"resultType\": \"FAIL\",\n" +
" \"narratives\": [\n" +
" {\n" +
" \"ruleProcessingDate\": \"Nov 2, 2014 7:30:20 AM\",\n" +
" \"area\": \"Discovery\",\n" +
" \"phase\": \"Validation\",\n" +
" \"message\": \"Chain does not have a discovery event. Possible it was cut by the date that was picked\",\n" +
" \"ruleName\": \"Validate chain\\u0027s discovery event existence\",\n" +
" \"lastRule\": true\n" +
" }\n" +
" ]\n" +
"},\n" +
"{\n" +
" \"mpTransactionId\": \"54298649e4b001fd500fda3e-fixCoreDiscovery_3-title\",\n" +
" \"resultType\": \"FAIL\",\n" +
" \"narratives\": [\n" +
" {\n" +
" \"ruleProcessingDate\": \"Nov 2, 2014 7:30:20 AM\",\n" +
" \"area\": \"Discovery\",\n" +
" \"phase\": \"Validation\",\n" +
" \"message\": \"There is one and only discovery event ContentDiscoveredEvent(230) found.\",\n" +
" \"ruleName\": \"Marks existence of discovery event (230)\",\n" +
" \"lastRule\": false\n" +
" },\n" +
" {\n" +
" \"ruleProcessingDate\": \"Nov 2, 2014 7:30:20 AM\",\n" +
" \"area\": \"Discovery/Processing\",\n" +
" \"phase\": \"Validation\",\n" +
" \"message\": \"Chain does not have SLA start event (204) in Discovery or Processing. \",\n" +
" \"ruleName\": \"Check if SLA start event is not present (204). \",\n" +
" \"lastRule\": false\n" +
" },\n" +
" {\n" +
" \"ruleProcessingDate\": \"Nov 2, 2014 7:30:20 AM\",\n" +
" \"area\": \"Processing\",\n" +
" \"phase\": \"Transcode\",\n" +
" \"message\": \"No start transcoding events found\",\n" +
" \"ruleName\": \"Start transcoding events missing (240)\",\n" +
" \"lastRule\": true\n" +
" }\n" +
" ]\n" +
"}]";
List<String> problems = JsonPath.read(json, "$..narratives[?(@.lastRule==true)].message");
Assertions.assertThat(problems).containsExactly("Chain does not have a discovery event. Possible it was cut by the date that was picked", "No start transcoding events found");
}
//http://stackoverflow.com/questions/28596324/jsonpath-filtering-api
@Test
public void stack_overflow_question_1() {
String json = "{\n" +
"\"store\": {\n" +
" \"book\": [\n" +
" {\n" +
" \"category\": \"reference\",\n" +
" \"authors\" : [\n" +
" {\n" +
" \"firstName\" : \"Nigel\",\n" +
" \"lastName\" : \"Rees\"\n" +
" }\n" +
" ],\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" },\n" +
" {\n" +
" \"category\": \"fiction\",\n" +
" \"authors\": [\n" +
" {\n" +
" \"firstName\" : \"Evelyn\",\n" +
" \"lastName\" : \"Waugh\"\n" +
" },\n" +
" {\n" +
" \"firstName\" : \"Another\",\n" +
" \"lastName\" : \"Author\"\n" +
" }\n" +
" ],\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
Filter filter = Filter.filter(Criteria.where("authors[*].lastName").contains("Waugh"));
Object read = JsonPath.parse(json).read("$.store.book[?]", filter);
System.out.println(read);
}
@Test
public void issue_71() {
String json = "{\n"
+ " \"logs\": [\n"
+ " {\n"
+ " \"message\": \"it's here\",\n"
+ " \"id\": 2\n"
+ " }\n"
+ " ]\n"
+ "}";
List<String> result = JsonPath.read(json, "$.logs[?(@.message == 'it\\'s here')].message");
Assertions.assertThat(result).containsExactly("it's here");
}
@Test
public void issue_76() throws Exception {
/*
String json = "{\n" +
" \"cpus\": -8.88178419700125e-16,\n" +
" \"disk\": 0,\n" +
" \"mem\": 0\n" +
"}";
JSONParser parser = new JSONParser(JSONParser.MODE_PERMISSIVE);
JSONAware jsonModel = (JSONAware)parser.parse(json);
jsonModel.toJSONString(); //java.lang.RuntimeException: no Getter for field scale in class java.math.BigDecimal
*/
DocumentContext doc = JsonPath.parse(this.getClass().getResourceAsStream("/issue_76.json"));
Object o;
o = doc.read("$.frameworks");
//Bug filed in json-smart
// https://code.google.com/p/json-smart/issues/detail?id=55&thanks=55&ts=1427547500
try {
System.out.println(JSON_SMART_CONFIGURATION.jsonProvider().toJson(o));
Assert.assertTrue(false);
} catch (RuntimeException e){
}
}
}