support stream api

This commit is contained in:
wenshao
2013-06-23 22:33:54 +08:00
parent 060a29e1c1
commit 50085dfbd0
17 changed files with 813 additions and 65 deletions
@@ -0,0 +1,47 @@
package com.alibaba.fastjson;
import java.io.StringReader;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.parser.JSONScanner;
public class JSONReaderTest_array_array extends TestCase {
String text = "[[],[],[],[],[], [],[],[],[],[]]";
public void test_read() throws Exception {
JSONReader reader = new JSONReader(new StringReader(text));
reader.startArray();
int count = 0;
while (reader.hasNext()) {
Object item = reader.readObject();
Assert.assertEquals(JSONArray.class, item.getClass());
count++;
}
Assert.assertEquals(10, count);
reader.endArray();
reader.close();
}
public void test_read_1() throws Exception {
JSONReader reader = new JSONReader(new JSONScanner(text));
reader.startArray();
int count = 0;
while (reader.hasNext()) {
Object item = reader.readObject();
Assert.assertEquals(JSONArray.class, item.getClass());
count++;
}
Assert.assertEquals(10, count);
reader.endArray();
reader.close();
}
}
@@ -0,0 +1,47 @@
package com.alibaba.fastjson;
import java.io.StringReader;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.parser.JSONScanner;
public class JSONReaderTest_array_array_2 extends TestCase {
String text = "[[],[],[],[],[], [],[],[],[],[]]";
public void test_read() throws Exception {
JSONReader reader = new JSONReader(new StringReader(text));
reader.startArray();
int count = 0;
while (reader.hasNext()) {
reader.startArray();
reader.endArray();
count++;
}
Assert.assertEquals(10, count);
reader.endArray();
reader.close();
}
public void test_read_1() throws Exception {
JSONReader reader = new JSONReader(new JSONScanner(text));
reader.startArray();
int count = 0;
while (reader.hasNext()) {
reader.startArray();
reader.endArray();
count++;
}
Assert.assertEquals(10, count);
reader.endArray();
reader.close();
}
}
@@ -0,0 +1,45 @@
package com.alibaba.fastjson;
import java.io.StringReader;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.parser.JSONScanner;
public class JSONReaderTest_array_object extends TestCase {
String text = "[{},{},{},{},{} ,{},{},{},{},{}]";
public void test_read() throws Exception {
JSONReader reader = new JSONReader(new StringReader(text));
reader.startArray();
int count = 0;
while (reader.hasNext()) {
reader.readObject();
count++;
}
Assert.assertEquals(10, count);
reader.endArray();
reader.close();
}
public void test_read_1() throws Exception {
JSONReader reader = new JSONReader(new JSONScanner(text));
reader.startArray();
int count = 0;
while (reader.hasNext()) {
reader.readObject();
count++;
}
Assert.assertEquals(10, count);
reader.endArray();
reader.close();
}
}
@@ -0,0 +1,47 @@
package com.alibaba.fastjson;
import java.io.StringReader;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.parser.JSONScanner;
public class JSONReaderTest_array_object_2 extends TestCase {
String text = "[{},{},{},{},{} ,{},{},{},{},{}]";
public void test_read() throws Exception {
JSONReader reader = new JSONReader(new StringReader(text));
reader.startArray();
int count = 0;
while (reader.hasNext()) {
reader.startObject();
reader.endObject();
count++;
}
Assert.assertEquals(10, count);
reader.endArray();
reader.close();
}
public void test_read_1() throws Exception {
JSONReader reader = new JSONReader(new JSONScanner(text));
reader.startArray();
int count = 0;
while (reader.hasNext()) {
reader.startObject();
reader.endObject();
count++;
}
Assert.assertEquals(10, count);
reader.endArray();
reader.close();
}
}
@@ -0,0 +1,46 @@
package com.alibaba.fastjson;
import java.io.StringReader;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.parser.JSONScanner;
public class JSONReaderTest_object_int extends TestCase {
public void test_read() throws Exception {
String text = "{\"f0\":0,\"f1\":1,\"f2\":2,\"f3\":3,\"f4\":4, " + //
"\"f5\":5,\"f6\":6,\"f7\":7,\"f8\":8,\"f9\":9}";
JSONReader reader = new JSONReader(new StringReader(text));
reader.startObject();
int count = 0;
while (reader.hasNext()) {
String key = (String) reader.readObject();
Integer value = reader.readInteger();
count++;
}
Assert.assertEquals(10, count);
reader.endObject();
reader.close();
}
public void test_read_1() throws Exception {
String text = "[{},{},{},{},{} ,{},{},{},{},{}]";
JSONReader reader = new JSONReader(new JSONScanner(text));
reader.startArray();
int count = 0;
while (reader.hasNext()) {
reader.readObject();
count++;
}
Assert.assertEquals(10, count);
reader.endArray();
reader.close();
}
}
@@ -0,0 +1,49 @@
package com.alibaba.fastjson;
import java.io.StringReader;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.JSONScanner;
public class JSONReaderTest_object_int_unquote extends TestCase {
String text = "{f0:0,f1:1,f2:2,f3:3,f4:4, " + //
"f5:5,f6:6,f7:7,f8:8,f9:9}";
public void test_read() throws Exception {
JSONReader reader = new JSONReader(new StringReader(text));
reader.startObject();
int count = 0;
while (reader.hasNext()) {
String key = (String) reader.readObject();
Integer value = reader.readInteger();
count++;
}
Assert.assertEquals(10, count);
reader.endObject();
reader.close();
}
public void test_read_1() throws Exception {
JSONReader reader = new JSONReader(new JSONScanner(text));
reader.startObject();
int count = 0;
while (reader.hasNext()) {
String key = (String) reader.readObject();
Integer value = reader.readInteger();
count++;
}
Assert.assertEquals(10, count);
reader.endObject();
reader.close();
}
}
@@ -0,0 +1,50 @@
package com.alibaba.fastjson;
import java.io.StringReader;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.parser.JSONScanner;
public class JSONReaderTest_object_long extends TestCase {
String text = "{\"f0\":0,\"f1\":1,\"f2\":2,\"f3\":3,\"f4\":4, " + //
"\"f5\":5,\"f6\":6,\"f7\":7,\"f8\":8,\"f9\":9}";
public void test_read() throws Exception {
JSONReader reader = new JSONReader(new StringReader(text));
reader.startObject();
int count = 0;
while (reader.hasNext()) {
String key = (String) reader.readObject();
Long value = reader.readLong();
count++;
}
Assert.assertEquals(10, count);
reader.endObject();
reader.close();
}
public void test_read_1() throws Exception {
JSONReader reader = new JSONReader(new JSONScanner(text));
reader.startObject();
int count = 0;
while (reader.hasNext()) {
String key = (String) reader.readObject();
Long value = reader.readLong();
count++;
}
Assert.assertEquals(10, count);
reader.endObject();
reader.close();
}
}
@@ -0,0 +1,48 @@
package com.alibaba.fastjson;
import java.io.StringReader;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.parser.JSONScanner;
public class JSONReaderTest_object_object extends TestCase {
String text = "{\"f0\":{},\"f1\":{},\"f2\":{},\"f3\":{},\"f4\":{}, " + //
"\"f5\":{},\"f6\":{},\"f7\":{},\"f8\":{},\"f9\":{}}";
public void test_read() throws Exception {
JSONReader reader = new JSONReader(new StringReader(text));
reader.startObject();
int count = 0;
while (reader.hasNext()) {
String key = (String) reader.readObject();
Object value = reader.readObject();
count++;
}
Assert.assertEquals(10, count);
reader.endObject();
reader.close();
}
public void test_read_1() throws Exception {
JSONReader reader = new JSONReader(new JSONScanner(text));
reader.startObject();
int count = 0;
while (reader.hasNext()) {
String key = (String) reader.readObject();
Object value = reader.readObject();
count++;
}
Assert.assertEquals(10, count);
reader.endObject();
reader.close();
}
}
@@ -0,0 +1,50 @@
package com.alibaba.fastjson;
import java.io.StringReader;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.parser.JSONScanner;
public class JSONReaderTest_object_string extends TestCase {
String text = "{\"f0\":\"0\",\"f1\":\"1\",\"f2\":\"2\",\"f3\":\"3\",\"f4\":\"4\", " + //
"\"f5\":\"5\",\"f6\":\"6\",\"f7\":\"7\",\"f8\":\"8\",\"f9\":\"9\"}";
public void test_read() throws Exception {
JSONReader reader = new JSONReader(new StringReader(text));
reader.startObject();
int count = 0;
while (reader.hasNext()) {
String key = (String) reader.readObject();
String value = reader.readString();
count++;
}
Assert.assertEquals(10, count);
reader.endObject();
reader.close();
}
public void test_read_1() throws Exception {
JSONReader reader = new JSONReader(new JSONScanner(text));
reader.startObject();
int count = 0;
while (reader.hasNext()) {
String key = (String) reader.readObject();
Long value = reader.readLong();
count++;
}
Assert.assertEquals(10, count);
reader.endObject();
reader.close();
}
}
@@ -105,12 +105,14 @@ public class JSONWriterTest extends TestCase {
writer.writeEndArray();
writer.writeEndArray();
writer.writeValue(1);
}
writer.writeEndArray();
writer.flush();
Assert.assertEquals("[{},{},[],[[],[]]]", out.toString());
Assert.assertEquals("[{},{},[],[[],[]],1]", out.toString());
}
}