package com.xydl.cac.util;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class JSONProcessor {


	public Map<String, Object> fromJSON2Map(String json) throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		@SuppressWarnings("unchecked")
		Map<String, Object> productMap = mapper.readValue(json, Map.class);
		return productMap;
	}

	public Map<String, Object> fromJSON2Map(Object obj) throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		@SuppressWarnings("unchecked")
		Map<String, Object> productMap = mapper.convertValue(obj, Map.class);
		return productMap;
	}

	public String buildJSONFromJSONObject(Object obj) {
		String jsonInString = null;
		ObjectMapper mapper = new ObjectMapper();
		try {
			mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
			mapper.setSerializationInclusion(Include.NON_NULL);
			jsonInString = mapper.writeValueAsString(obj);
		} catch (Exception e) {
//			LOG.error("JSON transform failed. ", e);
		}
		return jsonInString;
	}

	public String buildJSONFromJSONObject(Object obj, boolean prettyPrinter) {
		String jsonInString = null;
		ObjectMapper mapper = new ObjectMapper();
		try {
			mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
			mapper.setSerializationInclusion(Include.NON_NULL);
			if (prettyPrinter) {
				jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
			} else {
				jsonInString = mapper.writeValueAsString(obj);
			}
		} catch (Exception e) {
//			LOG.error("JSON transform failed. ", e);
		}
		return jsonInString;
	}

	public <T> T buildJSONObjectFromJSON(String json, Class<T> clazz) throws Exception {
		ObjectMapper mapper = new ObjectMapper();
		try {
			mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			return mapper.readValue(json, clazz);
		} catch (UnrecognizedPropertyException e) {
			throw new Exception(e.getPropertyName(), e);
		} catch (Exception e) {
			throw new Exception("JSON Object transform failed. ", e);
		}
	}

	public <T> T buildJSONObjectFromJSON(File file, Class<T> clazz) throws Exception {
		ObjectMapper mapper = new ObjectMapper();
		try {
			mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			return mapper.readValue(file, clazz);
		} catch (UnrecognizedPropertyException e) {
			throw new Exception(e.getPropertyName(), e);
		} catch (Exception e) {
			throw new Exception("JSON Object transform failed. ", e);
		}
	}
}
