[Gson课题一]JSON数据串反序列化为POJO对象

[Gson专题一]JSON数据串反序列化为POJO对象

1. Gson.<T> T fromJson(String json, Class<T> classOfT)

使用场景:对于本身不是泛型的POJO类(POJO的实例字段可以使用泛型集合),可以使用这个方法将JSON串转换成为POJO对象

 

1.1 Data类不是泛型类,但是Data中的集合List和Map都是泛型的,Data中不包含其它的POJO

 

   POJO

 

package gson.test2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Data {
    private int id;
    private List<Integer> numbers;
    private Map<String, Integer> map;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<Integer> getNumbers() {
        return numbers;
    }

    public void setNumbers(List<Integer> numbers) {
        this.numbers = numbers;
    }

    public Map<String, Integer> getMap() {
        return map;
    }

    public void setMap(Map<String, Integer> map) {
        this.map = map;
    }

    public static Data create() {
        Data d = new Data();
        d.setId(123456);
        List<Integer> numbers = new ArrayList<Integer>();
        numbers.add(100);
        numbers.add(200);
        numbers.add(300);
        d.setNumbers(numbers);

        Map<String, Integer> maps = new HashMap<String, Integer>();
        maps.put("x", 9);
        maps.put("y", 99);
        maps.put("z", 999);

        d.setMap(maps);
        return d;
    }
}
 

 

   测试类:

  

package gson.test2;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;

import java.util.*;

public class Test {
    public static void main(String[] args) {

        Data d = Data.create();
        String gson = new Gson().toJson(d);
        String fastjson = JSON.toJSONString(d);

        System.out.println(fastjson);
        System.out.println(gson);

        d = new Gson().fromJson(gson, Data.class);
        Map<String, Integer> map = d.getMap();
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = iterator.next();
            System.out.println(entry.getKey() + "\t" + entry.getValue());
        }
    }
}

 

1.2 POJO包含泛型集合类以及聚合其它的非泛型POJO

   C聚合B,B聚合A,B中有个Date类型的实例变量

 

import java.util.List;

public class A {
    private String key;
    private String value;
    private List<String> primitives;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public List<String> getPrimitives() {
        return primitives;
    }

    public void setPrimitives(List<String> primitives) {
        this.primitives = primitives;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        A a = (A) o;

        if (this.key == null && a.key != null || this.key != null && a.key == null) {
            return false;
        }
        if (this.value == null && a.value != null || this.value != null && a.value == null) {
            return false;
        }
        if (this.primitives == null && a.primitives != null || this.primitives != null && a.primitives == null) {
            return false;
        }

        if (!this.key.equals(a.key)) {
            return false;
        }

        if (!this.value.equals(a.value)) {
            return false;
        }

        if (this.primitives.size() != a.primitives.size()) {
            return false;
        }

        for (int i = 0; i < this.primitives.size(); i++) {
            String x = this.primitives.get(i);
            String y = a.primitives.get(i);
            if (!x.equals(y)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public int hashCode() {
        int result = key != null ? key.hashCode() : 0;
        result = 31 * result + (value != null ? value.hashCode() : 0);
        result = 31 * result + (primitives != null ? primitives.hashCode() : 0);
        return result;
    }
}
 

 

 

import java.util.Date;
import java.util.List;

public class B {
    private Date currentTime;
    private List<A> rows;

    public Date getCurrentTime() {
        return currentTime;
    }

    public void setCurrentTime(Date currentTime) {
        this.currentTime = currentTime;
    }

    public List<A> getRows() {
        return rows;
    }

    public void setRows(List<A> rows) {
        this.rows = rows;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        B b = (B) o;

        if (this.currentTime == null && b.currentTime != null || this.currentTime != null && b.currentTime == null) {
            return false;
        }

        if (this.rows == null && b.rows != null || this.rows != null && b.rows == null) {
            return false;
        }

//        if (!this.currentTime.equals(b.currentTime)) {
//            return false;
//        }

        if (this.rows.size() != b.rows.size()) {
            return false;
        }

        for (int i = 0; i < this.rows.size(); i++) {
            A a1 = this.rows.get(i);
            A a2 = b.rows.get(i);
            if (!a1.equals(a2)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public int hashCode() {
        int result = currentTime != null ? currentTime.hashCode() : 0;
        result = 31 * result + (rows != null ? rows.hashCode() : 0);
        return result;
    }
}
 

 

 

import java.util.List;

public class C {
    private A aObject;
    private List<B> rows;

    public A getAObject() {
        return aObject;
    }

    public void setAObject(A aObject) {
        this.aObject = aObject;
    }

    public List<B> getRows() {
        return rows;
    }

    public void setRows(List<B> rows) {
        this.rows = rows;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        C c = (C) o;

        if (this.aObject == null && c.aObject != null || this.aObject != null && c.aObject == null) {
            return false;
        }

        if (this.aObject != null && !this.aObject.equals(c.aObject)) {
            return false;
        }

        if (this.rows == null && c.rows != null || this.rows != null && c.rows == null) {
            return false;
        }

        if (this.rows.size() != c.rows.size()) {
            return false;
        }

        for (int i = 0; i < this.rows.size(); i++) {
            B b1 = this.rows.get(i);
            B b2 = c.rows.get(i);
            if (!b1.equals(b2)) {
                return false;
            }
        }


        return true;
    }

    @Override
    public int hashCode() {
        int result = aObject != null ? aObject.hashCode() : 0;
        result = 31 * result + (rows != null ? rows.hashCode() : 0);
        return result;
    }
}
 

    测试

import com.google.gson.Gson;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Controller {

    @Test
    public void testJSON2Java() {

        A a = new A();
        a.setKey("A-Key");
        a.setValue("A-Value");
        List<String> primitives = new ArrayList<String>();
        primitives.add("p1");
        primitives.add("p2");
        primitives.add("p3");
        a.setPrimitives(primitives);

        List<A> arrows = new ArrayList<A>();
        arrows.add(a);

        B b = new B();
        b.setCurrentTime(new Date());
        b.setRows(arrows);
        List<B> brows = new ArrayList<B>();
        brows = new ArrayList<B>();
        brows.add(b);


        A aObject = new A();
        aObject.setKey("A-B-Key1");
        aObject.setValue("A-B-Value1");
        aObject.setPrimitives(primitives);
        C c = new C();
        c.setAObject(aObject);
        c.setRows(brows);


        String str = new Gson().toJson(c);
        System.out.println(str);

        System.out.println("-----------------------------------------");
        C cc = new Gson().fromJson(str, C.class);
        Assert.assertTrue(c.equals(cc));
    }


}

 问题:当把B类的关于比较currentTime的注释打开,测试失败,调试发现,两个时间有1秒以内的偏差,不打算看源代码查找原因了。对于日期类型,保存数值而不是Date对象吧

 


 

1.3 POJO包含泛型集合类,POJO,POJO聚合的其它的泛型POJO

1.4 POJO是泛型类