The book is followed by the above:

  • JsonPath Practice (1)
  • JsonPath Practice (Ii)
  • JsonPath Practice (Iii)
  • JsonPath Practice (4)
  • JsonPath Practice (5)
  • JsonPath Practice (6)

After some twists and turns, I finally wrote the wrapper class of the JsonPath utility class in a hurry. There are no strict tests. When I have the opportunity, I will use Groovy to conduct unit testing framework Spock to write some single tests to verify.

Groovy, the language of the utility classes, is a bit of a no-go, and those using Java technology stacks should have no trouble reading it. In addition, I wrote the official API as a class annotation.

There are two methods on the Verify class, mainly for validation, involving the Groovy overloaded operator, which is a validation class written specifically for Groovy scripts, and to add functionality to the open source testing service.

The code is as follows:

package com.fun.utils

import com.alibaba.fastjson.JSONObject
import com.fun.base.exception.ParamException
import com.fun.frame.SourceCode
import com.jayway.jsonpath.JsonPath
import com.jayway.jsonpath.JsonPathException
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/ * * The following example, The official document address: https://github.com/json-path/JsonPath/blob/master/README.md * $. Store. The book [*]. The author The authors of all books * $.. author All authors * $.store.* All things, both books and bicycles * $.store.. price The price of everything * $.. book[2] The third book * $.. book[-2] The second to last book * $.. Book [0,1] The first two books * $.. book[:2] All books from index 0 (inclusive) until index 2 (exclusive) * $.. book[1:2] All books from index 1 (inclusive) until index 2 (exclusive) * $.. book[-2:] Last two books * $.. book[2:] Book number two from tail * $.. book[?(@.isbn)] All books with an ISBN number * $.store.book[?(@.price < 10)] All books in store cheaper than 10 * $.. book[?(@.price <= $['expensive'])] All books in store that are not "expensive" * $.. book[?(@.author =~ /.*REES/i)] All books matching regex (ignore case) * $.. * Give me every thing * $.. book.length() The number of books * * * min() Provides the min value of an array of numbers Double * max() Provides the max value of an array of numbers Double * avg() Provides the average value of an array of numbers Double * stddev() Provides the standard deviation value of an array of numbers Double * length() Provides the length of an array Integer * Sum () Provides the sum value of an array of numbers Double * min() Double * Max () Double * avG () average Double * Stddev () standard deviation Double * length() Array length Integer * sum() Array sum Double * == left is equal to right (note that 1 is not equal to '1') *! = left is not equal to right * < left is less than right * <= left is less or equal to right * > left is greater than right * >= left is greater than or equal to right * =~ left matches regular expression [?(@.name =~ /foo.*?/i)] * in left exists in right [?(@.size in ['S', 'M'])] * nin left does not exist in right * subsetof subsets [?(@.sizes subsetof ['S', 'M', 'L'])] * anyof left has an intersection with right [?(@.sizes anyof ['M', 'L'])] * noneof left has no intersection with right [?(@.sizes noneof ['M', 'L'])] * size size of left (array or string) should match right * empty left (array or string) should be empty */
class JsonUtil extends SourceCode {

    private static Logger logger = LoggerFactory.getLogger(JsonUtil.class)

    /** * The user builds the object and gets the verify object */
    private JSONObject json

    private JsonUtil(JSONObject json) {
        this.json = json
    }

    static JsonUtil getInstance(JSONObject json) {
        new JsonUtil(json)
    }

    Verify getVerify(String path) {
        Verify.getInstance(this.json, path)
    }

    * @param path * @return */
    String getString(String path) {
        def object = get(path)
        object == null ? EMPTY : object.toString()
    }


    /** * get int * @param path * @return */
    int getInt(String path) {
        changeStringToInt(getString(path))
    }

    /** * get Boolean type * @param path * @return */
    int getBoolean(String path) {
        changeStringToBoolean(getString(path))
    }

    /** * get long * @param path * @return */
    int getLong(String path) {
        changeStringToLong(getString(path))
    }
    
    /** * get double * @param path * @return */
    double getDouble(String path) {
        changeStringToDouble(getString(path))
    }

    /** * get the list object * @param path * @return */
    List getList(String path) {
        get(path) as List
    }

    Public IDE * @param path * @param tClass * @return */
    public <T> T getT(String path, Class<T> tClass) {
        try {
            get(path) as T
        } catch (ClassCastException e) {
            logger.warn("Type conversion failed!", e)
            null}}/** * get matching object * @param path * @return */
    Object get(String path) {
        logger.debug("Match object :{}, expression :{}", json.toString(), path)
        if (json == null || json.isEmpty()) ParamException.fail("Json is null or null, parameter error!")
        try {
            JsonPath.read(this.json, path)
        } catch (JsonPathException e) {
            logger.warn("Jsonpath :{} parsing failed, JSON value", json.toString(), path, e)
            null}}}Copy the code

  • Public account FunTester first, more original articles: 460+ original articles, welcome to pay attention to, exchange, prohibit the third party to reprint without authorization.

Hot article selected

  • Function This interface is used to test albums
  • Open source testing service
  • Performance Testing Topics
  • Graphic HTTP brain map
  • Programming thinking for everyone
  • 2020 Tester self-improvement
  • Fiddler Everywhere is the future
  • Test development engineer work skills
  • Fiddler Everywhere answers questions
  • Selenium4 IDE, it’s finally here