Performance comparison of json serialization tools

json serialization tool performance comparison


fastjson
1.fastjson is a state-owned open source Java toolkit made by Ali
2.FastJson will have some problems in complex type bean conversion Json, there may be reference types, resulting in Json conversion errors, and references need to be formulated .

jackson
1.Jackson is a data processing tool suite, its highlight is a streaming JSON parser and generator. It is designed for Java, but can also handle other encodings other than JSON.
2. From our statistics in Github, it should be the most popular JSON parser.
3. Jackson will have problems with json conversion beans of complex types, and some collection Map and List conversion will have problems.
4.Jackson converts Json for complex types of beans, and the converted json format is not the standard Json format

. JSON.simple
JSON.simple is a Java tool library for JSON encoding and decoding. It aims to create a lightweight, simple and high-performance tool library.

GSON 1.Google
's GSON
2.GSON This Java library can convert between Java objects and JSON.
3. Gson is impeccable in function, but there is a gap in performance compared to FastJson.

JSONP
1.Oracle's JSONP
2.JSONP (JSON Processing) is a set of Java APIs for JSON processing. From the name, it is used to generate and parse JSON strings. This is an open source implementation of the JSR353 specification.


Summarize
1. If your application often handles large JSON files (190MB), then Jackson should be your dish. GSON struggles quite a bit with large files.
2. If you mainly deal with small file (1K) requests, such as the initialization of a microservice or distributed architecture, then GSON is the first choice. Jackson's performance on small files was less than satisfactory.
3. If you deal with both files frequently, JSON.simple, which ranks second in both rounds, is more suitable for such scenarios. Jackson and GSON do not perform well at different file sizes.
4. If there are only functional requirements and no performance requirements, you can use Google's Gson
5. If there are performance requirements, you can use Gson to convert bean to json to ensure the correct data, and use FastJson to convert Json to Bean
6. If GSON is not generated every time When creating a new object, it is the fastest.

Conclusions :
1. GSON is the fastest when initialized.
2. When using GSON or jackJson for Json processing, it is better to have a global parsing object. Do not generate it every time it is called, which will have a great impact on performance. In addition, after testing, there is no thread safety problem.
3. FastJson has outstanding performance in the application scenario where json is converted to object.


Measured comparison:
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.1</version>
</dependency>
<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>2.6.2</version>
</dependency>
<dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-core-asl</artifactId>
	<version>1.9.13</version>
</dependency>

                       
                       
package com.cesmart;

import java.util.List;

class Demo {
	private String cmd;
	private String content;
	private List<User> userList;

	public static class User{
		private int age;
		private String name;
		private int year;
		public int getAge() {
			return age;
		}
		public void setAge(int age) {
			this.age = age;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getYear() {
			return year;
		}
		public void setYear(int year) {
			this.year = year;
		}
	
	}

	public String getCmd() {
		return cmd;
	}

	public void setCmd(String cmd) {
		this.cmd = cmd;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

}


Each time a new object is created:
package com.cesmart;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cesmart.Demo.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

public class Application {
	private static long count = 100;

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		long gsonTime = gson ();
		System.gc();
		long jackJsonTime = jackJson();
		System.gc();
		long fastJsonTime = fastJson();
		System.gc();
		System.out.println("---------------------------------------------------------------");
		long gsonTime2 = gsonJson();
		System.gc();
		long jackJsonTime2 = jackJsonJson();
		System.gc();
		long fastJsonTime2 = fastJsonJson();

		System.out.println("---------------------------------------------------------------");
		System.out.println("object to json: " + "gsonTime       == " + gsonTime);
		System.out.println("object to json: " + "jackJsonTime   == " + jackJsonTime);
		System.out.println("object to json: " + "fastJsonTime   == " + fastJsonTime);
		System.out.println("---------------------------------------------------------------");
		System.out.println("json to object: " + "gsonTime       == " + gsonTime2);
		System.out.println("json to object: " + "jackJsonTime   == " + jackJsonTime2);
		System.out.println("json to object: " + "fastJsonTime   == " + fastJsonTime2);
	}

	private static Demo getDemo(int i) {
		Demo demo = new Demo();

		List<User> userList = new ArrayList<User>();

		User user1 = new User();
		user1.setAge(i);
		user1.setName("name");
		user1.setYear(i);

		User user2 = new User();
		user2.setAge(i + 2);
		user2.setName("name");
		user2.setYear(i + 2);

		User user3 = new User();
		user3.setAge(i + 3);
		user3.setName("name");
		user3.setYear(i + 3);

		userList.add(user1);
		userList.add(user2);
		userList.add(user3);

		demo.setCmd("1");
		demo.setContent("dd");
		demo.setUserList(userList);

		return demo;
	}

