对象泛型互换工具集

2023-09-05  本文已影响0人  Yluozi

package com.zznode.activation.commons.tools.api.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GsonUtils {
   
   private static final Gson gson = new GsonBuilder().serializeNulls().create();


   /**
    * <pre>
    * Json格式转换, 由JSON字符串转化到制定类型T
    * 
    * @param json
    * @param cls
    * @return T
    *
    * <pre>
    */
   public static <T> T fromJson(String json, Class<T> cls) {
       if (StringUtils.isBlank(json)) {
           return null;
       }
       return gson.fromJson(json, cls);
   }
   
   /**
    * <pre>
    * Json格式转换, 由JSON字符串转化到泛型的类型
    * 
    * @param json
    * @param typeOfT
    * @return T
    *
    * <pre>
    */
   public static <T> T fromJson(String json, Type typeOfT){
       return gson.fromJson(json, typeOfT);
   }

   /**
    * <pre>
    * java对象转化JSON
    *
    * @return String
    *
    * <pre>
    */
   public static String toJson(Object obj) {
       if (obj == null) {
           return "";
       }
       return gson.toJson(obj);
   }
   
   public static <T> ArrayList<T> jsonToList(String json, Class<T> classOfT) {
       Type type = new TypeToken<ArrayList<JsonObject>>(){}.getType();
       ArrayList<JsonObject> jsonObjs = gson.fromJson(json, type);
       
       ArrayList<T> listOfT = new ArrayList<>();
       for (JsonObject jsonObj : jsonObjs) {
           listOfT.add(gson.fromJson(jsonObj, classOfT));
       }
       
       return listOfT;
   }
   
   public static String getJsonObjectAsString(JsonObject jsonObject, String name) {
       if (jsonObject == null || StringUtils.isBlank(name)) {
           return null;
       }
       JsonElement jsonElement = jsonObject.get(name);
       return (jsonElement == null) ? null : jsonElement.getAsString();
   }

   public static JsonObject getJsonObjectChild(JsonObject jsonObject, String name) {
       if (jsonObject == null || StringUtils.isBlank(name)) {
           return null;
       }
       JsonElement jsonElement = jsonObject.get(name);
       return (jsonElement == null) ? null : jsonElement.getAsJsonObject();
   }

   public static boolean getJsonObjectAsBoolean(JsonObject jsonObject, String name) {
       if (jsonObject == null || StringUtils.isBlank(name)) {
           return false;
       }
       JsonElement jsonElement = jsonObject.get(name);
       return (jsonElement == null) ? false : jsonElement.getAsBoolean();
   }


 //遍历map
   public static  void blMap(Map map) {
       Set<Map.Entry<String, String>> entryseSet=map.entrySet();
       for (Map.Entry<String, String> entry:entryseSet) {
           System.out.println(entry.getKey()+","+entry.getValue());
       }
   }


   /**
        * 返回单个实体内指定key为空的属性key
        *
        * @param t   参数
        * @param <T> 类型
        * @return 验证结果
        */
   public static <T> String validateInList(T t) {
       StringBuilder sb = new StringBuilder();
       try {
           for (Field field :t.getClass().getDeclaredFields()) {
               field.setAccessible(true);
               sb.append(field.getName()+";");
           }
       }
       //抛出IllegalAccessException异常 抛出其他异常可能会出错
       catch (Exception e) {
           e.printStackTrace();
       }
       return sb.toString();
   }

/**
    * List对象转List<map>
    *
    * @param list
    * @return
    * @throws IllegalAccessException
    */
   public static List<Map<String, Object>> toMap(List<?> list) throws IllegalAccessException {
       List<Map<String, Object>> mapList = new ArrayList<>();
       for (Object o : list) {
           Map<String, Object> map = objToMap(o);
           mapList.add(map);
       }
       return mapList;
   }

   /**
    * 对象转map
    *
    * @param object
    * @return
    * @throws IllegalAccessException
    */
   public static Map<String, Object> objToMap(Object object) throws IllegalAccessException {
       Map<String, Object> map = new HashMap<>();
       Field[] fields = object.getClass().getDeclaredFields();
       for (Field field : fields) {
           field.setAccessible(true);
           map.put(field.getName(), field.get(object));
       }
       return map;
   }

   public static <T> List<Map<String, T>> jsonToListMaps(String gsonString) {
       List<Map<String, T>> list = null;
       list = gson.fromJson(gsonString, new TypeToken<List<Map<String, T>>>() {
       }.getType());
       return list;
   }

   public static <T> Map<String, T> jsonToMaps(String gsonString) {
       Map<String, T> map = null;
       map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
       }.getType());
       return map;
   }


}



上一篇下一篇

猜你喜欢

热点阅读