【工具集】WTool JAVA工具集

依赖于Hutool


    cn.hutool
    hutool-all
    5.7.22

【工具集】WTool JAVA工具集


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.Date;

/**
 * W工具集
 * 聚合工具类
 * 时间
 * JSON
 *
 */
public class WTool {

    /**
     *  ======================  日期类 START  ======================================
     */

    /**
     * 获取日期
     *
     * @return yyyy-MM-dd 00:00:00
     */
    public static Date yyyyMMdd(){
        return DateUtil.parseDate(DateUtil.formatDate(new Date()));
    }


    /**
     *  ======================  日期类 END  ======================================
     */


    /**
     *  ======================  JSON类 START  ======================================
     */

    /**
     * 通过表达式获取JSON中嵌套的对象
     * 具体参考 JSONUtil.getByPath
     * @param jsonStr
     * @param expression
     * @param defaultValue
     * @param 
     * @return
     */
    public static  T jsonGetByPath(JSON jsonStr, String expression, T defaultValue){

        return JSONUtil.getByPath(JSONUtil.parse(jsonStr), expression, defaultValue);
    }

    /**
     * 通过表达式获取JSON中嵌套的对象
     * 具体参考 JSONUtil.getByPath
     * @param jsonStr
     * @param expression
     * @param defaultValue
     * @param 
     * @return
     */
    public static  T jsonGetByPath(String jsonStr, String expression, T defaultValue){
        if(!JSONUtil.isJson(jsonStr)){
            return defaultValue;
        }
        return WTool.jsonGetByPath(JSONUtil.parse(jsonStr), expression, defaultValue);
    }

    /**
     *
     * 通过表达式设置JSON中嵌套的对象
     JSONObject jsonObj = WTool.jsonPutByPath("", new Object[][]{
     {"parent.id", 1},
     {"parent.name", "张三"}
     });
     *
     * @param jsonStr       json字符串 可空
     * @param expression    表达式
     * @param value    值
     * @return
     */
    public static JSONObject jsonPutByPath(String jsonStr, String expression, Object value){
        JSONObject json = null;
        if(JSONUtil.isJson(jsonStr)){
            json = JSONUtil.parseObj(jsonStr);
        }
        json.putByPath(expression, value);
        return json;
    }

    /**
     *
     * 通过表达式设置JSON中嵌套的对象
      JSONObject jsonObj = WTool.jsonPutByPath("", new Object[][]{
                      {"parent.id", 1},
                      {"parent.name", "张三"}
              });
     *
     * @param jsonObject       json对象 可空
     * @param expressionAndValue    表达式及值
     * @return
     */
    public static JSONObject jsonPutByPath(JSONObject jsonObject, Object[][] expressionAndValue){

        if(jsonObject == null){
            jsonObject = new JSONObject();
        }

        if(ArrayUtil.isEmpty(expressionAndValue)){
            return jsonObject;
        }

        for (Object[] objects : expressionAndValue) {
            Object expression = objects[0];
            Object value = objects[1];

            if(expression == null || StrUtil.isBlank(expression.toString())){
                continue;
            }

            jsonObject.putByPath(expression.toString(), value);
        }

        return jsonObject;
    }

    /**
     *
     * 通过表达式设置JSON中嵌套的对象
     JSONObject jsonObj = WTool.jsonPutByPath("", new Object[][]{
     {"parent.id", 1},
     {"parent.name", "张三"}
     });
     *
     * @param jsonStr       json字符串 可空
     * @param expressionAndValue    表达式及值
     * @return
     */
    public static JSONObject jsonPutByPath(String jsonStr, Object[][] expressionAndValue){
        JSONObject jsonObject = null;
        if(JSONUtil.isJsonObj(jsonStr)){
            jsonObject = JSONUtil.parseObj(jsonStr);
        }

        return WTool.jsonPutByPath(jsonObject, expressionAndValue);
    }

    /**
     * 初始化json对象
     * @param expressionAndValue
     * @return
     */
    public static JSONObject jsonOfPath(Object[][] expressionAndValue){

        return WTool.jsonPutByPath("", expressionAndValue);
    }

    /**
     *  ======================  JSON类 END  ======================================
     */




    public static void main(String[] args) {
        JSONObject json = WTool.jsonPutByPath("", new Object[][]{
                {"parent.id", 1},
                {"parent.name", "张三"}
        });
        System.out.println(json);
    }

}

你可能感兴趣的:(工具自用,java,开源)