Search in sources :

Example 11 with JSONWriter

use of com.alibaba.fastjson.JSONWriter in project fastjson by alibaba.

the class JSONWriterTest_2 method test_writer.

public void test_writer() throws Exception {
    StringWriter out = new StringWriter();
    JSONWriter writer = new JSONWriter(out);
    writer.config(SerializerFeature.UseSingleQuotes, true);
    writer.startObject();
    writer.writeObject("a");
    writer.writeObject("1");
    writer.writeObject("b");
    writer.writeObject("2");
    writer.writeObject("c");
    writer.writeObject("3");
    writer.endObject();
    writer.close();
    Assert.assertEquals("{'a':'1','b':'2','c':'3'}", out.toString());
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) StringWriter(java.io.StringWriter)

Example 12 with JSONWriter

use of com.alibaba.fastjson.JSONWriter in project fastjson by alibaba.

the class JSONWriterTest_3 method test_writer.

public void test_writer() throws Exception {
    StringWriter out = new StringWriter();
    JSONWriter writer = new JSONWriter(out);
    writer.config(SerializerFeature.UseSingleQuotes, true);
    writer.startObject();
    writer.startObject();
    writer.endObject();
    writer.startObject();
    writer.endObject();
    writer.endObject();
    writer.close();
    Assert.assertEquals("{{}:{}}", out.toString());
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) StringWriter(java.io.StringWriter)

Example 13 with JSONWriter

use of com.alibaba.fastjson.JSONWriter in project fastjson by alibaba.

the class JSONWriterTest_4 method test_writer.

public void test_writer() throws Exception {
    StringWriter out = new StringWriter();
    JSONWriter writer = new JSONWriter(out);
    writer.config(SerializerFeature.UseSingleQuotes, true);
    writer.writeObject(Collections.emptyMap());
    writer.close();
    Assert.assertEquals("{}", out.toString());
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) StringWriter(java.io.StringWriter)

Example 14 with JSONWriter

use of com.alibaba.fastjson.JSONWriter in project fastjson by alibaba.

the class JSONWriterTest_5 method test_writer.

public void test_writer() throws Exception {
    StringWriter out = new StringWriter();
    JSONWriter writer = new JSONWriter(out);
    writer.startObject();
    writer.writeKey("value");
    writer.writeObject((String) null);
    writer.endObject();
    writer.close();
    Assert.assertEquals("{\"value\":null}", out.toString());
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) StringWriter(java.io.StringWriter)

Example 15 with JSONWriter

use of com.alibaba.fastjson.JSONWriter in project fastjson by alibaba.

the class JSONWriterTest_6 method test_writer.

public void test_writer() throws Exception {
    StringWriter out = new StringWriter();
    JSONWriter writer = new JSONWriter(out);
    writer.config(SerializerFeature.WriteNullStringAsEmpty, true);
    writer.startObject();
    writer.writeKey("value");
    writer.writeObject((String) null);
    writer.endObject();
    writer.close();
    Assert.assertEquals("{\"value\":\"\"}", 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