package cn.com.qmth.examcloud.commons.util; import java.lang.reflect.Type; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import com.google.common.collect.Maps; import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import cn.com.qmth.examcloud.commons.util.DateUtil.DatePatterns; /** * json tool * * @author WANGWEI * @param * @date 2019年1月16日 * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved. */ public class JsonUtil { /** * to json * * @author WANGWEI * @param obj * @return */ public static String toJson(Object obj) { GsonBuilder builder = new GsonBuilder().disableHtmlEscaping().serializeNulls() .setDateFormat(DatePatterns.CHINA_DEFAULT); return builder.create().toJson(obj); } /** * 方法注释 * * @author WANGWEI * @param obj * @param excludeFields * @return */ public static String toJson(Object obj, String... excludeFields) { GsonBuilder builder = new GsonBuilder().disableHtmlEscaping().serializeNulls() .setDateFormat(DatePatterns.CHINA_DEFAULT); builder.setExclusionStrategies(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { for (String field : excludeFields) { if (f.getName().matches(field)) { return true; } } return false; } @Override public boolean shouldSkipClass(Class clazz) { return false; } }); return builder.create().toJson(obj); } /** * to pretty json * * @author WANGWEI * @param obj * @return */ public static String toPrettyJson(Object obj) { GsonBuilder builder = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting() .serializeNulls().setDateFormat(DatePatterns.CHINA_DEFAULT); return builder.create().toJson(obj); } /** * json转对象 * * @author WANGWEI * @param json * @param c * @return */ public static T fromJson(String json, Class c) { Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting() .setDateFormat(DatePatterns.CHINA_DEFAULT).create(); return gson.fromJson(json, c); } /** * json数组转list * * @author WANGWEI * @param json * @param elementType * @return */ public static List fromJsonArray(String json, Class elementType) { Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting() .setDateFormat(DatePatterns.CHINA_DEFAULT).create(); T[] array = gson.fromJson(json, elementType); return Arrays.asList(array); } /** * simple json to map * * @author WANGWEI * @param s * @return */ public static Map json2Map(String s) { JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(s); JsonObject jsonObject = jsonElement.getAsJsonObject(); Set> entrySet = jsonObject.entrySet(); Map map = Maps.newHashMap(); for (Entry entry : entrySet) { JsonElement e = entry.getValue(); if (!e.isJsonNull()) { String v = e.getAsString(); map.put(entry.getKey(), v); } } return map; } /** * json转对象 * * @author WANGWEI * @param json * @param type * @return */ public static T fromJson(String json, Type type) { Gson gson = new GsonBuilder().disableHtmlEscaping() .setDateFormat(DatePatterns.CHINA_DEFAULT).create(); return gson.fromJson(json, type); } }