Jackson、Gson、Fastjson 序列化与反序列化使用

2019-08-28  本文已影响0人  我问你瓜保熟吗

Java序列化是指把Java对象转换为可传输的字节序列的过程。
Java反序列化是指把传输的字节序列恢复为Java对象的过程。
这两个过程使我们非常方便的存储和传输数据。

准备一个java bean Person 用来序列化和反序列化操作

package com.example.json;

/**
 * author:xyb
 * Date:2019-08-28 上午 11:09
 */
public class Persion {
    private  String name;
    private  int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

一、Gson

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.5</version>
</dependency>
implementation 'com.google.code.gson:gson:2.8.5'
    @Test
    public void toJSON(){
        HashMap map = new HashMap();
        map.put("name","gson");
        map.put("age","24");

        Person person = new Person();
        person.setName("jackson");
        person.setAge(24);

        // 创建Gson对象
        Gson gson = new Gson();

        // 1、map 序列化成 Json字符串
        String json = gson.toJson(map);
        System.out.println(json);

        // 2、Java Bean 序列化成 Json字符串
        String personJson = gson.toJson(person);
        System.out.println(personJson);

        // 3、List 集合序列化成 Json数组: 将两个Person bean添加到List中
        Person person1 = new Person();
        person1.setName("gson");
        person1.setAge(34);
        ArrayList<Person> personList = new ArrayList<>();
        personList.add(person);
        personList.add(person1);

        String personListJson = gson.toJson(personList);
        System.out.println(personListJson)
    }

    @Test
    public void  fromJSON(){
        // Json字符串
        String xiaoming = "{\"name\":\"gson\",\"age\":\"24\"}";

        // 创建Gson对象
        Gson gson = new Gson();

        // 1、Json对象反序列化成Map
        Map result = gson.fromJson(xiaoming, new TypeToken<Map>() { }.getType());
        System.out.println(result);

        // 2、Json对象反序列化成Java Bean
        Person person = gson.fromJson(xiaoming, Person.class);
        System.out.println(person);
    }

二、Fastjson

引入Maven 或 Gradle依赖:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.61</version>
</dependency>
compile 'com.alibaba:fastjson:1.2.61'
    @Test
    public void Fastjson(){
        Person person = new Person();
        person.setName("xie");
        person.setAge(24);

        // Java Bean序列化从json对象
        String s = JSON.toJSONString(person);
        System.out.println(s);

        // 从Map序列化成Json
        Map<String,Object> map = new HashMap<>();
        map.put("name", "xiexie");
        map.put("age", 26);
        String jsonString = JSON.toJSONString(map);
        System.out.println(jsonString);

        //List<Object>转json数组
        Person person1 = new Person();
        Person person2 = new Person();
        person1.setName("xie");
        person2.setName("zhang");
        person1.setAge(26);
        person2.setAge(24);

        List<Person> users = new ArrayList<Person>();
        users.add(person1);
        users.add(person2);

        String ListUserJson = JSON.toJSONString(users);
        System.out.println(ListUserJson);

    }
    @Test
    public void toFastjson(){
        // Json 字符串
        String xiaoming = "{\"name\":\"fastjson\",\"age\":\"24\"}";
        String personListJson = "{\"userList\":[{\"name\":\"jackson\",\"age\":\"24\"},{\"name\":\"gson\",\"age\":\"25\"}]}";

        // 1、Json字符串序列化成Map 先将字符串转换成json对象,再将json对象转Map
        JSONObject  jsons= JSONObject.parseObject(xiaoming);

        Map<String,Object> map = (Map<String,Object>)jsons;
        System.out.println("age的值是"+map.get("age"));

        // 2、Json字符串序列化成Java Bean
        Person jsonObject = JSON.parseObject(xiaoming,Person.class);
        System.out.println(jsonObject.getName());

        // 3、Json字符串序列化成List
    }

三、Jackson

    @Test
    public void toJson() throws IOException {

        Person person = new Person();
        person.setName("jackson");
        person.setAge(24);

        HashMap map = new HashMap();
        map.put("name","gson");
        map.put("age","24");

        // 创建Jackson对象
        ObjectMapper objectMapper = new ObjectMapper();

        // 1、map 序列化成 Json对象
        String s = objectMapper.writeValueAsString(map);
        System.out.println(s);

        // 2、Java Bean 序列化成 Json对象
        String userJson = objectMapper.writeValueAsString(person);
        System.out.println(userJson);

        // 3、List 集合序列化成 Json数组: 将两个Person bean添加到List中
        Person person1 = new Person();
        person1.setName("gson");
        person1.setAge(34);
        ArrayList<Person> personList = new ArrayList<>();
        personList.add(person);
        personList.add(person1);

        String personListJson = objectMapper.writeValueAsString(personList);
        System.out.println(personListJson);

    }
    @Test
    public void jacksonBean() throws IOException {

        // Json 字符串
        String xiaoming = "{\"name\":\"gson\",\"age\":\"24\"}";
        String personListJson = "{\"userList\":[{\"name\":\"jackson\",\"age\":\"24\"},{\"name\":\"gson\",\"age\":\"25\"}]}";

        // 创建Jackson对象
        ObjectMapper objectMapper = new ObjectMapper();

        // 1、Json字符串反序列化成 Map
        Map jacksonmap = objectMapper.readValue(xiaoming, Map.class);
        System.out.println(jacksonmap);

        // 2、Json字符串反序列化成 Java Bean
        com.example.json.pojo.Person person = objectMapper.readValue(xiaoming, com.example.json.pojo.Person.class);
        System.out.println(person);

        // 3、Json字符串数组反序列化成 List

    }
上一篇下一篇

猜你喜欢

热点阅读