对比XStream和JSON
时间:2011-04-05 BlogJava
XStream是thoughtworks开发的开源产品,采用的是xpp来进行xml和对象之间的转换。 它不需要schema或其他的mapping文件就可以进行java对象和xml文件之间的转换,API调 用起来非常方便,并且扩展功能强大。
网址:http://xstream.codehaus.org
JSON是目前比较流行的轻量级的对象数据转换方式,常常用在ajax和web service领域 。目前大部分语言都有JSON的实现。Java领域有json lib,还有xstream json(jettison) 。
网址:http://json-lib.sourceforge.net/
http://jettison.codehaus.org
主要是对3者 (xstream, json lib, xstream json) 转换的数据长度,还有转换的效 率做个比较。
转换的java类为:
package com.ldd600.ws;
public class Employee {
private String name;
private int age;
private Address address;
private double salary;
private boolean isActive;
private char sexy;
public String getName() {
return name;
}
public void setName(String naem) {
this.name = naem;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public boolean isActive() {
return isActive;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
public char getSexy() {
return sexy;
}
public void setSexy(char sexy) {
this.sexy = sexy;
}
}
对比XStream和JSON(2)
时间:2011-04-05 BlogJava
package com.ldd600.ws;
public class Address {
private String street;
private String country;
private String city;
private String doorNum;
// private Employee employee;
private String empName;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDoorNum() {
return doorNum;
}
public void setDoorNum(String doorNum) {
this.doorNum = doorNum;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
// public Employee getEmployee() {
// return employee;
// }
//
// public void setEmployee(Employee employee) {
// this.employee = employee;
// }
}
数据格式和长度比较:
方法 |
长度 |
格式 |
Xstream |
328 |
<com.ldd600.ws.Employee>
<name>gavin</name>
<age>0</age>
<address>
<street>zhaojiabang</street>
<country>china</country>
<city>shanghai</city>
<doorNum>789</doorNum>
<empName>gavin</empName>
</address>
<salary>300000.0</salary>
<isActive>true</isActive>
<sexy>m</sexy>
</com.ldd600.ws.Employee> |
Json lib |
170 |
"789","empName":"gavin","street":& quot;zhaojiabang"},"age":0,"name":"gavin",& quot;salary":300000,"sexy":"m"} |
XStream json |
183 |
{"employee": {"name":"gavin","age":0,"address": {"street":"zhaojiabang","country":"china&qu ot;,"city":"shanghai","doorNum":789,"empNam e":"gavin"},"salary":300000,"isActive":true ,"sexy":"m"}} |
|