package com.idea4j.framework.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

/**
 * json utility class (fastjson)
 *
 * @author andaicheng
 */
public class JsonUtil {

    private static final SerializeConfig SERIALIZE_CONFIG;
    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static final SerializerFeature[] SERIALIZER_FEATURE = {
            SerializerFeature.WriteMapNullValue,    // 输出空置字段
            SerializerFeature.WriteNullStringAsEmpty,   //字符类型字段如果为null，输出为""，而不是null
            SerializerFeature.WriteNullNumberAsZero,    //数值字段如果为null，输出为0，而不是null
            SerializerFeature.WriteNullBooleanAsFalse,  //Boolean字段如果为null，输出为false，而不是null
            SerializerFeature.WriteNullListAsEmpty  //list字段如果为null，输出为[]，而不是null
    };

    static {
        SERIALIZE_CONFIG = new SerializeConfig();
        SERIALIZE_CONFIG.put(java.util.Date.class, new SimpleDateFormatSerializer(DATE_FORMAT));
        SERIALIZE_CONFIG.put(java.sql.Date.class, new SimpleDateFormatSerializer(DATE_FORMAT));
    }

    private JsonUtil() {
    }

    public static String toJSONString(Object o) {
        return JSON.toJSONString(o, SERIALIZE_CONFIG, SERIALIZER_FEATURE);
    }

    public static <T> T parseObject(String json, Class<T> clazz) {
        return JSON.parseObject(json, clazz);
    }
}

@@ -906,16 +906,17 @@
 tAsEmpty
+,
   //list
@@ -935,24 +935,96 @@
 %E4%B8%BA%5B%5D%EF%BC%8C%E8%80%8C%E4%B8%8D%E6%98%AFnull%0A
+            SerializerFeature.DisableCircularReferenceDetect //%E5%85%B3%E9%97%AD%E5%BE%AA%E7%8E%AF%E5%BC%95%E7%94%A8%E6%A3%80%E6%9F%A5%0A
     %7D;%0A%0A    

optimize JsonUtil