[Gson一]非泛型POJO对象的反序列化

编程技术  /  houtizong 发布于 3年前   253

当要将JSON数据串反序列化自身为非泛型的POJO时,使用Gson.fromJson(String, Class)方法。自身为非泛型的POJO的包括两种:

1. POJO对象不包含任何泛型的字段

2. POJO对象包含泛型字段,例如泛型集合或者泛型类


Data类

a.不是泛型类,

b.Data中的集合List和Map都是泛型的

c.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());        }    }}

 

Data类

a.不是泛型类,

b.Data中的集合List和Map都是泛型的

c.Data中包含其它的非泛型的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;    }}
 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客