DevUtils Github
Gson 工具类 -> GsonUtils.java
方法注释
toJson将对象转换为 JSON StringfromJson将 JSON String 映射为指定类型对象isJSON判断字符串是否 JSON 格式toJsonIndentJSON String 缩进处理createGson创建 GsonBuildercreateGsonExcludeFields创建过滤指定修饰符字段 GsonBuildergetArrayType获取 Array TypegetListType获取 List TypegetSetType获取 Set TypegetMapType获取 Map TypegetType获取 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();
}
}
转载请注明原文地址: https://mac.8miu.com/read-485039.html