Jackson、Gson和Fastjson序列化、反序列化示例

Jackson、Gson和Fastjson序列化、反序列化示例

import java.lang.reflect.Type;
import java.util.Date;
import com.alibaba.fastjson.JSON;
import com.example.tooltest.bean.DemoBean;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.*;
import java.time.LocalDateTime;

@Slf4j
public class JsonSerializableTests {

    private static DemoBean bean;

    private static List<Map<String, DemoBean>> list;

    @BeforeAll
    static void init() {
        bean = new DemoBean();
        bean.setId(1000);
        bean.setContext("我是内容");
        bean.setCreateTime(new Date());
        bean.setUpdateTime(LocalDateTime.now());

        list = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            Map<String, DemoBean> map = new HashMap<>();
            for (int j = 0; j < 2; j++) {
                int num = Integer.parseInt(i + "" + j);
                DemoBean demoBean = new DemoBean();
                demoBean.setId(num);
                demoBean.setContext("我是内容" + num);
                demoBean.setCreateTime(new Date());
                demoBean.setUpdateTime(LocalDateTime.now());
                map.put(String.valueOf(num), demoBean);
            }
            list.add(map);
        }
    }

    @Test
    void testFastJson() {
        /**
         * ------ FastJson ------
         */
        
        /**
         * 简单对象
         */
        //序列化
        String simpleSerialize = JSON.toJSONString(bean);
        //反序列化
        DemoBean simpleDeserialize = JSON.parseObject(simpleSerialize, DemoBean.class);

        /**
         * 复杂对象
         */
        //序列化
        String complexSerialize = JSON.toJSONString(list);
        //反序列化
        com.alibaba.fastjson.TypeReference<List<Map<String, DemoBean>>> typeReference =
                new com.alibaba.fastjson.TypeReference<List<Map<String, DemoBean>>>(){};
        List<Map<String, DemoBean>> complexDeserialize = JSON.parseObject(complexSerialize, typeReference);
    }

    @Test
    void testGson() {
        /**
         * ------ Gson ------
         */
        
        Gson gson = new Gson();
        /**
         * 简单对象
         */
        //序列化
        String simpleSerialize = gson.toJson(bean);
        //反序列化
        DemoBean simpleDeserialize = gson.fromJson(simpleSerialize, DemoBean.class);

        /**
         * 复杂对象
         */
        //序列化
        String complexSerialize = gson.toJson(list);
        //反序列化
        Type type = new TypeToken<List<Map<String, DemoBean>>>() {}.getType();
        List<Map<String, DemoBean>> complexDeserialize = gson.fromJson(complexSerialize, type);
    }

    @Test
    void testJackson() throws JsonProcessingException {
        /**
         * ------ Jackson ------
         */
        
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        /**
         * 简单对象
         */
        //序列化
        String simpleSerialize = mapper.writeValueAsString(bean);
        //反序列化
        DemoBean simpleDeserialize = mapper.readValue(simpleSerialize, DemoBean.class);

        /**
         * 复杂对象
         */
        //序列化
        String complexSerialize = mapper.writeValueAsString(list);
        //反序列化
        com.fasterxml.jackson.core.type.TypeReference<List<Map<String, DemoBean>>> typeReference =
                new com.fasterxml.jackson.core.type.TypeReference<List<Map<String, DemoBean>>>(){};
        List<Map<String, DemoBean>> complexDeserialize = mapper.readValue(complexSerialize, typeReference);
    }
}

@Data
public class DemoBean {
    private Integer id;
    private String context;
    private Date createTime;
    private LocalDateTime updateTime;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值