论坛首页 Java企业应用论坛

吹牛:我写的JSONParser可能是这个星球上最快了(Java)

浏览 143273 次
该帖已经被评为精华帖
作者 正文
   发表时间:2011-01-11  
注解的实现方式是相对比较优雅的,但是和第2种比,性能怎么样?
0 请登录后投票
   发表时间:2011-01-11  
william_ai 写道
注解的实现方式是相对比较优雅的,但是和第2种比,性能怎么样?


目前已经实现了Annotation的方式,基本没有性能影响的。
0 请登录后投票
   发表时间:2011-01-11   最后修改:2011-01-11
优化了字符串序列化性能,SerializeWriter直接提供序列化JSON字符串的方法,代码如下:
private final static boolean[] specicalFlags = new boolean[(int) '\\' + 1];  // 用于快速判断是否特别字符
private final static char[] replaceChars = new char[(int) '\\' + 1]; // 用于特殊字符的替换处理
static {
	specicalFlags['\b'] = true;
	specicalFlags['\n'] = true;
	specicalFlags['\f'] = true;
	specicalFlags['\r'] = true;
	specicalFlags['\"'] = true;
	specicalFlags['\\'] = true;
	specicalFlags['/'] = true;
	
	replaceChars['\b'] = 'b';
	replaceChars['\n'] = 'n';
	replaceChars['\f'] = 'f';
	replaceChars['\r'] = 'r';
	replaceChars['\"'] = '"';
	replaceChars['\\'] = '\\';
	replaceChars['/'] = '/';
}

public void writeStringWithQuote(String text) throws IOException {
	if (text == null) {
		int newcount = count + 4;
		if (newcount > buf.length) {
			expandCapacity(newcount);
		}
		"null".getChars(0, 4, buf, count);
		count = newcount;
		return;
	}
	
	int len = text.length();
	int newcount = count + len + 2;
	if (newcount > buf.length) {
		expandCapacity(newcount);
	}

	count = newcount;
	
	int start = count + 1;
	int end = start + len;
	
	// 不管是否存在特殊字符,先写到buf
	buf[count] = '\"';
	text.getChars(0, len, buf, start);
	buf[newcount - 1] = '\"';
	
	// 处理特别字符,如果存在特别字符,把剩余的内容右移一位
	for (int i = start; i < end; ++i) {
		char ch = buf[i];
		if (ch < specicalFlags.length && specicalFlags[ch]) {
			newcount++;
			if (newcount > buf.length) {
				expandCapacity(newcount);
			}
			count = newcount;
			
			System.arraycopy(buf, i + 1, buf, i + 2, end - i); // 将特别字符后面的内容右移1位
			buf[i] = '\\';
			buf[++i] = replaceChars[(int) ch];
			end++;
		}
	}
	
	count = newcount;
}
0 请登录后投票
   发表时间:2011-01-11  
关于循环引用,jaxb有一个方法就是当遇到循环引用时,执行一个方法,用new 一个temp obj代替this返回。。。断开循环引用。。。
0 请登录后投票
   发表时间:2011-01-12  
luffyke 写道
关于循环引用,jaxb有一个方法就是当遇到循环引用时,执行一个方法,用new 一个temp obj代替this返回。。。断开循环引用。。。


你的建议不错,遇到循环饮用时,忽略处理,考虑考虑。。。 
0 请登录后投票
   发表时间:2011-01-12  
看你的代码 真是对我JAVA底子的 一次全新的学习 呜呜呜
0 请登录后投票
   发表时间:2011-01-12  
又见温少!
现在很流行的方式是直接把JSON或XML对象转换成一个POJO。然而转换出来的POJO,是否所有属性都需要用到?有必要为每个属性特别是强类型属性(如Date),嵌套子对象都在未使用之前做转换?
基于弱类型的迟转换我认为是解决之道。弱类型只所有类型都是String,迟转换是只由调用者去触发转换。
0 请登录后投票
   发表时间:2011-01-12  
fastjson\src\test\java\com\alibaba\json\test\benchkmark\

benchkmark是不是该写成benchmark?
0 请登录后投票
   发表时间:2011-01-12  
rockyeah 写道
fastjson\src\test\java\com\alibaba\json\test\benchkmark\

benchkmark是不是该写成benchmark?


你说的对,拼写错误了 

笔记本和台式机两台电脑混着用,打字没感觉,打错字好几次了 
0 请登录后投票
   发表时间:2011-01-12  
为了fastjson的可持续发展,将会逐步建立一套性能基准测试

引用
src\test\java\com\alibaba\json\test\benchmark\



0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics