Search in sources :

Example 1 with JSONWriter

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();
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) JSONReader(com.alibaba.fastjson.JSONReader)

Example 2 with JSONWriter

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();
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) Field(java.lang.reflect.Field) StringWriter(java.io.StringWriter)

Example 3 with JSONWriter

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();
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) FileReader(java.io.FileReader) JSONReader(com.alibaba.fastjson.JSONReader) File(java.io.File)

Example 4 with JSONWriter

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());
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) StringWriter(java.io.StringWriter)

Example 5 with JSONWriter

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());
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) StringWriter(java.io.StringWriter)

Aggregations

JSONWriter (com.alibaba.fastjson.JSONWriter)18 StringWriter (java.io.StringWriter)17 JSONReader (com.alibaba.fastjson.JSONReader)3 ArrayList (java.util.ArrayList)3 StringReader (java.io.StringReader)2 List (java.util.List)2 File (java.io.File)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.testng.annotations.Test)1