public class JsonUtil{
/**
* 对象转换成字符串时,最大允许的字符串大小,单位为KB
*/
public static final int MAX_LOG_SIZE_K = 100;
public static final String DATE_FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";
private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class);
public static final ExclusionStrategy IGNORE_STRATEGY = new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return null != f.getAnnotation(Ignore.class);
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return null != clazz.getAnnotation(Ignore.class);
}
};
private static final SuperclassExclusionStrategy SUPERCLASS_EXCLUSION_STRATEGY = new SuperclassExclusionStrategy();
private JsonUtil() {
}
/**
* @return 对象的json字符串
*/
public static String toJSONString(Object object) {
// TODO 下面的重载方法稳定后改为直接调用下面的重载方法,maxSizeK取默认值,即方法体替换为下面这行代码
// return toJSONString(object, MAX_LOG_SIZE_K);
try {
Gson gson = getGson();
return gson.toJson(object);
} catch (Exception e) {
LOGGER.error("toJSONString err, object = {}", object, e);
return "";
}
}
/**
* 将对象转换为json字符串,并考虑字符串长度约束(优先调用上面的单参重载方法)
* @param object 原对象
* @param maxSizeK 返回的字符串最长大小,单位为KB,传null时不限制返回大小(打印日志时不建议传null)
* @return 对象对应的json字符串
*/
public static String toJSONString(Object object, Integer maxSizeK) {
try {
if (Objects.isNull(object)) {
return null;
}
// 转换为字符串
Gson gson = getGson();
String jsonStr = gson.toJson(object);
if (StringUtils.isEmpty(jsonStr) || (maxSizeK == null)) {
// 不用考虑字符串大小,直接返回
return jsonStr;
}
// 验证字符串是否超过大小限制(以KB为单位,小于1K的不考虑)
int jsonSizeK = jsonStr.getBytes(StandardCharsets.UTF_8).length / 1024;
if (jsonSizeK <= maxSizeK) {
// 大小未超出,直接返回
return jsonStr;
}
// 超大字符上传S3系统(异步),仅返回链接
String fileName = UUID.randomUUID().toString();
initLogS3();
String jsonUrl = logS3Util.asyncUploadString(logBucketInfo, fileName, jsonStr);
return "【字符串过大,已存入 " + jsonUrl + "】";
} catch (Exception e) {
LOGGER.error("对象转换为json字符串时发生异常", e);
return "【解析异常】";
}
}
/**
* 从json字符串提取出给定对象
* @return 解悉后的对象
*/
public static <T> T toObject(String jsonStr, Class<T> clazz) {
try {
Gson gson = getGson();
return gson.fromJson(jsonStr, clazz);
} catch (Exception e) {
LOGGER.error("toObject err, jsonStr = {}", jsonStr, e);
return null;
}
}
/**
* 从json字符串提取出给定对象
*
* @return 解悉后的对象
*/
public static <T> T toObjectByType(String jsonStr, Type typeOfT) {
try {
Gson gson = getGson();
return gson.fromJson(jsonStr, typeOfT);
} catch (Exception e) {
LOGGER.error("toObjectByType err, jsonStr = {}", jsonStr, e);
return null;
}
}
private static class DateTypeAdapter implements JsonDeserializer<Date> {
private DateFormat format;
DateTypeAdapter(DateFormat format) {
this.format = format;
}
@Override
public synchronized Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
if (!(json instanceof JsonPrimitive)) {
throw new JsonParseException("This is not a primitive value");
}
String jsonStr = json.getAsString();
try {
return format.parse(jsonStr);
} catch (ParseException e) {
return new Date(Long.parseLong(jsonStr));
}
}
}
public static <T> List<T> toList(String jsonStr, Class<T> clazz) {
Gson gson = getGson();
return gson.fromJson(jsonStr, new ParameterizedListType(clazz));
}
public static String toJSONString(Object object, Collection<String> includeKeys) {
Gson gson = getGsonWithFilter((key) -> !includeKeys.contains(key), null);
return gson.toJson(object);
}
public static <T> T toObject(String jsonStr, Class<T> clazz, Collection<String> includeKeys) {
Gson gson = getGsonWithFilter((key) -> !includeKeys.contains(key), null);
return gson.fromJson(jsonStr, clazz);
}
public static <T> T toObject(String jsonStr, Class<T> clazz, Function<String, Boolean> keyNameFilter,
Function<Class<?>, Boolean> clazzFilter) {
Gson gson = getGsonWithFilter(keyNameFilter, clazzFilter);
return gson.fromJson(jsonStr, clazz);
}
/**
* @param keyNameFilter 按照属性名称过滤
* @param clazzFilter 按照属性类型过滤
* @return
*/
private static Gson getGsonWithFilter(Function<String, Boolean> keyNameFilter,
Function<Class<?>, Boolean> clazzFilter) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT_STRING);
DateTypeAdapter dateTypeAdapter = new DateTypeAdapter(simpleDateFormat);
return new GsonBuilder().setDateFormat(DATE_FORMAT_STRING).registerTypeAdapter(Date.class, dateTypeAdapter)
.registerTypeAdapter(LocalTime.class, new JsonAdapter.LocalTimeDeserializer())
.registerTypeAdapter(LocalTime.class, new JsonAdapter.LocalTimeSerializer())
.setExclusionStrategies(IGNORE_STRATEGY, SUPERCLASS_EXCLUSION_STRATEGY, new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
if (keyNameFilter == null) {
return false;
}
return keyNameFilter.apply(f.getName());
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
if (clazzFilter == null) {
return false;
}
return clazzFilter.apply(clazz);
}
}).create();
}
/** with ignore */
private static Gson getGson() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT_STRING);
DateTypeAdapter dateTypeAdapter = new DateTypeAdapter(simpleDateFormat);
return new GsonBuilder().setDateFormat(DATE_FORMAT_STRING).registerTypeAdapter(Date.class, dateTypeAdapter)
.registerTypeAdapter(LocalTime.class, new JsonAdapter.LocalTimeDeserializer())
.registerTypeAdapter(LocalTime.class, new JsonAdapter.LocalTimeSerializer())
.setExclusionStrategies(IGNORE_STRATEGY, SUPERCLASS_EXCLUSION_STRATEGY).create();
}
private static class ParameterizedListType implements ParameterizedType {
Class clazz;
public ParameterizedListType(Class clz) {
clazz = clz;
}
@Override
public Type[] getActualTypeArguments() {
return new Type[] { clazz };
}
@Override
public Type getRawType() {
return List.class;
}
@Override
public Type getOwnerType() {
return null;
}
}
private static class SuperclassExclusionStrategy implements ExclusionStrategy {
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
@Override
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
String fieldName = fieldAttributes.getName();
Class<?> theClass = fieldAttributes.getDeclaringClass();
return isFieldInSuperclass(theClass, fieldName);
}
private boolean isFieldInSuperclass(Class<?> subclass, String fieldName) {
Class<?> superclass = subclass.getSuperclass();
while (superclass != null) {
Field field = getField(superclass, fieldName);
if (field != null) {
return true;
}
superclass = superclass.getSuperclass();
}
return false;
}
private Field getField(Class<?> theClass, String fieldName) {
try {
return theClass.getDeclaredField(fieldName);
} catch (Exception e) {
return null;
}
}
}
}
Map<Long, List<Integer>> map= new HashMap<>();
map.put(1L, Arrays.asList(123,124));
map.put(2L, Arrays.asList(223,224));
String s = JsonUtil.toJSONString(map);
// TypeToken<Map<Long, List<Integer>>> token = new TypeToken<Map<Long, List<Integer>>>() {};
Map<Long, List<Integer>> map2 = JsonUtil.toObjectByType(s, new TypeToken<Map<Long, ArrayList<Integer>>>() {}.getType());
gson工具
最新推荐文章于 2024-05-14 20:09:10 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)