Posts 抛弃ObjectOutputStream用JSON处理Java对象串行化
Post
Cancel

抛弃ObjectOutputStream用JSON处理Java对象串行化

前面提到了Java推荐的串行化ObjectInputStream/ObjectOutputStream 缺点比较明显(生成的数据大;人和其他语言无法阅读),所以已经不再适用了。相反的,JSON或者XML格式处理串行化已经是主流,优点自然显而易见:

  • 数据小,快速传输
  • 人能直接阅读,各语言都能解析
  • 可以构造字符串,反串行化生成实例对象

这里举个Google的开源Gson,用于将Java对象JSON化。

JSON格式的串行与反串行

1
2
3
4
5
6
7
8
9
10
11
public class Employee implements Serializable {
    private static final long serialVersionUID = 9L;
    public int no;
    public String email;
    public float salary;
    public boolean active;
    public char sex;
    public String toString(){
        return String.format("no=%d, email=%s, salary=%f, active=%b, sex=%c", no, email, salary, active, sex);
    }
}
1
2
3
Gson gson = new Gson();
String employeeStr = gson.toJson(emp);
System.out.println(String.format("%s = %d", employeeStr, employeeStr.length()));

简单易读的JSON,字节量非常小。相比之下使用ObjectOutputStream生成的文件要137字节。

1
{"no":123,"email":"jie@xwiz.cn","salary":3000.45,"active":true,"sex":"m"} = 73

构造JSON生成类实例

另外,我可以模拟出一个JSON,通过网络传输给remote,让它生成Employee。

1
2
3
String newEmpoyeeStr = "{'no':456,'salary':4000.45,'sex':'f','active':false,'email':'jiechencn@qq.com'}";
Employee newEmp = gson.fromJson(newEmpoyeeStr, Employee.class);
System.out.println(newEmp.getClass() + ": " + newEmp.toString());
1
class cn.xwiz.jvm.serializable.Employee: no=456, email=jiechencn@qq.com, salary=4000.449951, active=false, sex=f
This post is licensed under CC BY 4.0 by the author.