方法 | 注释 |
---|
toJson | 将对象转换为 JSON String |
fromJson | 将 JSON String 映射为指定类型对象 |
isJSON | 判断字符串是否 JSON 格式 |
toJsonIndent | JSON String 缩进处理 |
createGson | 创建 GsonBuilder |
createGsonExcludeFields | 创建过滤指定修饰符字段 GsonBuilder |
getArrayType | 获取 Array Type |
getListType | 获取 List Type |
getSetType | 获取 Set Type |
getMapType | 获取 Map Type |
getType | 获取 Type |
package dev.other;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import java.io.StringReader;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Set;
import dev.utils.JCLogUtils;
public final class GsonUtils {
private GsonUtils() {
}
private static final String TAG = GsonUtils.class.getSimpleName();
private static final Gson TO_GSON = createGson(true).create();
private static final Gson FROM_GSON = createGson(true).create();
private static final Gson INDENT_GSON = createGson(true).setPrettyPrinting().create();
public static String toJson(final Object object) {
return toJson(object, TO_GSON);
}
public static String toJson(final Object object, final Gson gson) {
if (gson != null) {
try {
return gson.toJson(object);
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "toJson");
}
}
return null;
}
public static <T> T fromJson(final String json, final Class<T> classOfT) {
return fromJson(json, classOfT, FROM_GSON);
}
public static <T> T fromJson(final String json, final Class<T> classOfT, final Gson gson) {
if (gson != null) {
try {
return gson.fromJson(json, classOfT);
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "fromJson");
}
}
return null;
}
public static <T> T fromJson(final String json, final Type typeOfT) {
return fromJson(json, typeOfT, FROM_GSON);
}
public static <T> T fromJson(final String json, final Type typeOfT, final Gson gson) {
if (gson != null) {
try {
return gson.fromJson(json, typeOfT);
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "fromJson");
}
}
return null;
}
public static boolean isJSON(final String json) {
JsonElement jsonElement;
try {
jsonElement = new JsonParser().parse(json);
} catch (Exception e) {
return false;
}
if (jsonElement == null) {
return false;
}
if (!jsonElement.isJsonObject()) {
return false;
}
return true;
}
public static String toJsonIndent(final String json) {
return toJsonIndent(json, INDENT_GSON);
}
public static String toJsonIndent(final String json, final Gson gson) {
if (gson != null) {
try {
JsonReader reader = new JsonReader(new StringReader(json));
reader.setLenient(true);
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(reader);
return gson.toJson(jsonElement);
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "toJsonIndent");
}
}
return null;
}
public static String toJsonIndent(final Object object) {
return toJsonIndent(object, INDENT_GSON);
}
public static String toJsonIndent(final Object object, final Gson gson) {
if (gson != null) {
try {
return gson.toJson(object);
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "toJsonIndent");
}
}
return null;
}
public static GsonBuilder createGson(final boolean serializeNulls) {
GsonBuilder builder = new GsonBuilder();
if (serializeNulls) builder.serializeNulls();
return builder;
}
public static GsonBuilder createGsonExcludeFields(final GsonBuilder builder, final int... modifiers) {
if (builder != null) {
return builder.excludeFieldsWithModifiers(modifiers);
}
return null;
}
public static Type getArrayType(final Type type) {
return TypeToken.getArray(type).getType();
}
public static Type getListType(final Type type) {
return TypeToken.getParameterized(List.class, type).getType();
}
public static Type getSetType(final Type type) {
return TypeToken.getParameterized(Set.class, type).getType();
}
public static Type getMapType(final Type keyType, final Type valueType) {
return TypeToken.getParameterized(Map.class, keyType, valueType).getType();
}
public static Type getType(final Type rawType, final Type... typeArguments) {
return TypeToken.getParameterized(rawType, typeArguments).getType();
}
}