美文网首页
FastJson工具类适配Spring @ResponseBod

FastJson工具类适配Spring @ResponseBod

作者: liuliuzo | 来源:发表于2020-10-17 10:05 被阅读0次

Spring默认使用的是jackson,如果你想使用fastjson需要自己配置一下转换器,这个工具类很好的封装了fastjson,为后续替换json库提供了条件。

package com.liuliu.learning;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class FastJsonUtil {

    private static final SerializeConfig CONFIG;

    static {
        CONFIG = new SerializeConfig();
        CONFIG.put(java.util.Date.class, new JSONLibDataFormatSerializer());
        CONFIG.put(java.sql.Date.class, new JSONLibDataFormatSerializer());
    }

    // 适配@ResponseBody
    private static final SerializerFeature[] FEATURES = { SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullNumberAsZero,
            SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullStringAsEmpty,
            SerializerFeature.DisableCircularReferenceDetect };

    public static String toJSONString(Object object) {
        return JSON.toJSONString(object);
    }

    /**
     * To JSON String with util default config and features
     * 
     * @param object
     * @return
     */
    public static String toJSONStringDefault(Object object) {
        return JSON.toJSONString(object, CONFIG, FEATURES);
    }

    public static String toJSONString(Object object, SerializerFeature[] feature) {
        return JSON.toJSONString(object, feature);
    }

    public static String toJSONString(Object object, SerializeConfig config) {
        return JSON.toJSONString(object, config);
    }

    public static String toJSONString(Object object, SerializeConfig config, SerializerFeature[] feature) {
        return JSON.toJSONString(object, config, feature);
    }

    public static Object toBean(String text) {
        try {
            return JSON.parse(text);
        } catch (JSONException e) {
            throw new BizException(BasicErrorCode.B_JSON_PARSER, e.getMessage());
        }
    }

    public static <T> T toBean(String text, Class<T> clazz) {
        try {
            return JSON.parseObject(text, clazz);
        } catch (JSONException e) {
            throw new BizException(BasicErrorCode.B_JSON_PARSER, e.getMessage());
        }
    }

    public static Object[] toArray(String text) {
        return toArray(text, null);
    }

    public static <T> Object[] toArray(String text, Class<T> clazz) {
        return JSON.parseArray(text, clazz).toArray();
    }

    public static <T> List<T> toList(String text, Class<T> clazz) {
        return JSON.parseArray(text, clazz);
    }

    public static Object textToJson(String text) {
        return JSON.parse(text);
    }

    public static Map stringToCollect(String s) {
        return JSONObject.parseObject(s);
    }

    public static String collectToString(Map<?, ?> m) {
        return JSONObject.toJSONString(m);
    }

    public static String objectToJson(Object obj) {
        return JSON.toJSONString(obj);
    }

}

相关文章

网友评论

      本文标题:FastJson工具类适配Spring @ResponseBod

      本文链接:https://www.haomeiwen.com/subject/jyuxmktx.html