Search in sources :

Example 46 with SerializeWriter

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

the class JSONSerializerFeatureTest method test_4_s.

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

Example 47 with SerializeWriter

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

the class JSONSerializerFeatureTest method test_6.

public void test_6() 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 48 with SerializeWriter

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

the class JSONSerializerFeatureTest method test_2.

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

Example 49 with SerializeWriter

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

the class JSONSerializerFeatureTest method test_2_s.

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

Example 50 with SerializeWriter

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

the class JSONSerializerFeatureTest method test_1.

public void test_1() 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)

Aggregations

SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)345 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)206 PropertyFilter (com.alibaba.fastjson.serializer.PropertyFilter)54 HashMap (java.util.HashMap)47 StringWriter (java.io.StringWriter)38 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 MapSerializer (com.alibaba.fastjson.serializer.MapSerializer)10 JavaBeanSerializer (com.alibaba.fastjson.serializer.JavaBeanSerializer)8 Date (java.util.Date)8 Map (java.util.Map)8 CollectionCodec (com.alibaba.fastjson.serializer.CollectionCodec)6 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)6 LinkedHashMap (java.util.LinkedHashMap)6 JSONException (com.alibaba.fastjson.JSONException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 A (com.alibaba.json.bvt.serializer.filters.PropertyFilterTest.A)4 JSONObject (com.alibaba.fastjson.JSONObject)3