package test;
import cn.hutool.core.lang.Console;
import cn.jiguang.entity.ErrorMessage;
import com.alibaba.fastjson.JSON;
import com.dslplatform.json.DslJson;
import com.dslplatform.json.JsonReader;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jsoniter.JsonIterator;
import com.jsoniter.ValueType;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.Assert;
import java.io.*;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
/**
* @desc: dsl-json fastjson jackson jsoniter 效率比较
* @author: zengxc
* @date: 2018/4/4
*/
public class JSONTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
public static String[] factors = {};
private static String JSON_STR = null;
private static int count = 0;
private final DslJson dslJson = new DslJson();
@Test
public void testJsoniter2() throws IOException {
JsonIterator jsonIterator = JsonIterator.parse("[123,{'id':20, 'errorMsg':'this error'},456]".replace('\'', '"'));
AtomicReference errorMessage = new AtomicReference<>();
jsonIterator.readAny().forEach(any -> {
if (any.valueType() == ValueType.OBJECT) {
errorMessage.set(any.as(ErrorMessage.class));
}
});
Console.print(errorMessage.get());
Assert.isTrue(true, "Not to false");
}
@Test
public void testJsoniter() throws IOException {
JsonIterator jsonIterator = JsonIterator.parse("{'id':10, 'errorMsg':'this error'}".replace('\'', '"'));
ErrorMessage errorMessage = jsonIterator.read(ErrorMessage.class);
Assert.notNull(errorMessage, "can't be empty ");
}
@Test
public void testFor() throws IOException {
File file = new File("out.txt");
file.mkdirs();
PrintWriter printWriter = new PrintWriter(file);
StringBuffer buffer = new StringBuffer();
buffer.append("{");
for (int i = 0; i < 1000000; i++) {
if (i == 999999) {
buffer.append("\"k" + i + "\":" + "\"v" + i + "\"" + "}");
break;
}
buffer.append("\"k" + i + "\":" + "\"v" + i + "\"" + ",");
}
printWriter.println(buffer.toString());
printWriter.flush();
}
@Before
public void init() throws Exception {
FileReader fis = new FileReader(new File("D:\\WorkSpace\\springboot-helloworld\\out.txt"));
BufferedReader br = new BufferedReader(fis);
JSON_STR = br.readLine();
}
@Test
public void testFastJson() {
long startTime = System.currentTimeMillis();
Map map = JSON.parseObject(JSON_STR, Map.class);
map.forEach((k, v) -> {
count++;
});
System.out.println(System.currentTimeMillis() - startTime);
System.out.println(count);
}
@Test
public void testJsoniter03() throws IOException {
long startTime = System.currentTimeMillis();
JsonIterator jsonIterator = JsonIterator.parse(JSON_STR);
Map map = jsonIterator.readAny().as(Map.class);
map.forEach((k, v) -> {
count++;
});
System.out.println(System.currentTimeMillis() - startTime);
System.out.println(count);
}
@Test
public void testJackson() throws IOException {
long startTime = System.currentTimeMillis();
Map map = MAPPER.readValue(JSON_STR, Map.class);
map.forEach((k, v) -> {
count++;
});
System.out.println(System.currentTimeMillis() - startTime);
System.out.println(count);
}
@Test
public void testDslJson() throws IOException {
long startTime = System.currentTimeMillis();
final byte[] buff = JSON_STR.getBytes("UTF-8");
JsonReader jsonReader = dslJson.newReader(buff);
Map map = jsonReader.next(Map.class);
map.forEach((k, v) -> {
count++;
});
System.out.println(System.currentTimeMillis() - startTime);
System.out.println(count);
}
}
结论:
dslJson > jsonIter > fastJson > jackson