package com.yss.framework.api.util;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.MapType;
import org.codehaus.jackson.map.type.TypeFactory;
import org.codehaus.jackson.type.TypeReference;
import com.yss.framework.api.common.co.User;
import com.yss.mvc.utils.ParameterUtil;
public class JsonUtil {
/* JACKSON中对象,用来java与jackson之间的转换 */
private static ObjectMapper mapper;
static {
mapper = new ObjectMapper();
mapper.setDeserializationConfig(mapper.getDeserializationConfig()
.withDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
mapper.setSerializationConfig(mapper.getSerializationConfig()
.withDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
// 序列化时忽略无法序列化的对象
mapper.configure(org.codehaus.jackson.map.SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
// 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
mapper
.configure(
org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
// //禁止使用int代表Enum的order()来反序列化Enum,非常危险
// mapper.configure(DeserializationConfig.Feature.FAIL_ON_NUMBERS_FOR_ENUMS,
// true);
}
/**
* 将JSON字符串转换成JavaBEAN对象
*
* @param <T>
* Bean类型
* @param jsonString
* 需要转换的json数据
* @param clazz
* Bean类型
* @return bean对象
* @throws Exception
*/
public static <T extends Object> T toBean(String jsonString, Class<T> clazz) {
T obj = null;
try {
obj = mapper.readValue(jsonString, clazz);
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex.getCause());
}
return obj;
}
/**
* 20150630 刘志勇 STORY #24186 将系统本地化缓存改为集中式缓存 为redis提供的使用TypeReference转换对象的方法
* @param jsonString
* @param typeReference
* @return
*/
public static <T extends Object> T toBean(String jsonString, TypeReference<?> typeReference) {
T obj = null;
try {
obj = mapper.readValue(jsonString, typeReference);
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex.getCause());
}
return obj;
}
/**
* 将JSON字符串转换成JavaBEAN对象
*
* @param <T>
* Bean类型
* @param jsonString
* 需要转换的json数据
* @param clazz
* Bean类型
* @return bean对象
* @throws Exception
*/
public static Object toBeanNoGeneric(String jsonString, Class<?> clazz) {
Object obj = null;
try {
obj = mapper.readValue(jsonString, clazz);
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex.getCause());
}
return obj;
}
@SuppressWarnings("unchecked")
public static <T extends Object> T toBeanWithSuperClass(String jsonString,
Class<T> clazz) {
T obj = null;
HashMap<String, Object> fieldValueMap = new HashMap<String, Object>();
Field[] allDeclareField;
try {
fieldValueMap = mapper.readValue(jsonString, HashMap.class);
allDeclareField = ReflectionUtil
.getAllDeclaredFieldsWithOutBaseBean(clazz);
obj = clazz.newInstance();
Object fieldValueObj;
for (Field field : allDeclareField) {
fieldValueObj = fieldValueMap.get(field.getName());
if(fieldValueObj!=null){
field.setAccessible(true);
field.set(obj, ParameterUtil.Convert(fieldValueObj.toString(), field.getType()));
field.setAccessible(true);
}
}
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex.getCause());
}
return obj;
}
public static String toString(Object obj) {
String jsonStr = "";
try {
jsonStr = mapper.writeValueAsString(obj);
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex.getCause());
}
return jsonStr;
}
public static String toString(List<?> list) {
String json = ""; // 转换后的字符串
if (list == null || list.size() == 0) {
return json;
}
try {
json = mapper.writeValueAsString(list);
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex.getCause());
}
return json;
}
public static <T extends Object> List<T> toList(String json, Class<T> clazz) {
List<T> list = new ArrayList<T>();
TypeFactory factory = TypeFactory.defaultInstance();
try {
if(json != null && !json.equals("")){
list = mapper.readValue(json, factory.constructCollectionType(
ArrayList.class, clazz));
}
else{
throw new RuntimeException("json不能为空串。");
}
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex.getCause());
}
return list;
}
public static <V> Map<String,V> toMap(String json,Class<V> clazz){
return toMap(json,String.class,clazz);
}
public static <K, V> Map<K, V> toMap(String json, Class<K> key,Class<V> value){
Map<K, V> map = null;
try {
TypeFactory typeFactory = mapper.getTypeFactory();
MapType mapType = typeFactory.constructMapType(HashMap.class, key, value);
map = mapper.readValue(json, mapType);
} catch (IOException ex) {
throw new RuntimeException(ex.getMessage(), ex.getCause());
}
return map;
}
}