com.plumelog.core.util.GfJsonUtil Maven / Gradle / Ivy
package com.plumelog.core.util;
import com.alibaba.fastjson.JSON;
import java.util.*;
/**
* className:GfJsonUtil
* description:fastjson工具类
* time:2020-05-11.16:17
*
* @author Frank.chen
* @version 1.0.0
*/
public abstract class GfJsonUtil {
private GfJsonUtil() {
}
public static T parseObject(String json, Class clazz) {
if (json == null) {
return null;
}
return JSON.parseObject(json, clazz);
}
public static String toJSONString(T t) {
if (t == null) {
return null;
}
return JSON.toJSONString(t);
}
public static List parseList(Iterable jsonList, Class clazz) {
List retList = new ArrayList();
for (String json : jsonList) {
retList.add(parseObject(json, clazz));
}
return retList;
}
public static List> parseArrayList(Iterable jsonList, Class clazz) {
List> retList = new ArrayList>();
for (String json : jsonList) {
retList.add(parseArray(json, clazz));
}
return retList;
}
public static List parseArray(String json, Class clazz) {
if (json == null) {
return Collections.emptyList();
}
return JSON.parseArray(json, clazz);
}
public static Map parseMapByUid(List jsonList, List uidList, Class clazz, boolean isContainsNull) {
if (jsonList.size() != uidList.size()) {
return null;
}
Map uidMap = new HashMap(uidList.size());
for (int i = 0; i < uidList.size(); i++) {
T t = parseObject(jsonList.get(i), clazz);
if (isContainsNull || t != null) {
uidMap.put(uidList.get(i), t);
}
}
return uidMap;
}
public static Map> parseArrayMapByUid(List jsonList, List uidList, Class clazz, boolean isContainsNull) {
if (jsonList.size() != uidList.size()) {
return null;
}
Map> uidMap = new HashMap>(uidList.size());
for (int i = 0; i < uidList.size(); i++) {
List t = parseArray(jsonList.get(i), clazz);
if (isContainsNull || t != null) {
uidMap.put(uidList.get(i), t);
}
}
return uidMap;
}
}