package com.iformall.utils;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public final class JsonUtil {
    private static Logger log = LoggerFactory.getLogger(JsonUtil.class);
    public static final ObjectMapper mapper = new ObjectMapper();

    private JsonUtil() {
    }

    public static String obj2Json(Object obj) {
        if(obj != null) {
            try {
                return mapper.writeValueAsString(obj);
            } catch (JsonProcessingException var2) {
                throw new RuntimeException(var2);
            }
        } else {
            return null;
        }
    }

    public static byte[] obj2Byte(Object obj) {
        if(obj != null) {
            try {
                return mapper.writeValueAsBytes(obj);
            } catch (JsonProcessingException var2) {
                var2.printStackTrace();
            }
        }

        return new byte[0];
    }

    public static Object readValue(String json, Class<?> clazz) {
        Object t = null;

        try {
            return mapper.readValue(json, clazz);
        } catch (Exception var4) {
            log.info("解析对象:{}", json);
            log.error("json解析错误:{}", var4.getMessage(), var4);
            return t;
        }
    }

//    public static <T> T readValue(String json, TypeReference type) {
//        Object object = null;
//
//        try {
//            object = mapper.readValue(json, type);
//        } catch (Exception var4) {
//            log.info("解析对象:{}", json);
//            log.error("json解析错误:{}", var4.getMessage(), var4);
//        }
//
//        return object;
//    }
//
//    public static <T> T readValue(byte[] json, TypeReference type) {
//        Object object = null;
//
//        try {
//            object = mapper.readValue(json, type);
//        } catch (Exception var4) {
//            log.info("解析对象:{}", json);
//            log.error("json解析错误:{}", var4.getMessage(), var4);
//        }
//
//        return object;
//    }
//
//    public static Map<String, Object> readValueAsMap(String json) {
//        return (Map)readValue(json, new TypeReference<HashMap<String, Object>>() {
//        });
//    }

    static {
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        SimpleModule module = new SimpleModule();
        module.addSerializer(HashMap.class, new JsonUtil.HashMapSerializer());
        mapper.registerModule(module);
    }

    public static class HashMapSerializer extends JsonSerializer<HashMap> {
        public HashMapSerializer() {
        }

        public void serialize(HashMap value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            jgen.writeStartObject();
            Iterator var4 = value.entrySet().iterator();

            while(var4.hasNext()) {
                Object entry = var4.next();
                if(entry instanceof Map.Entry) {
                    Map.Entry en = (Map.Entry)entry;
                    if(en.getValue() != null && en.getKey() != null) {
                        jgen.writeObjectField(en.getKey().toString(), en.getValue());
                    }
                }
            }

            jgen.writeEndObject();
        }
    }
}
