springboot配置json数据序列化工具类

一、概述

public class FastJsonUtils {

    private static final SerializeConfig config;

    static {

        config = new SerializeConfig();

		config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式

		config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式

    }

    /**
     * 输出json字符串的属性要求,如果方法中未加入此属性则json格式按照默认的格式输出
     */
    private static final SerializerFeature[] features = {

            // 输出空置字段
            SerializerFeature.WriteMapNullValue,

            // list字段如果为null,输出为[],而不是null
            SerializerFeature.WriteNullListAsEmpty,

            // 数值字段如果为null,输出为0,而不是null
            SerializerFeature.WriteNullNumberAsZero,

            // Boolean字段如果为null,输出为false,而不是null
            SerializerFeature.WriteNullBooleanAsFalse,

            // 字符类型字段如果为null,输出为"",而不是null
            SerializerFeature.WriteNullStringAsEmpty,

            // 禁止循环引用
            SerializerFeature.DisableCircularReferenceDetect,

    };

    /**
     *
     * @Function: FastJsonUtils.java
     * @Description: object转json
     * @version: v1.0.0
     * @date: 2018年9月11日 上午9:12:53
     */

    public static String objectToJSON(Object obj) {

        return JSON.toJSONString(obj, config, features);

    }

    /**
     *
     * @Function: FastJsonUtils.java
     * @Description: object转json不带Features
     * @version: v1.0.0
     * @date: 2018年9月11日 上午9:13:22
     */
    public static String toJSONNoFeatures(Object obj) {

        return JSON.toJSONString(obj, config);

    }

    /**
     *
     * @Function: FastJsonUtils.java
     * @Description: json转object
     * @version: v1.0.0
     * @date: 2018年9月11日 上午9:12:25
     */
    public static Object stringToBean(String text) {

        return JSON.parse(text);

    }

    /**
     * json转泛类
     * @param text
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T stringToBean(String text, Class<T> clazz) {

        return JSON.parseObject(text, clazz);

    }

    /**
     *
     * @Function: FastJsonUtils.java
     * @Description: json转object数组
     * @version: v1.0.0
     * @date: 2018年9月11日 上午9:15:57
     */
    public static <T> Object[] toArray(String text) {

        return toArray(text, null);

    }

    /**
     *
     * @Function: FastJsonUtils.java
     * @Description: json转泛数组
     * @version: v1.0.0
     * @date: 2018年9月11日 上午9:15:52
     */
    public static <T> Object[] toArray(String text, Class<T> clazz) {

        return JSON.parseArray(text, clazz).toArray();

    }

    /**
     *
     * @Function: FastJsonUtils.java
     * @Description: json转list
     * @version: v1.0.0
     * @date: 2018年9月11日 上午9:15:47
     */
    public static <T> List<T> toList(String text, Class<T> clazz) {

        return JSON.parseArray(text, clazz);

    }





    /**
     *
     * @Function: FastJsonUtils.java
     * @Description: 字符串转map
     * @version: v1.0.0
     * @date: 2018年9月11日 上午9:17:19
     */
    public static <K, V> Map<K, V> stringToCollect(String s) {

        Map<K, V> m = (Map<K, V>) JSONObject.parseObject(s);

        return m;

    }


    /**
     *
     * @Function: FastJsonUtils.java
     * @Description: 该函数的功能描述
     * @version: v1.0.0
     * @date: 2018年9月11日 上午9:18:06
     */
    public static Object convertJsonToObject(String jsonData, Class<?> clazz) {

        return JSONObject.parseObject(jsonData, clazz);

    }

    public static Object convertJSONToObject(String content, Class<?> clazz) {

        return JSONObject.parseObject(content, clazz);

    }

    /**
     *
     * @Function: FastJsonUtils.java
     * @Description: 该函数的功能描述
     * @version: v1.0.0
     * @date: 2018年9月11日 上午9:18:47
     */

    public static <K, V> String collectToString(Map<K, V> m) {

        String s = JSONObject.toJSONString(m);

        return s;

    }

    public static <T> T stringToBean(String content, TypeReference<T> typeReference) {

        return JSONObject.parseObject(content, typeReference);

    }

    public static  String getString(String json,String key){
        return  JSON.parseObject(json).getString(key);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值