class BeanSerializer {

    public static <T> T deserializeByType(Object src, Class<T> clazz) {
        if (src == null) {
            return (T) ClassUtils.getDefaultPrimitiveValue(clazz);
        } else if (src instanceof Boolean) {
            return (T) CompatibleTypeUtils.convert(src, clazz);
        } else if (src instanceof Number) {
            return (T) CompatibleTypeUtils.convert(src, clazz);
        } else if (src instanceof Map) { // map-->可能是map或者自定义对象
            Map srcMap = (Map) src;
            if (clazz == Object.class) { // 需要自省
                if (srcMap.containsKey(JSON.CLASS_KEY)) {
                    return (T) mapToObject(srcMap, Object.class); // 自定义对象
                } else {
                    return (T) mapToMap(srcMap, srcMap.getClass());
                }
            } else {
                if (Map.class.isAssignableFrom(clazz)) { // map转map
                    return (T) mapToMap(srcMap, (Class<? extends Map>) clazz);
                } else {
                    return mapToObject(srcMap, clazz); // 自定义对象
                }
            }
        } else if (src instanceof Collection) {
            Collection list = (Collection) src;
            if (clazz == Object.class) {
                return (T) collection2Collection(list, list.getClass(), Object.class);
            } else if (Collection.class.isAssignableFrom(clazz)) {
                return (T) collection2Collection(list, (Class<? extends Collection>) clazz, Object.class);
            } else if (clazz.isArray()) {
                if (clazz.getComponentType().isPrimitive()) {
                    return (T) CompatibleTypeUtils.convert(list, clazz);
                } else {
                    return (T) collectionToArray(list, clazz.getComponentType());
                }
            } else {
                return (T) list;
            }
        } else if (src.getClass().isArray()) {
            Class componentType = src.getClass().getComponentType();
            if (componentType.isPrimitive()) {
                if (Collection.class.isAssignableFrom(clazz)) {
                    return (T) arrayToCollection(src, (Class<? extends Collection>) clazz, Object.class);
                } else {
                    return (T) src;
                }
            } else {
                Object[] array = (Object[]) src;
                if (clazz == Object.class) {
                    return (T) array2Array(array, array.getClass().getComponentType());
                } else if (clazz.isArray()) {
                    return (T) array2Array(array, clazz.getComponentType());
                } else if (Collection.class.isAssignableFrom(clazz)) {
                    return (T) arrayToCollection(src, (Class<? extends Collection>) clazz, Object.class);
                } else {
                    return (T) src;
                }
            }
        } else if (clazz.isEnum()) { // 枚举 从字符串进行读取
            if (src instanceof String) {
                return (T) Enum.valueOf((Class<? extends Enum>) clazz, (String) src);
            } else {
                throw new RuntimeException("Enum field must set string!");
            }
        } else if (Date.class.isAssignableFrom(clazz)) { // 日期：支持long和标准格式字符串
            if (src instanceof Long) {
                return (T) new Date((Long) src);
            } else if (src instanceof String) {
                try {
                    return (T) DateUtils.strToDate((String) src);
                } catch (Exception e) {
                    throw new RuntimeException("Date field must set string(yyyy-MM-dd HH:mm:ss)!");
                }
            } else {
                throw new RuntimeException("Date field must set long or string(yyyy-MM-dd HH:mm:ss)!");
            }
        } else if (src instanceof String) { // 字符串支持转换
            return (T) CompatibleTypeUtils.convert(src, clazz);
        } else { // 其它返回src
            return (T) src;
        }
    }

}