	private static long gson() {
		long endTime1 = System.currentTimeMillis();
		for (int i = 0; i < count; i++) {
			Demo demo = getDemo(i);
			Gson gson = new Gson ();
			String StringJson = gson.toJson (demo);
			System.out.println(StringJson);
		}
		long endTime2 = System.currentTimeMillis();
		System.out.println("gson--obj--json:" + (count * 1000) / (endTime2 - endTime1) + "次/s" + " use time == "
				+ (endTime2 - endTime1) + "ms");
		return endTime2 - endTime1;
	}

	private static long jackJson() throws Exception {
		long endTime1 = System.currentTimeMillis();
		for (int i = 0; i < count; i++) {
			Demo demo = getDemo(i);
			ObjectMapper objectMapper = new ObjectMapper();
			String StringJson = objectMapper.writeValueAsString(demo);
			System.out.println(StringJson);
		}
		long endTime2 = System.currentTimeMillis();
		System.out.println("jackJson--obj--json:" + (count * 1000) / (endTime2 - endTime1) + "次/s" + " use time == "
				+ (endTime2 - endTime1) + "ms");
		return endTime2 - endTime1;
	}

	private static long fastJson() {
		long endTime1 = System.currentTimeMillis();
		for (int i = 0; i < count; i++) {
			Demo demo = getDemo(i);
			// JSONObject jsonObject = new JSONObject();
			String StringJson = JSON.toJSONString(demo);
			System.out.println(StringJson);
		}
		long endTime2 = System.currentTimeMillis();
		System.out.println("fastJson--obj--json:" + (count * 1000) / (endTime2 - endTime1) + "次/s" + " use time == "
				+ (endTime2 - endTime1) + "ms");
		return endTime2 - endTime1;
	}

	private static long gsonJson() {
		long endTime1 = System.currentTimeMillis();

		for (int i = 0; i < count; i++) {
			Gson gson = new Gson ();
			Demo demo = getDemo(i);
			String StringJson = JSON.toJSONString(demo);

			gson.fromJson(StringJson, Demo.class);
		}
		long endTime2 = System.currentTimeMillis();
		System.out.println("gson--json--obj:" + (count * 1000) / (endTime2 - endTime1) + "次/s" + " use time == "
				+ (endTime2 - endTime1) + "ms");
		return endTime2 - endTime1;
	}

	private static long jackJsonJson() throws Exception {
		long endTime1 = System.currentTimeMillis();
		for (int i = 0; i < count; i++) {
			ObjectMapper objectMapper = new ObjectMapper();
			Demo demo = getDemo(i);
			String StringJson = JSON.toJSONString(demo);

			objectMapper.readValue(StringJson, Demo.class);
		}
		long endTime2 = System.currentTimeMillis();
		System.out.println("jackJson--json--obj:" + (count * 1000) / (endTime2 - endTime1) + "次/s" + " use time == "
				+ (endTime2 - endTime1) + "ms");
		return endTime2 - endTime1;
	}

	private static long fastJsonJson() {
		long endTime1 = System.currentTimeMillis();
		for (int i = 0; i < count; i++) {
			Demo demo = getDemo(i);
			String StringJson = JSON.toJSONString(demo);

			JSON.parseObject(StringJson, Demo.class);
		}
		long endTime2 = System.currentTimeMillis();
		System.out.println("fastJson--json--obj:" + (count * 1000) / (endTime2 - endTime1) + "次/s" + " use time == "
				+ (endTime2 - endTime1) + "ms");
		return endTime2 - endTime1;
	}

}


Output result:


Create a new object during initialization:
package com.cesmart;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cesmart.Demo.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

public class Application2 {
	private static ObjectMapper objectMapper = new ObjectMapper();

	private static Gson gson = new Gson ();

	private static JSONObject jsonObject = new JSONObject();

	private static long count = 100;

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		long gsonTime = gson ();
		System.gc();
		long jackJsonTime = jackJson();
		System.gc();
		long fastJsonTime = fastJson();
		System.gc();
		System.out.println("---------------------------------------------------------------");
		long gsonTime2 = gsonJson();
		System.gc();
		long jackJsonTime2 = jackJsonJson();
		System.gc();
		long fastJsonTime2 = fastJsonJson();

		System.out.println("---------------------------------------------------------------");
		System.out.println("object to json: " + "gsonTime       == " + gsonTime);
		System.out.println("object to json: " + "jackJsonTime   == " + jackJsonTime);
		System.out.println("object to json: " + "fastJsonTime   == " + fastJsonTime);
		System.out.println("---------------------------------------------------------------");
		System.out.println("json to object: " + "gsonTime       == " + gsonTime2);
		System.out.println("json to object: " + "jackJsonTime   == " + jackJsonTime2);
		System.out.println("json to object: " + "fastJsonTime   == " + fastJsonTime2);

	}

