com.plumelog.core.util.GfJsonUtil Maven / Gradle / Ivy
The newest version!
package com.plumelog.core.util;
//import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
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;
// }
private static final ObjectMapper mapper = new ObjectMapper();
public static T parseObject(String jsonString, Class classz) {
ObjectMapper mapper = new ObjectMapper();
try {
return (T) mapper.readValue(jsonString, classz);
} catch (JsonProcessingException e) {
}
return null;
}
public static String toJSONString(Object object) {
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
}
return null;
}
public static List parseArray(String json, Class clazz) {
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, clazz);
try {
return mapper.readValue(json, javaType);
} catch (Exception e) {
}
return null;
}
public static Map json2Map(String jsonData, Class keyType, Class valueType) {
JavaType javaType = mapper.getTypeFactory().constructMapType(Map.class, keyType, valueType);
try {
return mapper.readValue(jsonData, javaType);
} catch (Exception e) {
}
return null;
}
}