利用Fastjson进行泛型的序列化和反序列化

public class SerialClass<T> {
    public T deserialize(byte[] data) {
        if (data == null) {
            return null;
        }
        Class<T> entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return JSON.parseObject(data, entityClass);
    }

    public byte[] serialize(T data) {
        if (data == null) {
            return null;
        }
        return JSON.toJSONString(data).getBytes();
    }
}
发布了27 篇原创文章 · 获赞 3 · 访问量 1140

猜你喜欢

转载自blog.csdn.net/qq_39609993/article/details/103561574