	private static Demo getDemo(int i) {
		Demo demo = new Demo();

		List<User> userList = new ArrayList<User>();

		User user1 = new User();
		user1.setAge(i);
		user1.setName("name");
		user1.setYear(i);

		User user2 = new User();
		user2.setAge(i + 2);
		user2.setName("name");
		user2.setYear(i + 2);

		User user3 = new User();
		user3.setAge(i + 3);
		user3.setName("name");
		user3.setYear(i + 3);

		userList.add(user1);
		userList.add(user2);
		userList.add(user3);

		demo.setCmd("1");
		demo.setContent("dd");
		demo.setUserList(userList);

		return demo;
	}

	private static long gson() {
		long endTime1 = System.currentTimeMillis();

		for (int i = 0; i < count; i++) {
			Demo demo = getDemo(i);

			String StringJson = gson.toJson (demo);
			System.out.println(StringJson);
		}
		long endTime2 = System.currentTimeMillis();
		System.out.println("gson--obj--json:" + (count * 1000) / (endTime2 - endTime1) + "次/s" + " use time == "
				+ (endTime2 - endTime1) + "ms");
		return endTime2 - endTime1;
	}

	private static long jackJson() throws Exception {
		long endTime1 = System.currentTimeMillis();
		for (int i = 0; i < count; i++) {
			Demo demo = getDemo(i);

			String StringJson = objectMapper.writeValueAsString(demo);
			System.out.println(StringJson);
		}
		long endTime2 = System.currentTimeMillis();
		System.out.println("jackJson--obj--json:" + (count * 1000) / (endTime2 - endTime1) + "次/s" + " use time == "
				+ (endTime2 - endTime1) + "ms");
		return endTime2 - endTime1;
	}

	private static long fastJson() {
		long endTime1 = System.currentTimeMillis();
		for (int i = 0; i < count; i++) {
			Demo demo = getDemo(i);
			String StringJson = JSON.toJSONString(demo);
			System.out.println(StringJson);
		}
		long endTime2 = System.currentTimeMillis();
		System.out.println("fastJson--obj--json:" + (count * 1000) / (endTime2 - endTime1) + "次/s" + " use time == "
				+ (endTime2 - endTime1) + "ms");
		return endTime2 - endTime1;
	}

	private static long gsonJson() {
		long endTime1 = System.currentTimeMillis();

		for (int i = 0; i < count; i++) {
			Demo demo = getDemo(i);
			String StringJson = JSON.toJSONString(demo);

			gson.fromJson(StringJson, Demo.class);
		}
		long endTime2 = System.currentTimeMillis();
		System.out.println("gson--json--obj:" + (count * 1000) / (endTime2 - endTime1) + "次/s" + " use time == "
				+ (endTime2 - endTime1) + "ms");
		return endTime2 - endTime1;
	}

	private static long jackJsonJson() throws Exception {
		long endTime1 = System.currentTimeMillis();
		for (int i = 0; i < count; i++) {
			Demo demo = getDemo(i);
			String StringJson = JSON.toJSONString(demo);

			objectMapper.readValue(StringJson, Demo.class);
		}
		long endTime2 = System.currentTimeMillis();
		System.out.println("jackJson--json--obj:" + (count * 1000) / (endTime2 - endTime1) + "次/s" + " use time == "
				+ (endTime2 - endTime1) + "ms");
		return endTime2 - endTime1;
	}

	private static long fastJsonJson() {
		long endTime1 = System.currentTimeMillis();
		for (int i = 0; i < count; i++) {
			Demo demo = getDemo(i);
			String StringJson = JSON.toJSONString(demo);

			JSON.parseObject(StringJson, Demo.class);
		}
		long endTime2 = System.currentTimeMillis();
		System.out.println("fastJson--json--obj:" + (count * 1000) / (endTime2 - endTime1) + "次/s" + " use time == "
				+ (endTime2 - endTime1) + "ms");
		return endTime2 - endTime1;
	}

}

Output result:



Reference original text: http://blog.csdn.net/accountwcx/article/details/50252657
Reference original text: http://www.open-open.com/lib/view/open1434377191317.html
Reference original text: http: //www.oschina.net/code/snippet_1156226_26432
Reference text: http://chenyanxi.blog.51cto.com/4599355/1543445
Reference text (the fastest implementation of GSON): http://chenyanxi.blog.51cto.com /4599355/1543445

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326993631&siteId=291194637