Search in sources :

Example 41 with SerializeWriter

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

the class MapSerializerTest method test_empty_1.

public void test_empty_1() throws Exception {
    SerializeWriter out = new SerializeWriter();
    MapSerializer mapSerializer = new MapSerializer();
    mapSerializer.write(new JSONSerializer(out), Collections.EMPTY_MAP, null, null, 0);
    Assert.assertEquals("{}", out.toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) MapSerializer(com.alibaba.fastjson.serializer.MapSerializer) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 42 with SerializeWriter

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

the class MapTest method toJSONString.

public static final String toJSONString(Object object) {
    SerializeWriter out = new SerializeWriter();
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        serializer.config(SerializerFeature.SortField, false);
        serializer.config(SerializerFeature.UseSingleQuotes, true);
        serializer.write(object);
        return out.toString();
    } catch (StackOverflowError e) {
        throw new JSONException("maybe circular references", e);
    } finally {
        out.close();
    }
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONException(com.alibaba.fastjson.JSONException) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 43 with SerializeWriter

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

the class IntegerArrayEncodeTest method test_1_s.

public void test_1_s() throws Exception {
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.write(new Integer[] {});
    Assert.assertEquals("[]", out.toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 44 with SerializeWriter

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

the class IntegerArrayEncodeTest method test_3_s.

public void test_3_s() throws Exception {
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    StringBuilder sb = new StringBuilder();
    sb.append('[');
    int len = 1000;
    Integer[] array = new Integer[len];
    for (int i = 0; i < array.length; ++i) {
        array[i] = i;
        if (i != 0) {
            sb.append(',');
        }
        sb.append(i);
    }
    sb.append(']');
    serializer.write(array);
    Assert.assertEquals(sb.toString(), out.toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 45 with SerializeWriter

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

the class JSONSerializerFeatureTest method test_0.

public void test_0() throws Exception {
    JSONSerializer serializer = new JSONSerializer(new SerializeWriter());
    Assert.assertEquals(true, serializer.isEnabled(SerializerFeature.QuoteFieldNames));
    Assert.assertEquals(false, serializer.isEnabled(SerializerFeature.UseSingleQuotes));
}
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