use of com.alibaba.fastjson.JSONWriter in project fastjson by alibaba.
the class Issue169 method test_for_issue.
public void test_for_issue() throws Exception {
StringWriter strWriter = new StringWriter();
SectionRequest req = new SectionRequest();
// 父类中的属性
req.setScreenHeight(100);
// 父类中的属性
req.setScreenWidth(12);
req.setTag("11");
JSONWriter writer = new JSONWriter(strWriter);
writer.startArray();
writer.writeObject(req);
writer.endArray();
writer.close();
String text = strWriter.toString();
StringReader strReader = new StringReader(text);
JSONReader reader = new JSONReader(strReader);
reader.startArray();
;
while (reader.hasNext()) {
SectionRequest vo = reader.readObject(SectionRequest.class);
System.out.println("tag:" + vo.getTag() + "screenHeight:" + vo.getScreenHeight() + "ScreenWidth:" + vo.getScreenWidth());
Assert.assertEquals(100, vo.getScreenHeight());
Assert.assertEquals(12, vo.getScreenWidth());
Assert.assertEquals("11", vo.getTag());
}
reader.endArray();
reader.close();
}
use of com.alibaba.fastjson.JSONWriter in project fastjson by alibaba.
the class JSONWriterTest_error method test_writer.
public void test_writer() throws Exception {
Field field = JSONWriter.class.getDeclaredField("context");
field.setAccessible(true);
StringWriter out = new StringWriter();
JSONWriter writer = new JSONWriter(out);
writer.config(SerializerFeature.UseSingleQuotes, true);
writer.startObject();
Object context = field.get(writer);
Field stateField = context.getClass().getDeclaredField("state");
stateField.setAccessible(true);
stateField.set(context, -1);
Exception error = null;
try {
writer.startObject();
} catch (Exception ex) {
error = ex;
}
Assert.assertNotNull(error);
writer.close();
}
use of com.alibaba.fastjson.JSONWriter in project fastjson by alibaba.
the class LargeTest method test_0.
public void test_0() throws Exception {
List<String> list = new ArrayList<String>(1000 * 1);
for (int i = 0; i < 100 * 1; ++i) {
list.add(Integer.toString(i));
}
File file = File.createTempFile("fastjson-stream-large", "json");
JSONWriter writer = new JSONWriter(new FileWriter(file));
writer.startArray();
writer.writeObject(list);
writer.endArray();
writer.close();
System.out.println(FileUtils.readFileToString(file));
JSONReader reader = new JSONReader(new FileReader(file));
reader.startArray();
reader.readObject();
reader.endArray();
reader.close();
}
use of com.alibaba.fastjson.JSONWriter in project fastjson by alibaba.
the class JSONWriterTest method test_4.
public void test_4() throws Exception {
StringWriter out = new StringWriter();
JSONWriter writer = new JSONWriter(out);
writer.writeStartArray();
writer.writeStartObject();
writer.writeEndObject();
writer.writeStartObject();
writer.writeEndObject();
writer.writeStartArray();
writer.writeEndArray();
{
writer.writeStartArray();
writer.writeStartArray();
writer.writeEndArray();
writer.writeStartArray();
writer.writeEndArray();
writer.writeEndArray();
writer.writeValue(1);
}
writer.writeEndArray();
writer.flush();
Assert.assertEquals("[{},{},[],[[],[]],1]", out.toString());
}
use of com.alibaba.fastjson.JSONWriter in project fastjson by alibaba.
the class JSONWriterTest method test_0.
public void test_0() throws Exception {
StringWriter out = new StringWriter();
JSONWriter writer = new JSONWriter(out);
writer.writeStartObject();
writer.writeEndObject();
writer.flush();
Assert.assertEquals("{}", out.toString());
}
Aggregations