123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- 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 <T>
- * @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> T fromJson(String json, Class<T> 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 <T> List<T> fromJsonArray(String json, Class<T[]> 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<String, String> json2Map(String s) {
- JsonParser parser = new JsonParser();
- JsonElement jsonElement = parser.parse(s);
- JsonObject jsonObject = jsonElement.getAsJsonObject();
- Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
- Map<String, String> map = Maps.newHashMap();
- for (Entry<String, JsonElement> 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> T fromJson(String json, Type type) {
- Gson gson = new GsonBuilder().disableHtmlEscaping()
- .setDateFormat(DatePatterns.CHINA_DEFAULT).create();
- return gson.fromJson(json, type);
- }
- }
|