序列化(serialization)是将对象的状态信息转换为可以存储或传输的窗体的过程。在序列化期间,对象将其当前状态写入到临时或持久性存储区。以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。
下面用fastjson完成序列化与反序列化的一个例子:
要被序列化和反序列化的对象Person:
public class Person {
private String name;
private int age;
/*
* 对于下面的第二个例子,此空构造器必要,否则: com.alibaba.fastjson.JSONException:
* default constructor not found. class com.test.Person
*/
public Person() {}
public Person(String name, int age) {
this.name = name;
this.age = 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;
}
}
下面实测序列化与反序列化结果:
import com.alibaba.fastjson.JSON;
public class Serialization {
public static void main(String[] args) {
Person person = new Person("XiaoMing", 16);
//序列化
String text = JSON.toJSONString(person);
System.out.println(text);
//反序列化
Person person1 = JSON.parseObject(text, Person.class);
System.out.println(person1.getName()+","+person1.getAge());
}
}
结果:
{"age":16,"name":"XiaoMing"}
XiaoMing,16
如果在Person中加入另一个public类型的域变量(如public int ok;)或get方法(如public int getOk(){return 88},最终都会被序列化:
分别为:{"age":16,"name":"XiaoMing","ok":0} 、{"age":16,"name":"XiaoMing","ok":88}
要想不被序列化可加关键字transient,顾名思义就是不持久化,如publictransient ok = 99; ok字段将不会被序列化。
对于List的序列化与反序列化:
public class People {
private String id;
private List<Person> list = new ArrayList<Person>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Person> getList() {
return list;
}
public void setList(List<Person> list) {
this.list = list;
}
}
import com.alibaba.fastjson.JSON;
public class SerializationList {
public static void main(String[] args) {
People people = new People();
people.setId("10012");
Person p1 = new Person("XiaoMing", 16);
Person p2 = new Person("XiaoFang", 15);
List<Person> list = new ArrayList<Person>();
list.add(p1);
list.add(p2);
people.setList(list);
// 序列化
String text = JSON.toJSONString(people);
System.out.println(text);
// 反序列化
People p = JSON.parseObject(text, People.class);
System.out.println(p.getId());
for(Person person : p.getList()){
System.out.println(person.getName()+","+person.getAge());
}
}
}
结果:
{"id":"10012","list":[{"age":16,"name":"XiaoMing"},{"age":15,"name":"XiaoFang"}]}
10012
XiaoMing,16
XiaoFang,15
拼装json串:
JSONArray jArr = new JSONArray();
nameSet = contactMap.keySet();
for (String name : nameSet) {
JSONObject jobj = new JSONObject();
jobj.put("name", name);
jobj.put("phone", contactMap.get(name));
jArr.add(jobj);
}
String jsonStr = jArr.toJSONString();
System.out.print(jsonStr);
[{"name":"张三","phone":"13611112222"},{"name":"李四","phone":"13733334444"}]
注意,对于极端情况,如下:
public class Ruler {
public String name;
public int len;
}
String rawJson1 = "{}";
String rawJson2 = "{'list':[]}";
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
JSONObject jobj = JSON.parseObject(rawJson1);
Iterator<Object> it = jobj.values().iterator();
==>it.hasNext() == false
Ruler ruler = JSON.parseObject(rawJson1, Ruler.class);
==>ruler.name==null, ruler.len==0
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
JSONObject jobj = JSON.parseObject(rawJson2);
JSONArray jArr = jobj .getJSONArray("list");
List<Ruler> rulers = (List<Ruler>) JSONArray.parseArray(jArr.toJSONString(), Ruler.class);
==>rulers.size()==0
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
JSON.toJSONString(new Object())==>{}
JSON.toJSONString(new ArrayList())==>[]
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》