package com.copy.trade.util;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


public class JsonUtil {

	private static final Logger log = LoggerFactory.getLogger(JsonUtil.class);

	private static ObjectMapper mapper = new ObjectMapper();

	static {
		mapper.setSerializationInclusion(Include.NON_NULL);
		//设置输入时忽略JSON字符串中存在而Java对象实际没有的属性
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	}

	public static String object2Json(Object o) {
		if (o == null) {
			return null;
		}

		String s = null;
		try {
			s = mapper.writeValueAsString(o);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			WebAssert.throwException("sys.jackson.writeError");
		}
		return s;
	}

	public static <T> List<String> listObject2ListJson(List<T> objects) {
		if (objects == null) {
			return null;
		}

		List<String> lists = new ArrayList<String>();
		for (T t : objects) {
			lists.add(JsonUtil.object2Json(t));
		}

		return lists;
	}

	public static <T> List<T> listJson2ListObject(List<String> jsons, Class<T> c) {
		if (jsons == null) {
			return null;
		}

		List<T> ts = new ArrayList<>();
		for (String j : jsons) {
			ts.add(JsonUtil.json2Object(j, c));
		}

		return ts;
	}

	public static <T> T json2Object(String json, Class<T> c) {
		return json2Object(json, c, true);
	}

	public static <T> T json2Object(String json, Class<T> c, boolean throwError) {
		if (StringUtils.hasLength(json) == false) {
			return null;
		}

		T t = null;
		try {
			t = mapper.readValue(json, c);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			if(throwError){
				WebAssert.throwException("sys.jackson.json2objError");
			}
		}
		return t;
	}

	@SuppressWarnings("unchecked")
	public static <T> T json2Object(String json, TypeReference<T> tr) {
		if (StringUtils.hasLength(json) == false) {
			return null;
		}

		T t = null;
		try {
			t = mapper.readValue(json, tr);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			WebAssert.throwException("sys.jackson.json2objError");
		}
		return t;
	}
}
