Jackson优化使用实例

JSON的三种处理方式 
Jackson提供了三种可选的JSON处理方法(一种方式及其两个变型):

  • 流式 API(也称为"增量分析/生成") 读取和写入 JSON 内容作为离散事件。

  • 树模型 :提供一个 JSON 文档可变内存树的表示形式。

    • org.codehaus.jackson.map.ObjectMapper 生成树 ;树组成 JsonNode 节点集。

    • 树模型类似于 XML DOM。
  • 数据绑定: JSON和POJO相互转换,基于属性访问器规约或注解。

    • 有 两种变体: 简单 和 完整 的数据绑定:

    • 简单数据绑定: 是指从Java Map、List、String、Numbers、Boolean和空值进行转换

    • 完整数据绑定 :是指从任何 Java bean 类型 (及上文所述的"简单"类型) 进行转换

    • org.codehaus.jackson.map.ObjectMapper 对两个变种,进行编组(marshalling )处理 (写入 JSON) 和反编组(unmarshalling ,读 JSON)。

    • JAXB激励下的基于注释的 (代码优先)变种。

从使用的角度来看,总结这些3 种方法的用法如下:

  • 流 API: 性能最佳的方式 (最低开销、 速度最快的读/写; 其它二者基于它实现)。

  • 数据绑定 :使用最方便的方式。

  • 树模型: 最灵活的方式。

额,直接贴出所有代码吧,该说的都在代码中了

package com.wujintao.json;

import java.io.IOException;
import java.util.Date;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectReader;
import org.codehaus.jackson.map.ObjectWriter;
import org.junit.Test;

/**
 * http://wiki.fasterxml.com/JacksonDownload
 * http://wiki.fasterxml.com/JacksonInFiveMinutes
 * http://wiki.fasterxml.com/JacksonBestPracticesPerformance
 * http://jackson.codehaus.org/1.8.8/javadoc/index.html
 * http://wiki.fasterxml.com/ObjectReader
 * https://github.com/FasterXML/jackson-docs/wiki/ObjectWriter
 * jars:jackson-core-lgpl-1.8.10.jar,jackson-mapper-lgpl-1.8.10.jar
 * 
 * 1.实践结果,无论是哪种形式的转换,Jackson > Gson > Json-lib。Jackson的处理能力甚至高出Json-lib有10倍左右
 * 2.JSON-lib似乎已经停止更新,最新的版本也是基于JDK15,而Jackson的社区则较为活跃
 * 3.高并发情况下jackson表现尤为优越,内存占用甚少,json-lib会占用很多内存
 * 
 */
public class TestCase {
	
	@Test
	public void test() throws JsonGenerationException, JsonMappingException,
			IOException {
		long start = new Date().getTime();
		ObjectMapper mm = new ObjectMapper(); // can reuse, share
		Person pp = new Person();
		pp.setAdd("beijing");
		pp.setAge(11);
		pp.setSalary(1.1);
		pp.setSex('男');
		String jj = mm.writeValueAsString(pp);
		System.out.println(jj);

		Person pp2 = mm.readValue(jj, Person.class);
		System.out.println(pp2);
		
		long end = new Date().getTime();
		System.out.println("using ObjectMapper cost:"+(end-start)+"ms");

		System.out.println("=================================");
		// 7. Prefer ObjectReader/ObjectWriter over ObjectMapper
		// Although the main reason to prefer these objects is thread-safety
		// (and thus correctness),there may be performance benefits as well

		// 8.When reading a root-level sequence of POJOs,
		// readValues() method of ObjectReader can be much more efficient
		// than doing explicit iteration using ObjectMapper.readValue() method.

		long start2 = new Date().getTime();
		ObjectMapper mapper = new ObjectMapper();
		Person person = new Person();
		person.setAdd("beijing");
		person.setAge(11);
		person.setSalary(1.1);
		person.setSex('男');

		ObjectWriter writer = mapper.viewWriter(Person.class);
		String json = writer.writeValueAsString(person);
		System.out.println(json);
		// we'll be reading instances of MyBean
		ObjectReader reader = mapper.reader(Person.class);
		// and then do other configuration, if any, and read:
		Person result = reader.readValue(json);
		System.out.println(result);
		long end2 = new Date().getTime();
		System.out.println("using ObjectReader/ObjectWriter cost:"+(end2-start2)+"ms");
		
		//官网建议使用ObjectReader/ObjectWriter
	}
	
	//还有fast-json、Genson 等没做研究
}

猜你喜欢

转载自javacrazyer.iteye.com/blog/1746047