json转化过慢的调优

json转化过慢的调优

最近系统响应很慢,后来发现和SQL也关系不大。
跟踪程序后发现,和JAVA对象转化为JSON对象有一定的关系,也算是优化的一个方面吧。

json的官方网站
http://json.org/

找到一个遇到类似问题的朋友,参考他的日志
http://hi.baidu.com/camaro/blog/item/384b31e9e7bae237b90e2d44.html
http://twit88.com/blog/2008/07/11/java-json-parser-performance/
My test is simple.
Create a JSON object
Add few String objects to the object
Serialize and deserialize the JSON object
Loop the above for 1 million times

The performance of the above libraries sorted in order
Jackson
Noggit
JSON-lib
我使用的也是JSON-lib,NND,测试一下排名前面的两个东东试试。

jackson首页
http://jackson.codehaus.org/
下载地址
http://wiki.fasterxml.com/JacksonDownload
文档地址
http://wiki.fasterxml.com/JacksonInFiveMinutes

下载到jar jackson-xc-1.2.0.jar,jackson-core-asl-1.2.0.jar,jackson-mapper-asl-1.2.0.jar,jackson-jaxrs-1.2.0.jar

User user = new User();
user.setEmail("[email protected]");
user.setIdxNum(Integer.valueOf(1));
user.setLoginId("luohuazju");
Company u = new Company();
u.setName("handsome");
u.setLocation("四u24029 ");
user.setCompany(u);
Map item = new HashMap();
item.put("user", user);
JSONObject jo = JSONObject.fromObject(item, configJson());
System.out.println("=============json lib====================");
System.out.println(jo.toString());
System.out.println("=============json lib====================");
System.out.println("=============jackson lib====================");
System.out.println(getJsonString(item));
System.out.println("=============jackson lib====================");

private static JsonConfig configJson() {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
return jsonConfig;
}
private static String getJsonString(Object item) {
JsonFactory jf = new JsonFactory();
try {
StringWriter sw = new StringWriter();
JsonGenerator gen = jf.createJsonGenerator(sw);
new ObjectMapper().writeValue(gen, item);
gen.flush();
return sw.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
单元测试,没有问题,但是在实际的UserController中,user中有company对象,company中有users对象。导致了如下错误:
报错:
DEBUG DispatcherServlet - Could not complete request
Caused by: java.lang.StackOverflowError
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:71)
at org.codehaus.jackson.map.ser.BeanPropertyWriter$StdMethod.serializeAsField(BeanPropertyWriter.java:215)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:58)
at org.codehaus.jackson.map.ser.BeanPropertyWriter$StdMethod.serializeAsField(BeanPropertyWriter.java:215)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:58)
搜索到的结果,目前无法解决,要等待1.3版本

noggit的SVN地址
http://svn.apache.org/repos/asf/labs/noggit/
pom.xml注释掉有关eclipse的插件那块
命令 mvn clean
命令 mvn install
编译得到jar包noggit-1.0-SNAPSHOT.jar

玩不来,以后再看看。

gson首页
http://code.google.com/p/google-gson/

代码片段
Gson gson = new Gson();
String json = gson.toJson(item);
System.out.println("=============gson lib====================");
System.out.println(json);
System.out.println("=============gson lib====================");

但是当item中对象有循环引用时报错:
Exception in thread "main" java.lang.IllegalStateException: Circular reference found: cn.sccl.um.model.User@26d4f1
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:108)

暂时没有找到解决办法,算了,暂时解决对象中的循环调用吧。

你可能感兴趣的:(UI)