Search in sources :

Example 46 with JSONSerializer

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

the class MapSerializerTest method test_special2_s.

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

Example 47 with JSONSerializer

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

the class MapSerializerTest method test_string2_s.

public void test_string2_s() throws Exception {
    SerializeWriter out = new SerializeWriter();
    MapSerializer mapSerializer = new MapSerializer();
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("A", "1");
    map.put("B", "2");
    mapSerializer.write(new JSONSerializer(out), map, null, null, 0);
    Assert.assertEquals("{\"A\":\"1\",\"B\":\"2\"}", out.toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) MapSerializer(com.alibaba.fastjson.serializer.MapSerializer) LinkedHashMap(java.util.LinkedHashMap) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 48 with JSONSerializer

use of com.alibaba.fastjson.serializer.JSONSerializer 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 49 with JSONSerializer

use of com.alibaba.fastjson.serializer.JSONSerializer 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 50 with JSONSerializer

use of com.alibaba.fastjson.serializer.JSONSerializer 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)

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