102 lines
2.7 KiB
Java
102 lines
2.7 KiB
Java
|
|
package com.xscm.moduleutil.utils;
|
|||
|
|
|
|||
|
|
//import com.blankj.utilcode.util.LogUtils;
|
|||
|
|
|
|||
|
|
import com.google.gson.Gson;
|
|||
|
|
import com.google.gson.GsonBuilder;
|
|||
|
|
import com.google.gson.JsonArray;
|
|||
|
|
import com.google.gson.JsonElement;
|
|||
|
|
import com.google.gson.JsonParser;
|
|||
|
|
import com.google.gson.JsonSyntaxException;
|
|||
|
|
import com.google.gson.reflect.TypeToken;
|
|||
|
|
import com.xscm.moduleutil.http.BaseModelTypeAdapterFactory;
|
|||
|
|
import com.xscm.moduleutil.utils.logger.Logger;
|
|||
|
|
|
|||
|
|
import java.util.ArrayList;
|
|||
|
|
import java.util.List;
|
|||
|
|
import java.util.Map;
|
|||
|
|
|
|||
|
|
public class GsonUtils {
|
|||
|
|
private static Gson GSON ;
|
|||
|
|
// private static Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
|||
|
|
private static final JsonParser PARSER = new JsonParser();
|
|||
|
|
|
|||
|
|
public static Gson getGSON() {
|
|||
|
|
if (GSON == null) {
|
|||
|
|
GSON = new GsonBuilder()
|
|||
|
|
.serializeNulls() // 序列化 null 值
|
|||
|
|
.registerTypeAdapterFactory(new BaseModelTypeAdapterFactory())
|
|||
|
|
.create();
|
|||
|
|
}
|
|||
|
|
return GSON;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Object 转 json
|
|||
|
|
*
|
|||
|
|
* @param object object
|
|||
|
|
* @return object
|
|||
|
|
*/
|
|||
|
|
public static String GsonString(Object object) {
|
|||
|
|
return GSON.toJson(object);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static <T> T GsonToBean(String gsonString, Class<T> cls) {
|
|||
|
|
return GSON.fromJson(gsonString, cls);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 转成map的
|
|||
|
|
*
|
|||
|
|
* @param gsonString str
|
|||
|
|
* @return map
|
|||
|
|
*/
|
|||
|
|
public static Map<String, Object> GsonToMaps(String gsonString) {
|
|||
|
|
return GSON.fromJson(gsonString, new TypeToken<Map<String, Object>>() {
|
|||
|
|
}.getType());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 将json 格式化输出
|
|||
|
|
*
|
|||
|
|
* @param str str
|
|||
|
|
* @return str
|
|||
|
|
*/
|
|||
|
|
public static String GsonToString(String str) {
|
|||
|
|
try {
|
|||
|
|
return GSON.toJson(PARSER.parse(str));
|
|||
|
|
} catch (JsonSyntaxException e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
return str;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* json字符串转成list
|
|||
|
|
* 解决泛型问题
|
|||
|
|
* 备注:
|
|||
|
|
* List<T> list=gson.fromJson(gsonString, new TypeToken<List<T>>() {}.getType());
|
|||
|
|
* 该方法会报泛型类型擦除问题
|
|||
|
|
*
|
|||
|
|
* @param gsonString
|
|||
|
|
* @param cls
|
|||
|
|
* @param <T>
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static <T> List<T> GsonToList(String gsonString, Class<T> cls) {
|
|||
|
|
List<T> list = new ArrayList<T>();
|
|||
|
|
try {
|
|||
|
|
if (GSON != null) {
|
|||
|
|
JsonArray array = new JsonParser().parse(gsonString).getAsJsonArray();
|
|||
|
|
for (final JsonElement elem : array) {
|
|||
|
|
list.add(GSON.fromJson(elem, cls));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
Logger.e("非法json字符串", e);
|
|||
|
|
}
|
|||
|
|
return list;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|