JsonHelper.java 3.76 KB
Newer Older
yanzg's avatar
yanzg committed
1 2 3 4 5 6 7
package com.yanzuoguang.util.helper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

/**
 * JSON序列化的处理
yanzg's avatar
yanzg committed
8
 *
yanzg's avatar
yanzg committed
9
 * @author 颜佐光
yanzg's avatar
yanzg committed
10
 */
yanzg's avatar
yanzg committed
11 12 13 14 15 16
public class JsonHelper {

    /**
     * JSON对象开始标记
     */
    public static final String OBJECT_START_FLAG = "{";
yanzg's avatar
yanzg committed
17

yanzg's avatar
yanzg committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    /**
     * 将JSON字符串转化为JSON对象
     *
     * @param from 来源对象,可以为JSON字符串
     * @param type 对象的类型
     * @param <T>  转换后的结果的类型
     * @return 转换后的结果
     * @throws Exception 转换结果发生异常
     */
    public static <T> T to(Object from, Class<T> type) {
        String json = serialize(from);
        return deserialize(json, type);
    }

    /**
     * 将JSON字符串转化为JSON对象
     *
     * @param from 来源对象,可以为JSON字符串
     * @param type 对象的类型
     * @param <T>  转换后的结果的类型
     * @return 转换后的结果
     * @throws Exception 转换结果发生异常
     */
    public static <T> T to(Object from, TypeReference<T> type) {
        String json = serialize(from);
        return deserialize(json, type);
    }

    /**
     * 将JSON字符串转换为数组
     *
     * @param from 来源对象,可以为JSON字符串
     * @param type 转换的类型
     * @param <T>  数组的类型
     * @return 返回数组
     */
    public static <T> T toArray(Object from, TypeReference<T> type) {
        String json = serialize(from);
        return deserializeArray(json, type);
    }

yanzg's avatar
yanzg committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    /**
     * 将JSON字符串转化为JSON对象
     *
     * @param json JSON字符串
     * @param type 对象的类型
     * @param <T>  转换后的结果的类型
     * @return 转换后的结果
     * @throws Exception 转换结果发生异常
     */
    public static <T> T deserialize(String json, Class<T> type) {
        return JSON.parseObject(json, type);
    }

    /**
     * 将JSON字符串转化为JSON对象
     *
     * @param json JSON字符串
     * @param type 对象的类型
     * @param <T>  转换后的结果的类型
     * @return 转换后的结果
     * @throws Exception 转换结果发生异常
     */
    public static <T> T deserialize(String json, TypeReference<T> type) {
        return JSON.parseObject(json, type);
    }

    /**
     * 将JSON字符串转换为数组
     *
     * @param json 需要转换的JSON
     * @param type 转换的类型
     * @param <T>  数组的类型
     * @return 返回数组
     */
    public static <T> T deserializeArray(String json, TypeReference<T> type) {
        if (json == null || json.length() < 1) {
            return null;
        }
        json = json.trim();
yanzg's avatar
yanzg committed
98
        if (json.startsWith(OBJECT_START_FLAG)) {
yanzg's avatar
yanzg committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
            json = String.format("[%s]", json);
        }
        return JSON.parseObject(json, type);
    }

    /**
     * 将对象序列化成JSON
     *
     * @param value 需要序列化的对象
     * @return JSON结果
     * @throws Exception 抛出的异常信息
     */
    public static String serialize(Object value) {
        return JSON.toJSONString(value);
    }

    /**
     * 将对象序列化成JSON
     *
     * @param value  需要序列化的对象
     * @param isLine 是否需要换行
     * @return JSON结果
     * @throws Exception 抛出的异常信息
     */
    public static String serialize(Object value, boolean isLine) {
        return JSON.toJSONString(value, isLine);
    }

yanzg's avatar
yanzg committed
127 128 129 130 131 132 133 134 135 136 137 138 139
    /**
     * JSON对比对象值
     *
     * @param from  来源对象
     * @param to    目标对象
     * @return 对比结果
     */
    public static boolean compare(Object from, Object to) {
        return StringHelper.compare(
                JsonHelper.serialize(from),
                JsonHelper.serialize(to),
                false);
    }
yanzg's avatar
yanzg committed
140 141

}