Search in sources :

Example 61 with JSONSerializer

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

the class JSONSerializerFeatureTest method test_6_s.

public void test_6_s() throws Exception {
    JSONSerializer serializer = new JSONSerializer(new SerializeWriter());
    serializer.config(SerializerFeature.QuoteFieldNames, false);
    Assert.assertEquals(false, serializer.isEnabled(SerializerFeature.QuoteFieldNames));
    serializer.config(SerializerFeature.UseSingleQuotes, true);
    Assert.assertEquals(true, serializer.isEnabled(SerializerFeature.UseSingleQuotes));
    serializer.write(Collections.singletonMap("a'ge", 33));
    Assert.assertEquals("{'a\\'ge':33}", serializer.getWriter().toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 62 with JSONSerializer

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

the class JSONSerializerFeatureTest method test_1_s.

public void test_1_s() throws Exception {
    JSONSerializer serializer = new JSONSerializer(new SerializeWriter());
    Assert.assertEquals(true, serializer.isEnabled(SerializerFeature.QuoteFieldNames));
    Assert.assertEquals(false, serializer.isEnabled(SerializerFeature.UseSingleQuotes));
    serializer.config(SerializerFeature.UseSingleQuotes, true);
    Assert.assertEquals(true, serializer.isEnabled(SerializerFeature.UseSingleQuotes));
    serializer.write("abc");
    Assert.assertEquals("'abc'", serializer.getWriter().toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 63 with JSONSerializer

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

the class JSONSerializerTest method test_10.

public void test_10() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    serializer.write(3);
    Assert.assertEquals("3", serializer.getWriter().toString());
}
Also used : JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 64 with JSONSerializer

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

the class JSONSerializerTest method test_2.

public void test_2() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    serializer.write(Collections.EMPTY_MAP);
    Assert.assertEquals("{}", serializer.getWriter().toString());
}
Also used : JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 65 with JSONSerializer

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

the class JSONSerializerTest method test_5_null.

public void test_5_null() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    serializer.config(SerializerFeature.WriteMapNullValue, true);
    serializer.write(new A(null));
    Assert.assertEquals("{\"id\":null}", serializer.getWriter().toString());
}
Also used : 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