1,修改默認(rèn)行為
1.1,DEFAULT_PATH_LEAF_TO_NULL
默認(rèn):
@Test
public void defaultLeafToNull(){
// 使用默認(rèn)配置
Configuration conf = Configuration.defaultConfiguration();
// json數(shù)據(jù)book[2]有字段 isbn String isbn2 = JsonPath.using(conf).parse(jsonData).read("$.store.book[2].isbn");
Console.log(isbn2);
// 0-553-21311-3
// json數(shù)據(jù)book[0]沒(méi)有字段 isbn String isbn0 = JsonPath.using(conf).parse(jsonData).read("$.store.book[0].isbn");
Console.log(isbn0);
// com.jayway.jsonpath.PathNotFoundException: No results for path: $['store']['book'][0]['isbn']
}

20220721034858.png
DEFAULT_PATH_LEAF_TO_NULL配置讓沒(méi)有"葉子"的返回null
@Test
public void defaultLeafToNull2(){
Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
// json數(shù)據(jù)book[2]有字段 isbn String isbn2 = JsonPath.using(configuration).parse(jsonData).read("$.store.book[2].isbn");
Console.log(isbn2);
// json數(shù)據(jù)book[0]沒(méi)有字段 isbn String isbn0 = JsonPath.using(configuration).parse(jsonData).read("$.store.book[0].isbn");
Console.log(isbn0);
}

20220721035001.png
1.2,ALWAYS_RETURN_LIST
默認(rèn):
@Test
public void defaultReturnList(){
// 使用默認(rèn)配置
Configuration conf = Configuration.defaultConfiguration();
// json數(shù)據(jù)book[2]有字段 isbn String isbn2 = JsonPath.using(conf).parse(jsonData).read("$.store.book[2].isbn");
Console.log(isbn2);
}

20220721035247.png
ALWAYS_RETURN_LIST配置讓JsonPath返回結(jié)果總是為L(zhǎng)ist,即使查詢(xún)路徑是明確的.
@Test
public void defaultReturnList2(){
Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.ALWAYS_RETURN_LIST);
// json數(shù)據(jù)book[2]有字段 isbn List<String> isbn2 = JsonPath.using(configuration).parse(jsonData).read("$.store.book[2].isbn");
Console.log(isbn2);
}

20220721035349.png

20220721035423.png
1.3,SUPPRESS_EXCEPTIONS
SUPPRESS_EXCEPTIONS此選項(xiàng)確保不從路徑計(jì)算傳播異常。它遵循以下簡(jiǎn)單的規(guī)則:
- 如果選項(xiàng)' ALWAYS_RETURN_LIST '存在,返回一個(gè)空列表
- 如果選項(xiàng)' ALWAYS_RETURN_LIST '不存在,返回null
1.4,REQUIRE_PROPERTIES
默認(rèn):
@Test
public void defaultRequiredProperties2(){
Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES);
List<String> isbns = JsonPath.using(configuration).parse(jsonData).read("$.store.book[*].isbn");
Console.log(isbns);
}

20220721035859.png
REQUIRE_PROPERTIES該選項(xiàng)將JsonPath配置為在計(jì)算“不定”路徑時(shí)要求path中定義的屬性必須存在。
@Test
public void defaultRequiredProperties2(){
Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES);
List<String> isbns = JsonPath.using(configuration).parse(jsonData).read("$.store.book[*].isbn");
Console.log(isbns);
}

20220721040026.png
2,JsonPath中緩存自定義
2.1,Cache API介紹
在JsonPath 2.1.0中引入了一種新的Cache SPI。允許API使用者以適合的方式配置路徑緩存。在首次訪(fǎng)問(wèn)緩存或引發(fā)JsonPathException之前,必須對(duì)其進(jìn)行配置。
JsonPath 附帶了2中緩存實(shí)現(xiàn):
-
com.jayway.jsonpath.spi.cache.LRUCache(default, thread safe) -
com.jayway.jsonpath.spi.cache.NOOPCache(no cache)
2.2,實(shí)現(xiàn)自定義的緩存API
CacheProvider.setCache(new Cache() {
//Not thread safe simple cache
private Map<String, JsonPath> map = new HashMap<String, JsonPath>();
@Override
public JsonPath get(String key) {
return map.get(key);
}
@Override
public void put(String key, JsonPath jsonPath) {
map.put(key, jsonPath);
}
});
2.3,JsonPath默認(rèn)緩存實(shí)現(xiàn)源碼
JsonContext中

20220721040634.png
線(xiàn)程安全原因

20220721040757.png
默認(rèn)實(shí)現(xiàn)

20220721040822.png

20220721040856.png