Search in sources :

Example 76 with JSONSerializer

use of com.alibaba.fastjson.serializer.JSONSerializer in project fastjson by alibaba.

the class SerializeWriterTest_15 method test_writer_1.

@SuppressWarnings("rawtypes")
public void test_writer_1() throws Exception {
    StringWriter strOut = new StringWriter();
    SerializeWriter out = new SerializeWriter(strOut, 1);
    out.config(SerializerFeature.UseSingleQuotes, true);
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        Map map = Collections.singletonMap("", "a");
        serializer.write(map);
    } finally {
        out.close();
    }
    Assert.assertEquals("{'':'a'}", strOut.toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) StringWriter(java.io.StringWriter) Map(java.util.Map) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 77 with JSONSerializer

use of com.alibaba.fastjson.serializer.JSONSerializer in project fastjson by alibaba.

the class SerializeWriterTest_8 method test_singleQuote.

public void test_singleQuote() throws Exception {
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < 1024; ++i) {
        buf.append('a');
    }
    buf.append("中国");
    buf.append("\0");
    SerializeWriter out = new SerializeWriter(new StringWriter());
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        serializer.config(SerializerFeature.QuoteFieldNames, false);
        serializer.config(SerializerFeature.UseSingleQuotes, true);
        serializer.write(Collections.singletonMap(buf.toString(), ""));
    } finally {
        out.close();
    }
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) StringWriter(java.io.StringWriter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 78 with JSONSerializer

use of com.alibaba.fastjson.serializer.JSONSerializer in project fastjson by alibaba.

the class ObjectWriteTest method test_objectWriteTest.

public void test_objectWriteTest() throws Exception {
    ObjectSerializer serializer = SerializeConfig.getGlobalInstance().getObjectWriter(Model.class);
    JSONSerializer jsonSerializer = new JSONSerializer();
    serializer.write(jsonSerializer, null, "a", Model.class, 0);
    String text = jsonSerializer.out.toString();
    assertEquals("null", text);
}
Also used : ObjectSerializer(com.alibaba.fastjson.serializer.ObjectSerializer) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 79 with JSONSerializer

use of com.alibaba.fastjson.serializer.JSONSerializer in project fastjson by alibaba.

the class PascalNameFilterTest method test_0.

public void test_0() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    serializer.getNameFilters().add(new PascalNameFilter());
    VO vo = new VO();
    vo.setId(123);
    vo.setName("wenshao");
    serializer.write(vo);
    Assert.assertEquals("{\"Id\":123,\"Name\":\"wenshao\"}", serializer.toString());
    serializer.close();
}
Also used : PascalNameFilter(com.alibaba.fastjson.serializer.PascalNameFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 80 with JSONSerializer

use of com.alibaba.fastjson.serializer.JSONSerializer in project fastjson by alibaba.

the class SerializeWriterTest_16 method test_writer_1.

public void test_writer_1() throws Exception {
    StringWriter strOut = new StringWriter();
    SerializeWriter out = new SerializeWriter(strOut, 14);
    out.config(SerializerFeature.BrowserCompatible, true);
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        VO vo = new VO();
        vo.setValue("abcd\t");
        serializer.write(vo);
    } finally {
        out.close();
    }
    Assert.assertEquals("{value:\"abcd\\t\"}", strOut.toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) StringWriter(java.io.StringWriter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Aggregations

JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)263 SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)206 PropertyFilter (com.alibaba.fastjson.serializer.PropertyFilter)54 HashMap (java.util.HashMap)48 NameFilter (com.alibaba.fastjson.serializer.NameFilter)36 ValueFilter (com.alibaba.fastjson.serializer.ValueFilter)19 ArrayList (java.util.ArrayList)12 ListSerializer (com.alibaba.fastjson.serializer.ListSerializer)11 StringWriter (java.io.StringWriter)11 JavaBeanSerializer (com.alibaba.fastjson.serializer.JavaBeanSerializer)10 MapSerializer (com.alibaba.fastjson.serializer.MapSerializer)10 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)10 Map (java.util.Map)10 Date (java.util.Date)9 LinkedHashMap (java.util.LinkedHashMap)8 JSONException (com.alibaba.fastjson.JSONException)7 CollectionCodec (com.alibaba.fastjson.serializer.CollectionCodec)6 A (com.alibaba.json.bvt.serializer.filters.PropertyFilterTest.A)4 IOException (java.io.IOException)4 Color (java.awt.Color)3