美文网首页
5,JsonPath进阶实战--默认配置

5,JsonPath进阶实战--默认配置

作者: lcjyzm | 来源:发表于2022-07-25 09:33 被阅读0次

    1,修改默认行为

    1.1,DEFAULT_PATH_LEAF_TO_NULL
    默认:

    
    @Test  
    public void defaultLeafToNull(){  
        // 使用默认配置  
        Configuration conf = Configuration.defaultConfiguration();  
      
        // json数据book[2]有字段 isbn    String isbn2 = JsonPath.using(conf).parse(jsonData).read("$.store.book[2].isbn");  
        Console.log(isbn2);  
        // 0-553-21311-3  
      
        // json数据book[0]没有字段 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配置让没有"叶子"的返回null

    
    @Test  
    public void defaultLeafToNull2(){  
        Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);  
        // json数据book[2]有字段 isbn    String isbn2 = JsonPath.using(configuration).parse(jsonData).read("$.store.book[2].isbn");  
        Console.log(isbn2);  
      
        // json数据book[0]没有字段 isbn    String isbn0 = JsonPath.using(configuration).parse(jsonData).read("$.store.book[0].isbn");  
        Console.log(isbn0);  
    }
    
    
    20220721035001.png

    1.2,ALWAYS_RETURN_LIST
    默认:

    @Test  
    public void defaultReturnList(){  
        // 使用默认配置  
        Configuration conf = Configuration.defaultConfiguration();  
      
        // json数据book[2]有字段 isbn    String isbn2 = JsonPath.using(conf).parse(jsonData).read("$.store.book[2].isbn");  
        Console.log(isbn2);  
    }
    
    
    20220721035247.png

    ALWAYS_RETURN_LIST配置让JsonPath返回结果总是为List,即使查询路径是明确的.

    
    @Test  
    public void defaultReturnList2(){  
        Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.ALWAYS_RETURN_LIST);  
        // json数据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此选项确保不从路径计算传播异常。它遵循以下简单的规则:

    • 如果选项' ALWAYS_RETURN_LIST '存在,返回一个空列表
    • 如果选项' ALWAYS_RETURN_LIST '不存在,返回null

    1.4,REQUIRE_PROPERTIES
    默认:

    @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该选项将JsonPath配置为在计算“不定”路径时要求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使用者以适合的方式配置路径缓存。在首次访问缓存或引发JsonPathException之前,必须对其进行配置。
    JsonPath 附带了2中缓存实现:

    • com.jayway.jsonpath.spi.cache.LRUCache (default, thread safe)
    • com.jayway.jsonpath.spi.cache.NOOPCache (no cache)

    2.2,实现自定义的缓存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默认缓存实现源码

    JsonContext

    20220721040634.png

    线程安全原因

    20220721040757.png

    默认实现

    20220721040822.png 20220721040856.png

    相关文章

      网友评论

          本文标题:5,JsonPath进阶实战--默认配置

          本文链接:https://www.haomeiwen.com/subject/vjauirtx.html