package com.xhtt.ifserver.nio;
import com.alibaba.fastjson.JSON;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
/**
* auth: WenYF
* date: 2016/11/26
*/
public class JsonPerformenceTest {
static int max = 1000;
static int arrayMax = 100;
private static String str;
static {
JSONObject json = new JSONObject();
try {
for (int i = 0; i < max ; i ++) {
json.put("key" + i, "valuevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevalue" + i);
}
JSONArray array = new JSONArray();
for (int i = 0; i < arrayMax; i++) {
array.put("arrayarrayarrayarrayarray" + i);
}
json.put("array", array);
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println(json.toString());
str = json.toString();
}
@Test
public void testJsonCreate() {
JSONObject json = new JSONObject();
try {
for (int i = 0; i < max ; i ++) {
json.put("key" + i, "valuevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevalue" + i);
}
JSONArray array = new JSONArray();
for (int i = 0; i < arrayMax; i++) {
array.put("arrayarrayarrayarrayarray" + i);
}
json.put("array", array);
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println(json.toString());
}
@Test
public void testFastJsonCreate() {
com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
for (int i = 0; i < max ; i ++) {
json.put("key" + i, "valuevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevalue" + i);
}
com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray();
for (int i = 0; i < arrayMax; i++) {
array.add("arrayarrayarrayarrayarray" + i);
}
json.put("array", array);
System.out.println(json.toString());
}
@Test
public void testJsonParser() {
try {
JSONObject json = new JSONObject(str);
for (int i = 0; i < max ; i ++) {
json.get("key" + i);
}
int i = json.getJSONArray("array").length();
System.out.println("size = " + i);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Test
public void testFastJsonParser() {
com.alibaba.fastjson.JSONObject json = JSON.parseObject(str);
for (int i = 0; i < max ; i ++) {
json.get("key" + i);
}
int i = json.getJSONArray("array").size();
System.out.println("size = " + i);
}
}
json.jar | 50c3afb2166798c5de0896b90659d8a2b2f8fcec | http://www.json.org/license.html
fastjson |1.2.21 |https://github.com/alibaba/fastjson