Search in sources :

Example 1 with SerializeFilter

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

the class JSON method writeJSONString.

public static final // 
int writeJSONString(// 
OutputStream os, // 
Charset charset, // 
Object object, //
SerializeConfig config, //
SerializeFilter[] filters, //
String dateFormat, //
int defaultFeatures, SerializerFeature... features) throws IOException {
    SerializeWriter writer = new SerializeWriter(null, defaultFeatures, features);
    try {
        JSONSerializer serializer = new JSONSerializer(writer, config);
        if (dateFormat != null && dateFormat.length() != 0) {
            serializer.setDateFormat(dateFormat);
            serializer.config(SerializerFeature.WriteDateUseDateFormat, true);
        }
        if (filters != null) {
            for (SerializeFilter filter : filters) {
                serializer.addFilter(filter);
            }
        }
        serializer.write(object);
        int len = writer.writeToEx(os, charset);
        return len;
    } finally {
        writer.close();
    }
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) SerializeFilter(com.alibaba.fastjson.serializer.SerializeFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 2 with SerializeFilter

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

the class Issue204 method test_for_issue.

public void test_for_issue() throws Exception {
    VO vo = new VO();
    SerializeFilter filter = null;
    JSON.toJSONString(vo, SerializeConfig.getGlobalInstance(), filter);
    JSON.toJSONString(vo, SerializeConfig.getGlobalInstance(), new SerializeFilter[0]);
}
Also used : SerializeFilter(com.alibaba.fastjson.serializer.SerializeFilter)

Example 3 with SerializeFilter

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

the class EnumFieldsTest8 method test_enum.

public void test_enum() throws Exception {
    Model model = new Model();
    model.t1 = Type.A;
    model.t2 = null;
    ValueFilter valueFilter = new ValueFilter() {

        public Object process(Object object, String name, Object value) {
            return value;
        }
    };
    SerializeFilter[] filters = { valueFilter };
    String text = // 
    JSON.toJSONString(// 
    model, // 
    SerializeConfig.getGlobalInstance(), filters, null, // 
    0, // 
    SerializerFeature.QuoteFieldNames, // 
    SerializerFeature.BrowserCompatible, SerializerFeature.WriteEnumUsingName);
    Assert.assertEquals("{\"t1\":\"A\"}", text);
}
Also used : SerializeFilter(com.alibaba.fastjson.serializer.SerializeFilter) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter)

Example 4 with SerializeFilter

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

the class JSON method toJSONString.

/**
     * @since 1.2.9
     * @return
     */
public static // 
String toJSONString(// 
Object object, // 
SerializeConfig config, // 
SerializeFilter[] filters, //
String dateFormat, // 
int defaultFeatures, SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter(null, defaultFeatures, features);
    try {
        JSONSerializer serializer = new JSONSerializer(out, config);
        if (dateFormat != null && dateFormat.length() != 0) {
            serializer.setDateFormat(dateFormat);
            serializer.config(SerializerFeature.WriteDateUseDateFormat, true);
        }
        if (filters != null) {
            for (SerializeFilter filter : filters) {
                serializer.addFilter(filter);
            }
        }
        serializer.write(object);
        return out.toString();
    } finally {
        out.close();
    }
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) SerializeFilter(com.alibaba.fastjson.serializer.SerializeFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Aggregations

SerializeFilter (com.alibaba.fastjson.serializer.SerializeFilter)4 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)2 SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)2 ValueFilter (com.alibaba.fastjson.serializer.ValueFilter)1