当要将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; } }
测试
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对象吧
Model类
a.不是泛型类,
c.Data中包含其它的泛型Data
package gson.test5; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; class Piece { private String name; private int weight; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } } class Data<T> { private List<T> pieces; public List<T> getPieces() { return pieces; } public void setPieces(List<T> pieces) { this.pieces = pieces; } } class Model { private Data<Piece> data; public Data<Piece> getData() { return data; } public void setData(Data<Piece> data) { this.data = data; } } public class ModelTest { public static void main(String[] args) { List<Piece> pieces = new ArrayList<Piece>(); Piece p1 = new Piece(); p1.setName("Cat"); p1.setWeight(10); pieces.add(p1); Piece p2 = new Piece(); p2.setName("Dog"); p2.setWeight(26); pieces.add(p2); Data<Piece> data = new Data<Piece>(); data.setPieces(pieces); Model m = new Model(); m.setData(data); String str = new Gson().toJson(m); System.out.println(str); m = new Gson().fromJson(str, Model.class); } }