方法 | 注释 |
---|
toJson | 将对象转换为 JSON String |
fromJson | 将 JSON String 映射为指定类型对象 |
isJSON | 判断字符串是否 JSON 格式 |
isJSONObject | 判断字符串是否 JSON Object 格式 |
isJSONArray | 判断字符串是否 JSON Array 格式 |
toJsonIndent | JSON String 缩进处理 |
getArrayType | 获取 Array Type |
getListType | 获取 List Type |
getSetType | 获取 Set Type |
getMapType | 获取 Map Type |
getType | 获取 Type |
package dev.other;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.util.ParameterizedTypeImpl;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Set;
import dev.utils.JCLogUtils;
public final class FastjsonUtils {
private FastjsonUtils() {
}
private static final String TAG = FastjsonUtils.class.getSimpleName();
public static String toJson(final Object object) {
if (object != null) {
try {
return JSON.toJSONString(object);
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "toJson");
}
}
return null;
}
public static <T> T fromJson(final String json, final Class<T> classOfT) {
if (json != null) {
try {
return JSON.parseObject(json, classOfT);
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "fromJson");
}
}
return null;
}
public static <T> T fromJson(final String json, final Type typeOfT) {
if (json != null) {
try {
return JSON.parseObject(json, typeOfT);
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "fromJson");
}
}
return null;
}
public static boolean isJSON(final String json) {
try {
Object object = JSON.parse(json);
return (object != null);
} catch (Exception e) {
return false;
}
}
public static boolean isJSONObject(final String json) {
try {
Object object = JSON.parse(json);
return (object instanceof JSONObject);
} catch (Exception e) {
}
return false;
}
public static boolean isJSONArray(final String json) {
try {
Object object = JSON.parse(json);
return (object instanceof JSONArray);
} catch (Exception e) {
}
return false;
}
public static String toJsonIndent(final String json) {
if (json != null) {
try {
Object object = JSON.parse(json, Feature.OrderedField);
if (object instanceof JSONObject) {
return JSONObject.toJSONString(object, true);
} else if (object instanceof JSONArray) {
return JSONArray.toJSONString(object, true);
}
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "toJsonIndent");
}
}
return null;
}
public static String toJsonIndent(final Object object) {
if (object != null) {
try {
return toJsonIndent(toJson(object));
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "toJsonIndent");
}
}
return null;
}
public static Type getArrayType(final Type type) {
return new GenericArrayType() {
@Override
public Type getGenericComponentType() {
return type;
}
};
}
public static Type getListType(final Type type) {
return new ParameterizedTypeImpl(new Type[]{type}, null, List.class);
}
public static Type getSetType(final Type type) {
return new ParameterizedTypeImpl(new Type[]{type}, null, Set.class);
}
public static Type getMapType(final Type keyType, final Type valueType) {
return new ParameterizedTypeImpl(new Type[]{keyType, valueType}, null, Map.class);
}
public static Type getType(final Type rawType, final Type... typeArguments) {
return new ParameterizedTypeImpl(typeArguments, null, rawType);
}
}