XML跟Json的一些对比

xml、json序列化反序列化时间对比

 

结果:单位(毫秒)

xml:30810

fastjson:859

gson:1482

 

源代码:

private void compTime() {

	Person p = new Person();
	p.setAge(23);
	p.setName("李志鹏");
	p.setBirthday(new Date());

	long count = 100000;

	System.out
			.println("-----------------------compTime-----------------------");

	long startMili;
	long endMili;

	startMili = System.currentTimeMillis();
	for (int i = 0; i < count; i++) {
		Writer writer = new StringWriter();
		JAXB.marshal(p, writer);

		String xml = writer.toString();
		Reader reader = new StringReader(xml);
		Person retP = JAXB.unmarshal(reader, p.getClass());
	}
	endMili = System.currentTimeMillis();
	System.out.println("xml:" + (endMili - startMili));

	startMili = System.currentTimeMillis();
	for (int i = 0; i < count; i++) {
		String json = JSON.toJSONString(p);
		Person retP = JSON.parseObject(json, new TypeReference<Person>() {
		});
	}
	endMili = System.currentTimeMillis();
	System.out.println("fastjson:" + (endMili - startMili));
	
	startMili = System.currentTimeMillis();
	Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
			.create();
	for (int i = 0; i < count; i++) {
		String json = gson.toJson(p);
		Person retP = gson.fromJson(json,
				new TypeToken<Person>() {
				}.getType());
	}
	endMili = System.currentTimeMillis();
	System.out.println("gson:" + (endMili - startMili));		
}

 

序列化同一对象形成的数据大小对比:

结果:单位:字节

json:368
xml:568


源代码:

	private void compSize() {

		Map<String, Object> map = TestPrepareData.getStudentAndTeacher();
		String json = JSON.toJSONString(map);
		String xml = XmlJson.json2xml(json);

		System.out.println("json:" + json);
		System.out.println("xml:" + xml);
		System.out.println("json:" + json.length());
		System.out.println("xml:" + xml.length());

	}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值