基于Jackson的jsonUtil


import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

    public JsonUtil() {
    }

    public static String toJsonString(Object o) {
        try {
            return toJsonStringWithException(o);
        } catch (JsonProcessingException var2) {
            log.warn("序列化发生错误", var2);
            return "";
        }
    }

    public static String toJsonStringWithException(Object o) throws JsonProcessingException {
        return Objects.isNull(o) ? null : objectMapper.writeValueAsString(o);
    }

    public static <T> T parseObject(byte[] body, Class<T> clazz) {
        try {
            return null == body ? null : objectMapper.readValue(body, clazz);
        } catch (IOException var3) {
            log.warn("序列化发生错误", var3);
            throw getThrowException();
        }
    }

    public static <T> T parseObject(String body, Class<T> clazz) {
        try {
            return null == body ? null : objectMapper.readValue(body, clazz);
        } catch (IOException var3) {
            log.warn("序列化发生错误", var3);
            throw getThrowException();
        }
    }

    public static <T> T parseObject(byte[] body, final Type type) {
        try {
            return null == body ? null : objectMapper.readValue(body, new TypeReference<T>() {
                public Type getType() {
                    return type;
                }
            });
        } catch (JsonProcessingException var3) {
            log.warn("序列化发生错误", var3);
            throw getThrowException();
        } catch (IOException var4) {
            log.warn("数据读取发生错误", var4);
            throw getThrowException();
        }
    }

    public static <T> T parseObject(String body, final Type type) {
        try {
            return null == body ? null : objectMapper.readValue(body, new TypeReference<T>() {
                public Type getType() {
                    return type;
                }
            });
        } catch (JsonProcessingException var3) {
            log.warn("序列化发生错误", var3);
            throw getThrowException();
        }
    }

    public static <T> List<T> parseArray(String body, Class<T> clazz) {
        try {
            return (List)objectMapper.readValue(body, objectMapper.getTypeFactory().constructParametricType(List.class, new Class[]{clazz}));
        } catch (JsonProcessingException var3) {
            log.warn("序列化发生错误", var3);
            throw getThrowException();
        }
    }

    public static <T> byte[] toJsonBytes(T data) {
        try {
            return null == data ? new byte[0] : objectMapper.writeValueAsBytes(data);
        } catch (JsonProcessingException var2) {
            log.warn("序列化发生错误", var2);
            throw getThrowException();
        }
    }

    private static boolean blankBody(String body) {
        return null == body || body.length() == 0;
    }

    private static RuntimeException getThrowException() {
        return new RuntimeException("json处理发生异常,请检查数据");
    }

    static {
// 忽略null打印日志
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值