package com.supServer.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.List;
import java.util.Map;
import java.util.Set;

@SuppressWarnings("unchecked")
public class JsonUtil {

    public static final SerializerFeature[] DEFAULT_SERIALIZE_FEATURES = {
            SerializerFeature.QuoteFieldNames,
            SerializerFeature.SkipTransientField,
            SerializerFeature.SortField,
            SerializerFeature.WriteEnumUsingToString,
            SerializerFeature.WriteMapNullValue
    };

    public static String toJSONString(Object obj) {
        return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue, SerializerFeature.QuoteFieldNames);
    }

    public static String toPrettyJSONString(Object obj) {
        return JSON.toJSONString(obj, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.QuoteFieldNames);
    }

    public static Map<String, Object> parse(String jsonText) {
        Map<String, Object> object = (Map<String, Object>) JSON.parse(jsonText);
        return object;
    }

    public static <T> T parse(String jsonText, Class<T> clazz) {
        T object = JSON.parseObject(jsonText, clazz);
        return object;
    }
   
	@SuppressWarnings("rawtypes")
	public static List<Map<String, Object>> parseArray(String jsonText) {
       	List<Map<String, Object>> result = (List) JSON.parseArray(jsonText);
       	return result;
	}
	
	public static <T> List<T> parseArray(String jsonText, Class<T> clazz) {
        List<T> result = JSON.parseArray(jsonText, clazz);
        return result;
    }
	
	/**
	 * json对象中key下划线转驼峰json字符串
	 * @param json
	 * @return
	 */
	public final static String parseLineToHump(Object obj) {
        Object o = JSON.toJSON(obj);
        convert(o);
        return toJSONString(o);
    }
	
	private final static void convert(Object json) {
        if (json instanceof JSONArray) {
            JSONArray arr = (JSONArray) json;
            for (Object obj : arr) {
                convert(obj);
            }
        } else if (json instanceof JSONObject) {
            JSONObject jo = (JSONObject) json;
            Set<String> keys = jo.keySet();
            Object[] array = keys.toArray(new Object[keys.size()]);
            for (Object key : array) {
                Object value = jo.get(key);
                String[] key_strs = key.toString().split("_");
                if (key_strs.length > 1) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < key_strs.length; i++) {
                        String ks = key_strs[i];
                        if (!"".equals(ks)) {
                            if (i == 0) {
                                sb.append(ks);
                            } else {
                                int c = ks.charAt(0);
                                if (c >= 97 && c <= 122) {
                                    int v = c - 32;
                                    sb.append((char) v);
                                    if (ks.length() > 1) {
                                        sb.append(ks.substring(1));
                                    }
                                } else {
                                    sb.append(ks);
                                }
                            }
                        }
                    }
                    jo.remove(key);
                    jo.put(sb.toString(), value);
                }
                convert(value);
            }
        }
    }
	
}
