【utils】基于jackson的解析转换JsonUtil

jackson version: 2.8.6

/**
 * @description json工具类,依赖jackson
 * @author paul
 * @date 2017年7月10日 上午10:54:43 
 * @update 2017年7月10日 上午10:54:43 
 * @version V1.0
 */
public class JsonUtil {
    private static ObjectMapper INSTANCE = new ObjectMapper();

    private JsonUtil() {}

    /**
     * @param obj 准备转换对象
     * @return
     * @description 对象转换成json字符串
     * @author paul
     * @date 2017年7月10日 上午10:54:50
     * @update 2017年7月10日 上午10:54:50
     * @version V1.0
     * @throws JsonProcessingException 
     */
    public static String toJsonStr(Object obj) {
        try {
            return INSTANCE.writeValueAsString(obj);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param json 准备转换json
     * @param type 转换类型
     * @return
     * @throws Exception 转换异常
     * @description json字符串转换成对象
     * @author paul
     * @date 2017年7月10日 上午11:08:34
     * @update 2017年7月10日 上午11:08:34
     * @version V1.0
     */
    @SuppressWarnings("unchecked")
    public static <T> T parseJson(String json, String type) {
        try {
            return (T) parseJson(json, Class.forName(type));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param json 准备转换json
     * @param clazz 转换类型
     * @return
     * @description json字符串转换成对象
     * @author paul
     * @date 2017年7月10日 上午11:12:58
     * @update 2017年7月10日 上午11:12:58
     * @version V1.0
     */
    public static <T> T parseJson(String json, Class<T> clazz) {
        try {
            return (T) INSTANCE.readValue(json, clazz);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param json 准备转换json
     * @param clazz 集合元素类型
     * @return
     * @description json字符串转换成对象集合
     * @author paul
     * @date 2017年8月12日 下午1:28:27
     * @update 2017年8月12日 下午1:28:27
     * @version V1.0
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> parseJsonList(String json, Class<T> clazz) {
        try {
            JavaType javaType = getCollectionType(ArrayList.class, clazz); 
            return (List<T>) INSTANCE.readValue(json, javaType);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param collectionClass 集合类
     * @param elementClasses 集合元素类
     * @return
     * @description 获取泛型的ColloectionType
     * @author paul
     * @date 2017年8月12日 下午2:17:38
     * @update 2017年8月12日 下午2:17:38
     * @version V1.0
     */
    private static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {   
        return INSTANCE.getTypeFactory().constructParametricType(collectionClass, elementClasses);   
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值