package cn.util.string;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import cn.constant.CharsetKit;
import cn.util.abs.DataTypeAdapter;
import cn.util.abs.MapDeserializerDoubleAsInt;
import cn.util.core.IsKit;
import cn.util.core.LogKit;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/** JSON对象：{}； Json数组：[{},{}]*/
public class JsonKit <T>
{
	private final static ObjectMapper objectMapper = new ObjectMapper();

	public static Map<String, Object> json2Map_jackson(String jsonObjStr) 
	{
		Map<String, Object> result = new HashMap<>();
		if(IsKit.isNullOrEmpty(jsonObjStr)){
			return result;
		}
		JavaType javaType = objectMapper.getTypeFactory().constructMapType(Map.class,String.class,Object.class);
		try {
			result = objectMapper.readValue(jsonObjStr,javaType);
		} catch (IOException e) {
			LogKit.info(e);
		}
		return result;
	}
	/**将json数据转换成pojo对象list */
	public List<T> jsonToList_jackson(String jsonData, Class<T> beanType)
	{
		JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, beanType);
		try {
			return objectMapper.readValue(jsonData, javaType);
		} catch (Exception e) {
			LogKit.info(e);
		}
		return null;
	}

	/**从一个JSON 对象字符格式中得到一个java对象.参数含有[]报错*/
	public static <T> T json2Bean_jackson(String jsonData, Class<T> beanType) {
		try {
			return objectMapper.readValue(jsonData, beanType);
		} catch (Exception e) {
			LogKit.info(e);
		}
		return null;
	}
	/**从一个JSON对象字符格式中得到一个java对象.参数含有[]没有问题*/
	public static <T> T json2Bean_fast(JSONObject jsonData, Class<T> beanType) 
	{
		if(IsKit.isNotNull(jsonData)){
			return JSONObject.parseObject(jsonData.toString(),beanType);
		}
		else {
			return null;
		}
	}
	
	/** 解决Gson转换错误导致Int变为Double类型 */
	public static List<Map<String, Object>> jsonToList_gson(String jsonArry) 
	{ 
		if(IsKit.isNullOrEmpty(jsonArry)){
			return null;
		}
		jsonArry = jsonArry.replaceAll(CharsetKit.SYMBOL_HASH,CharsetKit.SPACE);
		Type type = new TypeToken<List<Map<String, Object>>>(){}.getType();//List<T> 
		Gson gson = new GsonBuilder().registerTypeAdapter(type,new DataTypeAdapter()).create();
		return gson.fromJson(jsonArry,type);
	}

	/**解决Int变为Double类型 */
	public static List<Map<String, Object>> json2List_gson(String jsonArry) 
	{ 
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.registerTypeAdapter(new TypeToken<Map <String, Object>>(){}.getType(),new MapDeserializerDoubleAsInt());
		Gson gson = gsonBuilder.create();
		return gson.fromJson(jsonArry, new TypeToken<List<Map<String, Object>>>(){}.getType() );
	}

	
	//Object转Map
	@SuppressWarnings("unchecked")
	public static Map<String, Object> jsonObjToMap(Object obj) 
	{   //Linked
		Map<String, Object> map = new LinkedHashMap<>();
		ObjectMapper mapper = new ObjectMapper();
		try {
			map = mapper.readValue(mapper.writeValueAsString(obj), Map.class);
		} catch (Exception e) {
			LogKit.info(e);
		}
		return map;
	}
	/**实体 转map*/
	public static Map<String, Object> entityToMap(Object obj)
	{
		Map<String, Object> map = new HashMap<>();
		Class<?> clazz = obj.getClass();
		for(Field field : clazz.getDeclaredFields()){
			field.setAccessible(true);
			String fieldName = field.getName();
			Object object = null;
			try {
				object = field.get(obj);
			} catch (IllegalArgumentException | IllegalAccessException e) {
				LogKit.info(e);
			}
			map.put(fieldName, object);
		}
		return map;
	}
}
