fastjson2 tool class

Why do we need some self-encapsulated tool classes? Taking fastjson in this article as an example, in daily development, when using fastjson, it is written directly as follows:

// ****** 常见的用法 ******

// 把对象转成json字符串
JSON.toJSONString(response);

// 把json字符串转成对象
Response response = JSON.parseObject(str, Response.class);

There is no problem in this way, but when doing system transformation and upgrade later, I decided to upgrade fastjson to fastjson2. All classes that use fastjson and the places where the package is imported must be changed. Although it can be replaced globally, there are a lot of files to change. , you still need to check it after making the changes. But if there is a unified tool class, you only need to change the tool class. Other places where this tool class is used do not need to be changed. It is very convenient and error-free.

There are other examples, such as log. In springboot, the default logback is used, and some places or other projects use log4j. In order to unify it later, it must be changed to one of them. If there is a log tool class, then switch to a different one. For the log, just change the tool class, otherwise you have to change the files one by one.

The following is a simple fastjson tool class that can be copied and used directly: 

package com.xxxx.xxxx;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.List;

public class JsonUtil {

    /**
     * 将对象转换为JSON字符串
     * @param obj 要转换为JSON的对象
     * @return JSON字符串
     */
    public static String toJSONString(Object obj) {
        return JSON.toJSONString(obj);
    }

    /**
     * 将对象转换为byte[]
     * @param obj 要转换为JSON的对象
     * @return byte[]
     */
    public static byte[] toJSONBytes(Object obj) {
        return JSON.toJSONBytes(obj);
    }

    /**
     * 将JSON字符串转换为指定类型的对象
     * @param jsonString JSON字符串
     * @param clazz 目标对象类型的Class
     * @param <T> 目标对象的类型
     * @return 转换后的目标对象
     */
    public static <T> T parseObject(String jsonString, Class<T> clazz) {
        return JSON.parseObject(jsonString, clazz);
    }

    /**
     * 将JSON字符串转换为指定类型的list
     * @param jsonString JSON字符串
     * @param clazz 目标对象类型的Class
     * @param <T> 目标对象的类型
     * @return 转换后的目标对象
     */
    public static <T> List<T> parseArray(String jsonString, Class<T> clazz) {
        return JSON.parseArray(jsonString, clazz);
    }

    /**
     * 将JSON字符串转换为JSONObject对象
     * @param jsonString JSON字符串
     * @return 转换后的JSONObject对象
     */
    public static JSONObject parseJSONObject(String jsonString) {
        return JSON.parseObject(jsonString);
    }

    /**
     * 将JSON字符串转换为JSONArray对象
     * @param jsonString JSON字符串
     * @return 转换后的JSONArray对象
     */
    public static JSONArray parseJSONArray(String jsonString) {
        return JSON.parseArray(jsonString);
    }

    /**
     * 将Java对象转换为JSONObject对象
     * @param obj Java对象
     * @return 转换后的JSONObject对象
     */
    public static JSONObject toJSONObject(Object obj) {
        return (JSONObject) JSON.toJSON(obj);
    }

    /**
     * 将Java对象转换为JSONArray对象
     * @param obj Java对象
     * @return 转换后的JSONArray对象
     */
    public static JSONArray toJSONArray(Object obj) {
        return (JSONArray) JSON.toJSON(obj);
    }

}

Guess you like

Origin blog.csdn.net/wangkaichenjuan/article/details/132554710