Java(反)序列化,处理特殊情况,尽力避免出现空指针、格式不准确等异常

Java(反)序列化,处理特殊情况,尽力避免出现空指针、格式不准确等异常

本文连接:https://www.cnblogs.com/muphy/p/15242173.html

 

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

public class JsonUtils {


    public static final DateTimeFormatter YYYY_MM_DD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter YYYY_MM_DD_HH_MM_SS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static <T> T parseObject(Class<T> paramType, String value) {
        if (Character.class == paramType || char.class == paramType) {
            if (StringUtils.isEmpty(value)) {
                return (T) (Object) '\0';
            }
            return (T) (Object) value.charAt(0);
        }
        if (CharSequence.class.isAssignableFrom(paramType)) {
            if (StringUtils.isEmpty(value)) {
                return (T) "";
            }
            return (T) value;
        }
        if (Boolean.class == paramType || boolean.class == paramType) {
            if (StringUtils.isEmpty(value)) {
                return (T)(Object)false;
            }
            return (T)(Object)"true".equalsIgnoreCase(value);
        }
        if (LocalDate.class.isAssignableFrom(paramType)) {
            if (StringUtils.isEmpty(value)) {
                return null;
            }
            if (value.contains(" ")) {
                return (T) LocalDate.parse(value, YYYY_MM_DD_HH_MM_SS);
            }
            return (T) LocalDate.parse(value, YYYY_MM_DD);
        }
        if (LocalDateTime.class.isAssignableFrom(paramType)) {
            if (StringUtils.isEmpty(value)) {
                return null;
            }
            if (value.contains(" ")) {
                return (T) LocalDateTime.parse(value, YYYY_MM_DD_HH_MM_SS);
            }
            return (T) LocalDateTime.parse(value, YYYY_MM_DD);
        }
        if (Date.class.isAssignableFrom(paramType)) {
            if (StringUtils.isEmpty(value)) {
                return null;
            }
            SimpleDateFormat dateFormat;
            if (value.contains(" ")) {
                dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            } else {
                dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            }
            try {
                return (T) dateFormat.parse(value);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
        if (Number.class.isAssignableFrom(paramType) || paramType.isPrimitive()) {
            if (StringUtils.isEmpty(value)) {
                value = "0";
            }
        } else if (Collection.class.isAssignableFrom(paramType) || paramType.isArray()) {
            if (StringUtils.isEmpty(value)) {
                value = "[]";
            }
        } else if (Map.class.isAssignableFrom(paramType)) {
            if (StringUtils.isEmpty(value)) {
                value = "{}";
            }
        } else if (StringUtils.isEmpty(value)) {
            value = "{}";
        }
        return JSON.parseObject(value, paramType);
    }

    public static String toJSONString(Object value) {
        if (value == null) {
            return "";
        }
        Class<?> paramType = value.getClass();
        if (Character.class == paramType) {
            return String.valueOf(value);
        }
        if (CharSequence.class.isAssignableFrom(paramType)) {
            return String.valueOf(value);
        }
        if (Number.class.isAssignableFrom(paramType) || paramType.isPrimitive()) {
            return String.valueOf(value);
        }
        if (LocalDate.class.isAssignableFrom(paramType)) {
            LocalDate localDate = (LocalDate) value;
            return localDate.format(YYYY_MM_DD);
        }
        if (LocalDateTime.class.isAssignableFrom(paramType)) {
            LocalDateTime localDate = (LocalDateTime) value;
            return localDate.format(YYYY_MM_DD_HH_MM_SS);
        }
        if (Date.class.isAssignableFrom(paramType)) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return dateFormat.format(value);
        }
        return JSON.toJSONString(value);
    }
}

 

posted @ 2021-09-08 12:08  明月心~  阅读(294)  评论(0)    收藏  举报