其他
请不要在 JDK 7+ 中使用这个 JSON 包了!
Json-lib 介绍
http://json-lib.sourceforge.net/
package net.mayswind;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import java.io.File;
public class JsonLibBenchmark {
public static void main(String[] args) throws Exception {
String data = FileUtils.readFileToString(new File("Z:\\data.json"));
benchmark(data, 5);
}
private static void benchmark(String data, int count) {
long startTime = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
JSONObject root = JSONObject.fromObject(data);
}
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println(String.format("count=%d, elapsed time=%d ms, avg cost=%f ms", count, elapsedTime, (double) elapsedTime / count));
}
}
{
"data":
[
{
"foo": 0123456789,
"bar": 1234567890
},
{
"foo": 0123456789,
"bar": 1234567890
},
...
]
}
private static JSONObject _fromJSONTokener(JSONTokener tokener, JsonConfig jsonConfig) {
try {
if (tokener.matches("null.*")) {
fireObjectStartEvent(jsonConfig);
fireObjectEndEvent(jsonConfig);
return new JSONObject(true);
} else if (tokener.nextClean() != '{') {
throw tokener.syntaxError("A JSONObject text must begin with '{'");
} else {
fireObjectStartEvent(jsonConfig);
Collection exclusions = jsonConfig.getMergedExcludes();
PropertyFilter jsonPropertyFilter = jsonConfig.getJsonPropertyFilter();
JSONObject jsonObject = new JSONObject();
...
public boolean matches(String pattern) {
String str = this.mySource.substring(this.myIndex);
return RegexpUtils.getMatcher(pattern).matches(str);
}
public String substring(int beginIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}
public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if (offset <= value.length) {
this.value = "".value;
return;
}
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count);
}
END
学习资料:
最近热文:
Spring干货:
点击「阅读原文」带你飞~