Jackson 泛型序列化


代码实现

  • JSON序列化工具类
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * @Description 通用JSON序列化
 * @date Aug 16, 2021
 * @Version 1.0
 */
@Component
public class GenericAndJson {

    private static ObjectMapper mapper;

    /**
     * 注入mapper
     * @param mapper
     */
    @Autowired
    public void setMapper(ObjectMapper mapper) {
        GenericAndJson.mapper = mapper;
    }

    /**
     * Object转JSON
     * @param t
     * @param <T>
     * @return
     */
    public static <T> String objectToJson(T t) {
        try {
            return GenericAndJson.mapper.writeValueAsString(t);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    /**
     * JSON转Object
     * @param str
     * @param tClass
     * @param <T>
     * @return
     */
    public static <T> T jsonToObject(String str, TypeReference<T> tr) {

        if (StringUtils.isEmpty(str)) {
            return null;
        }

        try {
            return GenericAndJson.mapper.readValue(str, tr);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

    }

    /**
     * JSON转List
     * @param str
     * @param <T>
     * @return
     */
    public static <T> List<T> jsontToList(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }

        try {
            List<T> list = GenericAndJson.mapper.readValue(str, new TypeReference<List<T>>() {
            });
            return list;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

    }

    /**
     * JSON转List
     * @param str
     * @param tTypeReference
     * @param <T>
     * @return
     */
    public static <T> T jsonToList(String str, TypeReference<T> tr) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }

        try {
            T t = GenericAndJson.mapper.readValue(str, tr);
            return t;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

}
  • 调用方法
// JSON转List
List<Spec> specs = GenericAndJson.jsonToList(this.specs, new TypeReference<List<Spec>>() {});

// Object转JSON
String specs = GenericAndJson.objectToJson(specs);

- End -
梦想是咸鱼
关注一下吧
posted @ 2021-08-17 02:33  Maggieq8324  阅读(172)  评论(0编辑  收藏  举报