以下数据来自:http://wangym.iteye.com/blog/738933
Jackson:http://jackson.codehaus.org/
JSON-lib:http://json-lib.sourceforge.net/
Gson:http://code.google.com/p/google-gson/
测试环境:
1、工作电脑:Intel双核E8400 共6GHz,内存4GB,WinXP
2、JSON-lib用最新的JDK15,GSON版本是最新的v1.4,Jackson也是最新的v1.5.5,JDK-v1.6.0_20,JMeter-v2.4
3、测试时不开启任何无关进程,每完成一项测试后关闭JMeter整理内存后,再进行下一项测试,每项测试运行3次,取平均值
4、JSON转Java Bean意为将JSON格式转换成Java类,这个类内包括Map、List、Date、Integer/Long/Double、String等类型的属性,Java Bean转Json则同理。另外,两者互转,每次转换的数据都是随机生成
测试结果:
* 吞吐量的值越大越好,总耗时的值越小越好
JSON转Bean,5个线程并发,约200字节对象,1千万次转换:
Jackson | JSON-lib | Gson | |
TPS | 64113.7 | 8067.4 | 13952.8 |
总耗时(秒) | 155 | 1238 | 700 |
Bean转JSON,5个线程并发,约200字节对象,1千万次转换:
Jackson | JSON-lib | Gson | |
TPS | 54802 | 15093.2 | 17308.2 |
总耗时(秒) | 181 | 661 | 560 |
JSON转Bean,5个线程并发,约2K对象,1千万次转换:
Jackson | JSON-lib | Gson | |
TPS | 37314 | 2406.9 | 3657.50 |
总耗时(秒) | 267 | 4120 | 2720 |
Bean转JSON,5个线程并发,约2K对象,1千万次转换:
Jackson | JSON-lib | Gson | |
TPS | 30922.2 | 4274.8 | 4977.00 |
总耗时(秒) | 322 | 2320 | 2000 |
测试总结:
1、显而易见,无论是哪种形式的转换,Jackson > Gson > Json-lib。
Jackson的处理能力甚至高出Json-lib有10倍左右
2、JSON-lib似乎已经停止更新,最新的版本也是基于JDK15,而Jackson的社区则较为活跃;
3、在测试性能的同时,又以人肉方式对这三个类库转换的正确性 进行了检查 ,三者均达100%正确 ;
4、JSON-lib在转换诸如Date类型时较为累赘,如以下是两者的转换结果:
JSON-lib:
{“brithday”:{“date”:17,”day”:2,”hours”:9,”minutes”:24,”month”:7,”seconds”:26,”time”:1282008266398,”timezoneOffset”:-480,”year”:110}}
Jackson:
{“brithday”:1282008123101}
5、JSON-lib依赖commons系列的包及 ezmorph包共 5个,而Jackson除自身的以外只依赖于commons-logging
6、Jackson提供完整基于节点的Tree Model,以及完整的OJM数据绑定功能。
Jackson使用示例:
JacksonMapper:
创建为饿汉式单例模式 ,Jackson用于转换的核心类ObjectMapper无需每次都new一个object,官网上的一句话:can reuse, share globally
- /**
- * @author xuanyin
- *
- */
- public class JacksonMapper {
- /**
- *
- */
- private static final ObjectMapper mapper = new ObjectMapper();
- /**
- *
- */
- private JacksonMapper() {
- }
- /**
- *
- * @return
- */
- public static ObjectMapper getInstance() {
- return mapper;
- }
- }
JSON转Bean:
- ……
- String json = “…”;
- ObjectMapper mapper = JacksonMapper.getInstance();
- YourBean bean = mapper.readValue(json, new YourBean().getClass());
- ……
Bean转JSON:
- ……
- YourBean bean = new YourBean();
- ……
- ObjectMapper mapper = JacksonMapper.getInstance();
- StringWriter sw = new StringWriter();
- JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
- mapper.writeValue(gen, bean);
- gen.close();
- String json = sw.toString();
- ……
* 上面两段代码中的YourBean当然也可以是Java的基本类型
***************************************************************************************************************
附jackon具体使用:
一、入门
Jackson中有个ObjectMapper类很是实用,用于Java对象与JSON的互换。
1.JAVA对象转JSON[JSON序列化]
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonDemo {
public static void main(String[] args) throws ParseException, IOException {
User user = new User();
user.setName(“小民”);
user.setEmail(“[email protected]”);
user.setAge(20);
SimpleDateFormat dateformat = new SimpleDateFormat(“yyyy-MM-dd”);
user.setBirthday(dateformat.parse(“1996-10-01”));
/**
* ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。
* ObjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。
* writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。
* writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。
* writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。
* writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。
*/
ObjectMapper mapper = new ObjectMapper();
//User类转JSON
//输出结果:{“name”:”小民”,”age”:20,”birthday”:844099200000,”email”:”[email protected]”}
String json = mapper.writeValueAsString(user);
System.out.println(json);
//Java集合转JSON
//输出结果:[{“name”:”小民”,”age”:20,”birthday”:844099200000,”email”:”[email protected]”}]
List<User> users = new ArrayList<User>();
users.add(user);
String jsonlist = mapper.writeValueAsString(users);
System.out.println(jsonlist);
}
}
2.JSON转Java类[JSON反序列化]
import java.io.IOException;
import java.text.ParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonDemo {
public static void main(String[] args) throws ParseException, IOException {
String json = “{\”name\”:\”小民\”,\”age\”:20,\”birthday\”:844099200000,\”email\”:\”[email protected]\”}”;
/**
* ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。
*/
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
System.out.println(user);
}
}
二、Jackson支持3种使用方式:
1、Data Binding:最方便使用.
(1)Full Data Binding:
private static final String MODEL_BINDING = “{\”name\”:\”name1\”,\”type\”:1}”;
public void fullDataBinding() throws Exception{
ObjectMapper mapper = new ObjectMapper();
Model user = mapper.readValue(MODEL_BINDING, Model.class);//readValue到一个实体类中.
System.out.println(user.getName());
System.out.println(user.getType());
}
Model类:
private static class Model{
private String name;
private int type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
(2)Raw Data Binding:
/**
Concrete Java types that Jackson will use for simple data binding are:
JSON Type Java Type
object LinkedHashMap<String,Object>
array ArrayList<Object>
string String
number(no fraction) Integer, Long or BigInteger (smallest applicable)
number(fraction) Double(configurable to use BigDecimal)
true|false Boolean
null null
*/
public void rawDataBinding() throws Exception{
ObjectMapper mapper = new ObjectMapper();
HashMap map = mapper.readValue(MODEL_BINDING,HashMap.class);//readValue到一个原始数据类型.
System.out.println(map.get(“name”));
System.out.println(map.get(“type”));
}
(3)generic Data Binding:
private static final String GENERIC_BINDING = “{\”key1\”:{\”name\”:\”name2\”,\”type\”:2},\”key2\”:{\”name\”:\”name3\”,\”type\”:3}}”;
public void genericDataBinding() throws Exception{
ObjectMapper mapper = new ObjectMapper();
HashMap<String,Model> modelMap = mapper.readValue(GENERIC_BINDING,new TypeReference<HashMap<String,Model>>(){});//readValue到一个范型数据中.
Model model = modelMap.get(“key2”);
System.out.println(model.getName());
System.out.println(model.getType());
}
2、Tree Model:最灵活。
private static final String TREE_MODEL_BINDING = “{\”treekey1\”:\”treevalue1\”,\”treekey2\”:\”treevalue2\”,\”children\”:[{\”childkey1\”:\”childkey1\”}]}”;
public void treeModelBinding() throws Exception{
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(TREE_MODEL_BINDING);
//path与get作用相同,但是当找不到该节点的时候,返回missing node而不是Null.
String treekey2value = rootNode.path(“treekey2”).getTextValue();//
System.out.println(“treekey2value:” + treekey2value);
JsonNode childrenNode = rootNode.path(“children”);
String childkey1Value = childrenNode.get(0).path(“childkey1”).getTextValue();
System.out.println(“childkey1Value:”+childkey1Value);
//创建根节点
ObjectNode root = mapper.createObjectNode();
//创建子节点1
ObjectNode node1 = mapper.createObjectNode();
node1.put(“nodekey1”,1);
node1.put(“nodekey2”,2);
//绑定子节点1
root.put(“child”,node1);
//数组节点
ArrayNode arrayNode = mapper.createArrayNode();
arrayNode.add(node1);
arrayNode.add(1);
//绑定数组节点
root.put(“arraynode”, arrayNode);
//JSON读到树节点
JsonNode valueToTreeNode = mapper.valueToTree(TREE_MODEL_BINDING);
//绑定JSON节点
root.put(“valuetotreenode”,valueToTreeNode);
//JSON绑定到JSON节点对象
JsonNode bindJsonNode = mapper.readValue(GENERIC_BINDING, JsonNode.class);//绑定JSON到JSON节点对象.
//绑定JSON节点
root.put(“bindJsonNode”,bindJsonNode);
System.out.println(mapper.writeValueAsString(root));
}
3、Streaming API:最佳性能。
对于性能要求高的程序,推荐使用流API,否则使用其他方法
不管是创建JsonGenerator还是JsonParser,都是使用JsonFactory。