Skip to content

Commit

Permalink
improved JSONReader performance, fix #3627
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 31, 2021
1 parent a383623 commit 1a4fb9b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Expand Up @@ -327,6 +327,8 @@ public final Object parseObject(final Map object, Object fieldName) {
if (object != null
&& object.getClass().getName().equals(typeName)) {
clazz = object.getClass();
} else if ("java.util.HashMap".equals(typeName)) {
clazz = java.util.HashMap.class;
} else {

boolean allDigits = true;
Expand Down
@@ -0,0 +1,51 @@
package com.alibaba.json.bvt.parser.stream;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONReader;
import junit.framework.TestCase;
import org.junit.Assert;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

public class JSONReaderTest_5 extends TestCase {
public void test_read() throws Exception {
final int COUNT = 1000 * 10;
StringBuilder buf = new StringBuilder();
buf.append('[');
for (int i = 0; i < COUNT; ++i) {
if (i != 0) {
buf.append(',');
}
buf.append("{\"id\":").append(i).append('}');
}
buf.append(']');


JSONReader reader = new JSONReader(new StringReader(buf.toString()));

reader.startArray();
Map map = new HashMap();
int count = 0;
for (;;) {
if (reader.hasNext()) {
reader.startObject();
String key = reader.readString();
Long value = reader.readLong();
reader.endObject();
count++;
} else {
break;
}
}
assertEquals(COUNT, count);

reader.endArray();

reader.close();
}
}

0 comments on commit 1a4fb9b

Please sign in to comment.