#
luxiaotao1123
2025-02-12 71eeac34fee9f5a53168e0872e5fb7b855c0b4c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.vincent.rsf.common.utils;
 
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
 
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
 
public class GsonUtils {
 
    // include cache
    private static final Gson gson = new Gson();
 
    public static String toJson(Object object) {
        return gson.toJson(object);
    }
 
    public static <T> T fromJson(String json, Class<T> clazz) throws JsonSyntaxException {
        return gson.fromJson(json, clazz);
    }
 
    public static <T> T fromJson(String json, Type typeOfT) throws JsonSyntaxException {
        return gson.fromJson(json, typeOfT);
    }
 
    public static <T> List<T> fromJsonToList(String json, Class<T> clazz) throws JsonSyntaxException {
        Type type = TypeToken.getParameterized(List.class, clazz).getType();
        return gson.fromJson(json, type);
    }
 
    public static <K, V> Map<K, V> fromJsonToMap(String json, Class<K> keyType, Class<V> valueType) throws JsonSyntaxException {
        Type type = TypeToken.getParameterized(Map.class, keyType, valueType).getType();
        return gson.fromJson(json, type);
    }
 
}